5

I'm reading JoeDuffy book where he said: C# is a (mostly) statically typed.

Most of the articles and books I’ve read that describe C# as a strongly typed language are effectively using “strongly typed” to mean statically typed.

Where is the truth? What does mostly mean? (perhaps referring to very few features that step outside of the CLR's type-safety?)

2 Answers2

6

The most obvious non-statically typed part of C# is the dynamic keyword. A variable declared as dynamic defers type-related checks until runtime, so the following code is legal:

dynamic x = "a string";
x = 7;
x.NonExistingMethod();

Other parts of C# (including the var keyword) are statically typed. People sometimes confuse var with the keyword in JavaScript, but what var in C# does is type inference. Still statically typed, only less explicit.

sara
  • 3,521
  • 14
  • 34
  • `int[] a = new int[3] { 1, 2, 3 }; Console.WriteLine(a[3]);` Why I can build this code successfully? –  Mar 20 '16 at 14:50
  • I think that overflowing a buffer or indexing into an arbitrary location of system memory are just not possible –  Mar 20 '16 at 14:52
  • @ivan_petrushenko why wouldn't you be able to? What part do you expect to fail? the way I understand it, indexers in C# are basically just method calls. The array type exposes an indexer that accepts an `int` argument. 3 is a valid `int`, so there's nothing fishy going on here. – sara Mar 20 '16 at 15:20
2

Statically typed programming languages do type checking (the process of verifying and enforcing the constraints of types) at compile-time as opposed to run-time. Dynamically typed programming languages do type checking at run-time as opposed to Compile-time.

Strong/Weak typing is about how strictly types are distinguished (e.g. whether the language tries to do implicit conversion from strings to numbers).

Simply put it this way: in a statically typed language the type is static, meaning once you set a variable to a type, you CANNOT change it.

The main advantage here is that all kinds of checking can be done by the compiler, and therefore a lot of stupid bugs are caught at a very early stage.

A language is dynamically typed if the type of a variable is interpreted at run-time. This means that you as a programmer can write a little quicker because you do not have to specify type every-time. Example: Perl, Ruby, Python

Most scripting languages have this feature as there is no compiler to do static type-checking anyway, but you may find yourself searching for a bug that is due to the interpreter misinterpreting the type of a variable

MMM
  • 3,132
  • 3
  • 20
  • 32
  • 1
    Can you answer to the main question? What does mostly mean? –  Mar 20 '16 at 13:56
  • @ivan_petrushenko In C# 3.0 and later you can use the "var" keyword instead of the type name in many cases – MMM Mar 20 '16 at 13:59
  • 4
    @MidhunMundayadan the `var` keyword does not let go of static typing. try wrtiting `var x = "hi"; x = 1;` and see if the compiler complains. the `dynamic` keyword does however. – sara Mar 20 '16 at 14:05
  • 1
    Please do incorporate what actually applies to C# into this answer. – TaW Mar 20 '16 at 14:10