Skip to main content

Posts

Showing posts from May, 2010

Covariance and Contravariance-General Discussion

If you have just started the exploration of .Net Framework 4.0, two terms namely Covariance and Contravariance might have been heard. The concept that these terms encapsulate are used by most developer almost daily, however there has never been any botheration about the terminologies. Now, what actually these terms mean and how are these going to affect us as a developer, if we dive in to the details. The simple answer is it’s always good to know your tools before actually using them. Enough philosophy, let’s get to the business. Starting the discussion let me reiterate that in addition to Covariance and Contravariance, there is another terminology, Invariance. I’ll by start here by diving into the details of Invariance and then proceed further. Invariance: Invariance can be better understood by considering the types in .Net.>net has basically two type, value-types and reference-types. Value types (int, double etc) are invariant i.e. the types can’t be interchanged either

ExpandoObject Class Part-I

ExpandoObject Class Today I’ll cover the basics of ExpandoObject, which is similar to “dynamic”(covered in last post). The major dfference in Dynamic and ExpandoObject class is the fact that using an instance of this class, members can be dynamically added and removed at run time.In addition it can be used to get abd set the values of dynamically added members. The class is a part of System.Dynamic namespace This class implements the following interfaces. IDynamicMetaObjectProvider IDictionary ICollection > IEnumerable > IEnumerable INotifyPropertyChanged Example: dynamic expcheck = new ExpandoObject(); expcheck.Test = " This is Pradeep's Test"; Response.Write(expcheck.Test); Response.Write(expcheck.Test.GetType()); In the example above, an object of ExpandoObject class is created and and property(“Test”) is added to the object at runtime. To verify the concept, I print out the values and the output shows the value of the proper

C# 4.0 dynamic Objects-Part-I

 C# 4.0 dynamic Objects Revisiting the C# 4.0 article (see my earlier post), new features introduced in C# 4.o can be grouped under the following Dynamic binding Named and optional arguments Variance Today I’ll discuss the most interesting feature the “Dynamic Programming”. C# 4.0 introduces a new static type called dynamic (under using System.Dynamic namespace) Dynamic objects expose members such as properties and methods at run time rather than at compile time. At compile time the dynamic object is assumed (no static binding) to support any specified operation and it is only at runtime that the error will crop up if the concerned operation is not supported. The type dynamic can be considered as a special version of the object type . Any object can be implicitly converted to dynamic with type checking postponed until runtime. Conversely, expressions of type dynamic can be implicitly converted to object , or any other type, as long as there exists a conversion at runtime.