Deep C# - Dynamic C#
Written by Mike James   
Thursday, 16 January 2020
Article Index
Deep C# - Dynamic C#
Static Typing
Anonymous Typing
Dynamic typing
Overloading

What exactly is C#'s dynamic type all about? Is it dynamic or is it just static typing under cover? And how does it change things like early binding, virtual and non-virtual?

This is a chapter of our ebook on C#, a work in progress.

Deep C#

 coverCsharp

 Chapter List

  1. Value And Reference
    Value and reference are a fundamental division in the way C# treats data. It is important that you understand the differences and most importantly when to use a struct and when to use a class. These aren't just differences in efficiency, they affect the semantics too.

  2. Dynamic C#
    What exactly is C#'s dynamic type all about? Is it dynamic or is it just static typing under cover? And how does it change things like early binding, virtual and non-virtual?

  3. Passing Parameters
    Passing parameters is easy as it always works in the same way but the effects aren't always the same. It can be confusing and even error prone unless you understand how it all works.

  4. Inheritance
    Inheritance is a simple idea, until you try to make use of it.

  5. Casting – the escape from strong typing
    Casting is one of the most confusing aspects of any modern language and it often makes beginners think hard. But if you know why you are doing it then the how makes a lot more sense.

  6. Controlling Inheritance
    Inheritance is a great idea but it is a powerful technique that can be misused. C# provides the tools to keep inheritance under control or to turn it off completely if you want to.

  7. Delegates
    Delegates are C#'s original way of allowing you to work with functions as if they were first class objects. The aim may be simple but the need to define a type and then an instance of the type can be confusing. Let's see if we can make it all seem logical.

  8. Multicast delegates and events
    Multicast delegates are useful in their own right but they also form the basis on which the C# event system is built. We take a close look at how they work and how to use them. For example, did you know you could add and subtract delegates?

  9. Anonymous Methods, Lambdas And Closures
    Anonymous methods aren't particularly new, but they have hidden depths and lead on to lambdas and the idea of a closure. These are all important ideas in modern programming.

  10. Take Exception To Everything

  11. What's The Matter With Pointers?
    In C# pointers have been replaced by references but there is still a need for the raw dangerous original concept. You could say "what's so dangerous about pointers?"
Multicast delegates and events
Tuesday, 25 May 2010
Article Index
Multicast delegates and events
Events
Generic Events

Multicast delegates are useful in their own right but they also form the basis on which the C# event system is built. We take a close look at how they work and how to use them. For example, did you know you could add and subtract delegates?

C# is a language that started out in the style of Java with strong typing and very little extra. Over time it has added features in a careful and thoughtful way to include things that you might not associate with a strongly typed "traditional" object oriented language. For many this is what makes C# special. 

Back in version, 4.0, it added the unthinkable - late binding with dynamic typing. This provides the modern C# programmer with idioms that previously would have been thought out of the question. Dynamic typing goes directly against static strong typing that is supposed to catch so many errors are compile time that otherwise would only appear at run time. 

The worry is that by adopting such loose practice C# might become a mess by adopting models that go against its birthright of strict typing and object-oriented procedural code? 

What is interesting is the way that C# combines the best of both worlds - but see if you agree. 

On being dynamic in C#

Strong typing isn't necessarily the good idea it first seems to be - it has some advantages but so does dynamic typing.

It is also true that it is the biggest current divide between programmers and programming languages is into camps that swear allegiance to the strong typing principle or simply attempt to ignore type altogether because it is simpler.

C# started out as a young, clean, strongly-typed language but as it reaches its middle age it is displaying signs of being envious of the currently popular dynamic languages. This is made worse by the desire to meet the demand to be simpler, easier to use and more productive.

Some definitions

First it is worth saying what strict, static and dynamic typing are all about.

A language uses strict typing if it demands that every item of data has a type and can only be stored in a variable of that type.

Usually things are a little more complicated in that types form a hierarchy and a variable of a given type can store data of that type and types derived from it. Put simply a strictly typed language "worries" about type and generates type exceptions when it thinks you are trying to store data of the wrong type in a variable. 

Contrast this with a freely typed language that tries to make it possible for you to store any data in any variable and tries to make sense of what you want to do taking the actual type into account. Put simply a freely typed language tries to ignore data type as much as possible and generates a type exception only when you write something that cannot be reasonably achieved. the type free language motto is "if it can be made to work its ok".

A strongly typed language can also be statically or dynamically typed.

Static typing means that all data types can be worked out at compile time and any errors can be flagged at compile time. In other words you can work out the types of all data and all variables simply by reading the code.

A dynamic language allows a variable to determine its type at run time. In this case you can't necessarily work out the type of a variable by simply reading the code and not all type errors can be picked up at compile time.

Notice that there are two variations possible on dynamic typing. You could enforce a rule which fixes the type of a variable once it is known i.e. variables can't change their type once set. Or you could implement fully dynamic typed variables that can be used to store anything anytime and change there type according to what is stored in them.

You might at this point think that its all very simple but in an object oriented language like C# type actually plays a role in determining what happens in a program - it can change the flow of control and hence things are more subtle.

csharp



Last Updated ( Thursday, 16 January 2020 )