You are currently browsing the tag archive for the ‘C# 4.0’ tag.

How to: DISTINCT, SUM, COUNT the DataTable for a given XML?

 

Advertisements

Couple of articles ago, I did write about new dynamic type feature in C# 4.0

Some what related to the dynamic is the ExpandoObject.

Yep I know it does sound like an eight armed sea creature, but don’t worry – its just an object that comes with .NET 4.0. Dont know what Microsofties think whenever they come up with such a name.

MSDN says, ExpandoObject “represents an object whose members can be dynamically added and removed at run time.”

Add a property:

sampleObject.test = "Dynamic Property";
Console.WriteLine(sampleObject.test);
Console.WriteLine(sampleObject.test.GetType());

Output:

// This code example produces the following output:
// Dynamic Property
// System.String

Remove a property:

dynamic employee = new ExpandoObject();
employee.Name = "John Smith";
((IDictionary)employee).Remove("Name");

Associate and call events dynamically:

class Program
{
static void Main(string[] args)
{
dynamic sampleObject = new ExpandoObject();

// Create a new event and initialize it with null.
sampleObject.sampleEvent = null;

// Add an event handler.
sampleObject.sampleEvent += new EventHandler(SampleHandler);

// Raise an event for testing purposes.
sampleObject.sampleEvent(sampleObject, new EventArgs());
}

// Event handler.
static void SampleHandler(object sender, EventArgs e)
{
Console.WriteLine("SampleHandler for {0} event", sender);
}
}

Output:

// This code example produces the following output:
SampleHandler for System.Dynamic.ExpandoObject event.

Cons of ExpandoObject:

Though the usage of dynamic keyword seems quite interesting; but it could become very hard to catch errors. For instance, a new developer who doesn’t know much about it adds a property that was not required; the compiler won’t show an error – only, probably runtime will. This means, typos won’t be picked up during compile time because you can just declare about anything anywhere.

Plus, this code gives an impression that it type-safe, which it clearly, is not.

Even the tools like ReSharper would not be able to grab that error.

Anyway, you may also want to look into the unusual uses of Expando Object.

Happy coding!

So can you use EF v4.0 with .NET 3.5 in Visual Studio 2008?

Short answer, No!

Longer answer… is still no, but lets go through the reasons it is so;

Similar to, that you can’t use C# v2.0 features without at least VS 2005 and you can’t use C# v3.0 features without VS 2008; it is that you cannot use EF v4.0 in VS2008.

Still a ‘why’? Follow on…

Because EF v4.0 requires .NET v4.0(that is, 4.0.30319.1 to be exact – stable release out in the market, approximately 38 days ago); which means EF v4.0 is actually a part of .NET v4.0 – And, 3.5 uses CLR v2.0 while 4.0 uses CLR v4.0.

Among its many new improvements, Visual Studio 2010 introduces the much-awaited Entity Framework 4.0 and WCF Data Services 4.0 (formerly ADO.NET Data Services), which together simplify how you model, consume and produce data.

So to conclude: This means,

  • .NET v1.0 was used with VS2002 having C# v1.0
  • .NET v1.1 – VS2003
  • .NET v2.0 – VS2005 – C# v2.0
  • .NET v3.0 – Useable in VS2005
  • .NET v3.5 – Visual Studio 2008 having C# v3.0
  • .NET v4.0 – Visual Studio 2010 with C# v4.0

And according to Jon Skeet on SO, C# v5.0 contains major new speculated features such as Meta Programming.

If this makes you interested more in v4.0, checkout what’s new in the latest .NET Framework.

References:
1. C# Indepth by Jon Skeet: http://csharpindepth.com/Articles/Chapter1/Versions.aspx
2. .NET Frameworks: http://en.wikipedia.org/wiki/.NET_Framework
3. Microsoft .NET Framework: http://msdn.microsoft.com/en-us/netframework/default.aspx
4. .NET Framework Evolution Map: http://bloggingabout.net/blogs/fernando/archive/2009/01/11/net-framework-release-map.aspx

There is a new type, a contextual keyword, called dynamic in C# v4.0; getting to much of a popularity among advanced programming.It is used to call operations of dynamic languages (DLR).

To put it simply, binding is the association of a symbol within a program with an address in memory. The term “binding”, in this case refers to the process of resolving types, members, and operations.

  • Static binding happens at compile time, where a compiler identifies, resolves, and throws exception if necessary, at compile time.
  • Dynamic binding refers to identification, resolution, and throwing of exception if any, during runtime.

Dynamic binding in useful in cases where you know that a certain member, function, or operation would be available at run time. But compiler doesn’t know about that; the compiler would not throw any exceptions as well because it would just “trust you”, because you used dynamic keyword.

“A dynamic type tells the compiler to relax. We expect the runtime type of d to have
a Quack method. We just can’t prove it statically.” – (C# in a Nutshell Chap-4 Pg161)

dynamic dynTest = new Test();
dynTest.AnotherTest(1);

dynamic means that all expressions using dynTest is bound at execution time, rather than at compile-time, and thus makes it behave dynamically.

dynamic versus var keyword

A lot of times dynamic is confused with the var keyword; and to prove that statement, following is such an example that creates quite a confusion.

//CODE 1
var varTest = new Test();
varTest.AnotherTest(1);
//CODE 2
dynamic dynTest = new Test();
dynTest.AnotherTest(1);

Here, in CODE 1, var means the static type is inferred – in this case it’s same as

Test varTest = new Test();

var is static typed – the compiler actually knows the type All the binding is still done entirely statically.

dynamic, in CODE 2, means that all expressions using dynTest is bound at execution time, rather than at compile-time, and thus makes it behave dynamically. Compiler won’t check whether the AnotherTest() method exists – the behavior is determined at execution/run time.

Note that, somewhat on a tangent, if the object implements IDynamicMetaObjectProvider it could decide what to do with the call at execution time, responding to any method call – in other words, there doesn’t have to be a “real” method called AnotherTest() at all.

This type of binding is also called “Custom Binding”; Custom binding occurs when a dynamic object implements IDynamicMetaObjectProvider (IDMOP). I’ll write more about custom binding sometime.

If you’d like to read more, here is a worth viewing, if you are short of reading time, article C# 4.0 – Dynamic Language Runtime by Microsoft’s Bruno Terkaly with excellent visuals.

Happy dynamic’s! (0:

%d bloggers like this: