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?


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.


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.

  • MgSm88: It's in the penultimate paragraph.

  • Not sure I agree that C# is ever weakly typed (without tedious user intervention).

    Can you show an example of what you mean?

    PS: My normal criteria for a weakly typed language is "if it looks like a number, it can be one, no matter the backing type".

  • I think C++ is the same thing. It is type-safe but also allows casts. Seems like a lot of languages are "strongly-typed" AND "weakly-typed."

  • Is the gist of today's blog that strongly/weak typing is a Partial-Ordering?

  • leppie, That just demonstrates Eric's point about how "strongly-typed" and "weakly-typed" don't have widely-accepted definitions. Personally, to avoid confusion, I would propose using the term "horribly-typed" to refer to the languages that fit your criteria of "weakly-typed." :)

  • Pythonistas will often say Python is a strongly (yet dynamically) typed language, meaning type conversion is usually explicit. If x = 5 and y = "foo", then in C#, x + y yields "5foo" whereas Python doesn't allow the implicit string-conversion (well, in a statically typed language, it's merely sugar, but in a dynamic language, it can lead to ambiguity)

  • I've been hearing these type definitions for a long time, but I never completelly understood them until now.

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

    Well, it depends how hard I hit the keyboard keys when I'm programming.


  • Not suprisingly (given that this is a question of semantics), you're playing a bit of a semantic game here.

    The argument seems to be 'Since I can get around C#'s type enforcement, it's not really a strongly typed language'.. which is like arguing that since I can remove the brakes in my car, clearly the car is inherently unsafe. It's *technically* true, but only when you consider the worst cases, rather than the typical and ignores the notion of 'intent'.

    Oddly, I thought you were going to go in a different direction - and argue that the dynamic extensions to C# render it weakly typed (ie: var, but as we all know, var isn't actually var). 'Object' is a better example of that dynamic nature - but it's been around since the start.

    To most programmers, I believe, the term 'strongly typed' means "the compiler expects me to know the types for variables and data and get them right at compile time, not deferring it to run time unless I explicitly tell the compiler I'm doing something weird - otherwise, it will say 'no'". That may not be a formal definition - but it's certainly how most people use the term.

    So is C# strongly typed? Best answer I have is 'most of the time'.

  • Great Article. C# doesn't enforce these 3 safety systems all time but only most time, but this can allow risky developers to get performance gain in performance critical jobs. Java enforce these safety systems all time , but usually this drop performance making applications as eclipse and netbeans much slower than monodevelop and sharpdevelop, with no way out to increase performace.

  • @  Commentor Eric:

    Java is strongly typed that do enforce these restrictions all the time.

    1-Java doesn't allow any use of pointers at all, no  way to override this with "unsafe"

    2-Java doesn't allow "dynamic"

    but java is weakly typed as :

    1-It removes type information from generics in compile time (type erasure) since java 5 but type information will be reserved to be checked during runtime starting from java 8 insha'Allah

    On the other hand C# is reserving type info in generics for runtime checking since .net 2

    2- Java as c# allow cast operators

  • what the means of type verification and type system here?

  • @Jeff Lewis: A car *is* inherently unsafe!

    When you take a ton of steel, add in a dozen gallons of highly combustible liquid, then intentionally detonate that liquid thousands of time per minute to propel the vehicle to high speeds, what you get is definitely unsafe.

  • @Jimmy: In C#, 5 + "foo" is (effective) shorthand for String.Format("{0}foo", 5), or maybe String.Format("{0}{1}, 5, "foo"). Either way, it's not actually an overload on +.

    </pedant> ;)

  • Interesting read. At the end I was imagining Eric as Obi-wan Kenobi in "Return of the Jedi", when he was talking about it depends on your point of view regarding Darth Vader.

Page 2 of 3 (36 items) 123