Browse DevX
Sign up for e-mail newsletters from DevX


Explore C# 4's New Dynamic Types and Named/Optional Parameters : Page 2

Leverage these new features in C# 4.0 to interoperate with new dynamic .NET languages, and simplify your code.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Dynamic Lookup

The introduction of dynamic types to the Common Type System led directly to one of the great new features added to C# 4.0: You longer need to worry about the type of an object when assigning values returned from methods or expressions; the runtime performs the necessary binding for you according to the returned value's type.

Static and Dynamic Typing

Statically typed languages such as C++, C#, or Java perform type checking at compile time. In contrast, dynamically typed languages such as JavaScript, PERL, and Ruby perform type checks at runtime. C# was originally designed as a language based on strong typing—which is good because the compiler can ensure that types match, catching potential bugs earlier in the development cycle. The change now to include dynamic typing in C# is intended to let you invoke different kinds of objects such as COM, and JavaScript seamlessly.

The var and dynamic keywords

To support dynamic variable declaration, C# 4.0 introduces the dynamic keyword. Both the var and dynamic keywords provide local type inference in C#. You need not specify the data type on the left hand-side of an assignment operator, and the system will bind to the correct type on the fly. However, unlike the dynamic keyword, when you use var you must still specify the type on the right-hand side of an assignment operator. Using the dynamic keyword, you need not specify the type at all: all type binding gets done at runtime.

A Dynamic Typing Example

All this sounds more complicated than it is. Here's an example. Consider the following three business logic classes, each with its own set of methods.

class ProductBL { public void ProcessNewProductData() { Console.WriteLine("Process method of the ProductBL " + "class has been called."); } } class OrderBL { public void ProcessNewOrderData() { Console.WriteLine("Process method of the OrderBL " + "class has been called."); } } class CustomerBL { public void ProcessNewCustomerData() { Console.WriteLine("Process method of the CustomerBL " + "class has been called."); } }

As you can see, each class has distinct methods. Now, suppose you need to invoke the "process" method on instances of any of these classes on the fly. This type of problem is well-suited to dynamic typing. First, create an enum that contains a list of the business logic instance types.

public enum BusinessLogicObjectType { ProductBL, CustomerBL, OrderBL };

The following method returns an instance of one of the business logic classes:

public static object GetBusinesLogicInstance( BusinessLogicObjectType businessLogicObjectType) { switch (businessLogicObjectType) { case BusinessLogicObjectType.ProductBL: return new ProductBL(); case BusinessLogicObjectType.CustomerBL: return new CustomerBL(); default: return null; } }

You can now use the dynamic keyword to invoke the appropriate business logic instance on the fly, as shown below:

Figure 2. Sample Output: Here's the output from dynamically invoking one of the business object class methods.

static void Main(string[] args) { dynamic dynamicBLObject = GetBusinesLogicInstance( BusinessLogicObjectType.ProductBL); dynamicBLObject.ProcessNewProductData(); Console.Read(); }

That's it! As shown in Figure 2, executing the application displays the message "Process method of the ProductBL class has been called." You can see the complete sample code in Listing 1.

Thanks for your registration, follow us on our social networks to keep up-to-date