Pages

C# Features Through the History - Version 2.0



The 2.0 of the C# language was released in 2005, following the version 2.0 of the .NET Framework, and coming along with a massive amount of interesting changes. These changes were represented in the 3rd version of the ECMA-334.

How to use the examples 

The examples are available here. You can open the solution and play around. Just uncomment the code and have fun! 

Generics 

The concepts of generics was present already in Java. With generics, or parameterized types,  classes, interfaces, structs and methods can be parameterized by the types of data they store and manipulate. Advantages:

  • You have the advantages of maximize reuse, type safety and performance;
  • With generics, you eliminates boxing and unboxing of value types into collections;
  • The type-safety is achieved at compile time. You'll not allowed to add a different type of specified in the instance of the object.
In this example, we created a Stack class that will will handle a list of objects. Look the comments:
  


This is a generic Stack. See the difference in the resulted code:



Partial Types 


The idea of partial types is to write source a code in separate files, compiling in a single unit. There are some situations in which this strategy can be useful:

  • When working in large projects, enable multiple programmers working at the same time;
  • Maybe the grow of a class is so large that keep in a single code is impractical;
  • When the code is generated by a program, instead of a person (e.g. Windows Forms).
In this example, we have three partial interfaces and two classes PartialTest:


The interesting part is that we don't need to implement the interface in both partial classes:


Anonymous Methods 


Anonymous methods work like inline delegates. They can simplify the use of delegates, without the necessity to write a separate method.

Examples of anonymous methods and events:

Nullable types 


Added the support for nullabity across all types, including value types. A nullable type combines a value of the unterlying type with a boolean null indicator. With this characteristics, we have the property HasValue of type bool and Value exposed.



Iterators 

The main idea is to reduce the effort and amount of code used to iterate over a custom collection. The iterator uses a yield return construct, allowing the method maintain it's state during the iteration in a structure that implements IEnumerable interface. 

On each successive iteration of the foreach loop (or the direct call to IEnumerator.MoveNext), the next iterator code body resumes after the previous yield return statement. It then continues to the next yield return statement until the end of the iterator body is reached, or until a yield break statement is encountered.

Some examples of iterators:

In this example, the use of yield break will prevent the yield return to be executed (line 37). Different from the simple break (line 55): 

Covariance and Contravariance 


Covariance and contravariance enable implicit reference conversion for arrays, delegates and generic type arguments. Covariance preserves assignment compatibility whereas contravariance reverses it.




Getters and Setters Accessibility 


In this version it's possible to attribute different accessibility for set and get properties and indexers.

In this code, you can see that, in the line 13, the setter is not accessible because it's protected:

Delegate inference 


It's possible to assign the name of a method to a delegate, without the use of new, or invoke the delegate's constructor.

Static Classes 


Before this version, classes are supposed to have at least a default constructor. Even for classes that would never meant to be instantiated, you need at least a private constructor to prevent instantiation. With this version, it's possible to create static classes, that are not derived from or instantiated, and have no constructor. All members will be static.




----------------------
I hope you have enjoyed the journey until now. Have a nice week!

Here is the list of all articles:



Fabio Ono

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. It's a great explanation about C# principles. I'm looking forward to reading your next post.

    ReplyDelete