Showing posts with label 2010. Show all posts
Showing posts with label 2010. Show all posts

Dynamic import | Get Ready For C# 4.0

Posted by Tarek N. Elsamni | Posted in , , , , , , , | Posted on 12:33

0

Many COM methods accept and return “variant” types, which are represented in the PIAs as object. In the vast majority of cases, a programmer calling these methods already knows the static type of a returned object from context, but explicitly has to perform a cast on the returned value to make use of that knowledge. These casts are so common that they constitute a major nuisance.
In order to facilitate a smoother experience, if you choose to import these COM APIs with PIA-embedding, variants are instead represented using the type dynamic. In other words, from your point of view, COM signatures now have occurrences of dynamic instead of object in them.
This means that you can easily access members directly off a returned object, or you can assign it to a strongly typed local variable without having to cast. To illustrate, you can now say


instead of


and


instead of

Optional (or Default) Parameters | Get Ready For C# 4.0

Posted by Tarek N. Elsamni | Posted in , , , , , , | Posted on 01:19

0

I see that people have been asking for this feature since C# 1.0. Three versions later, it’s finally here.

Now you can assign a default value to a parameter right within the method declaration. The user of the method can either pass a value or simply skip the argument. In the latter case, the default value is passed to the method.

Method declaration:



Method calls:

Dynamic Keyword | Get Ready For C# 4.0

Posted by Tarek N. Elsamni | Posted in , , , , , , | Posted on 01:14

0

The dynamic keyword is a key feature of this release. It closes the gap between dynamic and statically-typed languages. Now you can create dynamic objects and let their types be determined at run time. With the addition of the System.Dynamic namespace, you can create expandable objects and advanced class wrappers, and you can provide interoperability between different languages, including dynamic ones. Here is one quick example:



More Complicated Example: