Is C# a strongly typed or a weakly typed language?

Is C# a strongly typed or a weakly typed language?

Rate This
  • Comments 36

Presented as a dialogue, as is my wont!

Is C# a strongly typed or a weakly typed language?

Yes.

That is unhelpful.

I don't doubt it. Interestingly, if you rephrased the question as an "and" question, the answer would be the same.

What? You mean, is C# a strongly typed and a weakly typed language?

Yes, C# is a strongly typed language and a weakly typed language.

I'm confused.

Me too. Perhaps you should tell me precisely what you mean by "strongly typed" and "weakly typed".

Um. I don't actually know what I mean by those terms, so perhaps that is the question I should be asking. What does it really mean for a language to be "weakly typed" or "strongly typed"?

"Weakly typed" means "this language uses a type verification system that I find distasteful", and "strongly typed" means "this language uses a type system that I find attractive".

No way!

Way, dude.

Really?

These terms are meaningless and you should avoid them. Wikipedia lists eleven different meanings for "strongly typed", several of which contradict each other. Any time two people use "strongly typed" or "weakly typed" in a conversation about programming languages, odds are good that they have two subtly or grossly different meanings in their heads for those terms, and are therefore automatically talking past each other.

But surely they mean something other than "unattractive" or "attractive"!

I do exaggerate somewhat for comedic effect. So lets say: a more-strongly-typed language is one that has some restriction in its type system that a more-weakly-typed language it is being compared to lacks. That's all you can really say without more context.

How can I have sensible conversations about languages and their type systems then?

You can provide the missing context. Instead of using "strongly typed" and "weakly typed", actually describe the restriction you mean. For example, C# is for the most part a statically typed language, because the compiler determines facts about the types of every expression. C# is for the most part a type safe language because it prevents values of one static type from being stored in variables of an incompatible type (and other similar type errors). And C# is for the most part a memory safe language because it prevents accidental access to bad memory.

Thus, someone who thinks that "strongly typed" means "the language encourages static typing, type safety and memory safety in the vast majority of normal programs" would classify C# as a "strongly typed" language. C# is certainly more strongly typed than languages that do not have these restrictions in their type systems.

But here's the thing: because C# is a pragmatic language there is a way to override all three of those safety systems. Cast operators and "dynamic" in C# 4 override compile-time type checking and replace it with runtime type checking, and "unsafe" blocks allow you to turn off type safety and memory safety should you need to. Someone who thinks that "strongly typed" means "the language absolutely positively guarantees static typing, type safety and memory safety under all circumstances" would quite rightly classify C# as "weakly typed". C# is not as strongly typed as languages that do enforce these restrictions all the time.

So which is it, strong or weak? It is impossible to say because it depends on the point of view of the speaker, it depends on what they are comparing it to, and it depends on their attitude towards various language features. It's therefore best to simply avoid these terms altogether, and speak more precisely about type system features.

  • Excellent post, as usual.

    "C# is not as strongly typed as languages that do enforce these restrictions all the time."

    Can anyone mention here some of these languages.

  • My favorite definition is that how "strongly typed" a language is measures the degree to which circumventing type restrictions is considered bad coding practice by its programming community.

    This definition has the advantages of being completely orthogonal to static typing (and every other feature of a type system), of aligning well with many informal uses of the term, and of being patently absurd as a way of describing languages.

  • @Eric: ADA comes to mind. They have a very strict type system (A string of length 5 is not assignable into a string variable of length 6, for example).

  • @Eric

    Not sure, but maybe Haskell? No pointers\unsafe mode. No dynamic. No casting (?).

  • "as is my wont!" -  It must have taken a lot of strong typing to add such an excellent phrase that is often underutilized in today's discourse.

  • While I would classify Haskell as perhaps more 'strongly' typed than C#, there are still ways to get around its typing discipline. There are dynamic types (Data.Dynamic), casts, both safe (Data.Typeable.cast) and unsafe (Unsafe.Coerce.unsafeCoerce), and pointers (mostly used for interfacing with C).

  • You left out anything about the "dynamic" keyword/type. I think maybe for the public at large's sake (as your blog is often regarded as a definitive source on C#) that you should mention dynamic and how it throws an additional wrench into the picture.

  • > Not sure, but maybe Haskell? No pointers\unsafe mode. No dynamic. No casting (?).

    Technically, Haskell has a whole host of unsafe "modes": www.haskell.org/hoogle the parent of all unsafe* being unsafePerformIO which allows bypassing the IO monad and performing IO from anywhere (which may completely crash the runtime, or result in very wacky behavior, such as the IO code being called a million times in a second due to STM). Casts and pointers are part of the unsafe* stuff (unsafeCoerce, unsafe*Ptr).

    There are also dynamic types: hackage.haskell.org/.../Data-Dynamic.html but they're kind-of safe (see fromDyn and fromDynamic)

  • Hi Eric,

    My question isn't actually in relation to this posts topic, although I will mention a "dynamically typed" language... :-) Not sure where else to direct questions to you.

    Message cascades were recently added to the Dart programming language:

    news.dartlang.org/.../method-cascades-in-dart-posted-by-gilad.html

    Was just wondering, is it in the realm of possibility for these to appear in a future version of C#? Or is there something in their design which makes them incompatible with C#?

    I like that folks would no longer have to design their APIs to be "fluent". I have a personal library of extension methods for making WPF more fluent (github.com/.../FluentWpf). Message cascades in C# would eliminate the need for things like FluentWpf.

    Message cascades also appear to make object initializer syntax (new Obj() { abc = 10 }) unnecessary (although I realize, that's here to stay).

    Thanks for any insight!

    Signed,

    Envious of Dart programmers right now

  • @dharmatech Visual Basic has that.

    msdn.microsoft.com/.../wc500chb.aspx

  • Eric : "...C# is a pragmatic language ..."

    DBJ : Is C++ "a pragmatic language" ?

  • What do you think about the term Manifest vs. Latent typing?

  • "C# is for the most part a type safe language because it prevents values of one static type from being stored in variables of an incompatible type."

    Unless you use StructLayout.Explicit and define two fields with incompatible types and the same FieldOffset value.

  • And sometimes it depends on the C# grammar :)

    -> enum test : int // COMPILES

    -> enum test2 : System.Int32 // DOES NOT COMPILE (integral-type expected)

  • @Chris Eargle: I guess you missed the *for the most part* bit.

Page 1 of 3 (36 items) 123