Microsoft: "C# is strongly typed ... Just kidding!"

by Jon Davis 11. May 2008 11:40

Microsoft has changed its mind in keeping C# a strongly typed language. All of the virtues of strongly typing your variables, and depending on alternative languages such as Visual Basic for such coding techniques as late binding, are soon to be flushed down the toilet. Since C# is a product and not an open-source language firmly grounded upon disciplines (apparently ECMA is a farce), Microsoft is apparently trying hard to keep the much-loved language evolving according to the demands and requests of its users rather than keep each language feature unique to each language dependent upon its featured language. Late binding was one of the last things that made Visual Basic more versatile to programmers than C#, and it is a feature that Microsoft has finally caved on and decided to add to C#. In the end, where dynamic code is used, there is no member checking on late-bound objects, and code like this is plausible.

static void Main(string[] args)
        object myDynamicObject = GetDynamicObject();
// call a method  
        myDynamicObject.someString = "value";
// Set a field
        myDynamicObject[0] = 25;             
// Access an indexer

The above code, which syntax is not finalized, gives some relief to doubters with its explicit "dynamic { }" block. It suggests that C# will still enforce type safety and member checking at compile-time, except only where late-bound code is invoked in a "dynamic" declaration block.

Microsoft added "strongly typed anonymous typing" in C# 3.0 with the keyword var and with the ability to create and populate anonymous types. I for one thought var was C#'s biggest invitation for bad code abuse, but I was persuaded that "it's okay, it's still strongly typed" because ultimately the type does infer a strong type at compile-time and you get compile-time checking if you use members that are not part of what is assigned.

But late binding invokes no such member checking. Late binding is potentially useful in places where System.Reflection was previously used to perform invocations of type members without having a strong reference to them at compile-time, such as in plug-in support. However, as with var, there is potential for abuse of this functionality; outside of plug-in support, it should not be used at all in my opinion. (But I can just imagine the sort of fist-fighting that was going on between the CLR team and the Microsoft Office COM automation team.)

On the other hand, I can see where this functionality would be necessary for being able to interoperate with objects created in a dynamic language. I hope this will mean that Silverlight v3.0 will natively be able to support interoperating with browser Javascript objects, although that's likely wishful thinking since browser Javascript does not run in the CLR...

EDIT: So um as I was writing this post I was all bent out of shape but then I noticed the dynamic {} block and realized it's okay so but then I left the "bent out of shape" bits at the top so *sigh* .. I hate mondays .. I'm tired ..

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Software Development


Add comment

(Will show your Gravatar icon)  

  Country flag

  • Comment
  • Preview


Powered by BlogEngine.NET
Theme by Mads Kristensen

About the author

Jon Davis (aka "stimpy77") has been a programmer, developer, and consultant for web and Windows software solutions professionally since 1997, with experience ranging from OS and hardware support to DHTML programming to IIS/ASP web apps to Java network programming to Visual Basic applications to C# desktop apps.
Software in all forms is also his sole hobby, whether playing PC games or tinkering with programming them. "I was playing Defender on the Commodore 64," he reminisces, "when I decided at the age of 12 or so that I want to be a computer programmer when I grow up."

Jon was previously employed as a senior .NET developer at a very well-known Internet services company whom you're more likely than not to have directly done business with. However, this blog and all of have no affiliation with, and are not representative of, his former employer in any way.

Contact Me 

Tag cloud


<<  May 2021  >>

View posts in large calendar