Generating a Property Stub | Get Ready For C# 4.0

Posted by Tarek N. Elsamni | Posted in | Posted on 02:46

0

If code references an undefined property, click Generate property stub. The property stub is generated in the appropriate class. The property’s return type is determined from the context.

For example, assume that you generate the InstanceProperty property from the statement in the following code.


When you generate the property, the following stub is created in the Customer class.


If a property is invoked on a type and not an instance, the generated stub will be a static property (C#) or a shared property (Visual Basic).

For example, imagine that you generate a property from the following statement (assuming that Customer is a class name).



When you generate the property, the following stub is created in the Customer class.


If a property is invoked without a qualification (referring to a member of the current type), the generated stub is static if the property is called from a static method. Otherwise, it is an instance property.

For example, assume that you generate a property from the following statement.


When you generate the property, the following stub is created in the current class.

Indexed properties | Get Ready For C# 4.0

Posted by Tarek N. Elsamni | Posted in | Posted on 12:35

0

Many COM APIs expose “indexed properties” which are essentially properties with parameters. C# will not allow you to declare indexed properties, but to the extent that non-C# APIs expose them, will now allow you to access these using element access syntax. So instead of



You can now write the more intuitive

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: