Jump to content

Check out our Community Blogs

- - - - -

C# features part 1: C# 2.0 features

  Posted by Tonchi, 10 March 2013 · 1695 views

Posted Image

C# features from 2.0 to 5.0 version

“Generics are the most powerful feature of C# 2.0. Generics allow you to define type-safe data structures, without committing to actual data types. This results in a significant performance boost and higher quality code, because you get to reuse data processing algorithms without duplicating type-specific code. In concept, generics are similar to C++ templates, but are drastically different in implementation and capabilities. This article discusses the problem space generics address, how they are implemented, the benefits of the programming model, and unique innovations, such as constrains, generic methods and delegates, and generic inheritance. You will also see how generics are utilized in other areas of the .NET Framework such as reflection, arrays, collections, serialization, and remoting, and how to improve on the basic offering.” by MSDN
Generics are very easy feature to learn and it is inevitably to know. It is very easy to learn the basics of C# language like: iterations, classes, structures, methods, properties,… but you will have to learn other parts of the language if you want to say that you really know C#.
So let’s start with basic examples. The most basic example of generic class is List. I believe that everyone has used List at least once. If no, let’s start with the example.
 List<type> _list = new List<type>();
What we have done here? Nothing special. We have just declared a new List and we named it _list. This is type safe code because it is generic. That’s the basic meaning of generics in C#.
List is one of the many collections in C#/.NET so we have to declare a type of that List. Let’s say it’s a string type. That means that we would insert unkown numbers of string in our List. We are declaring that by <> brackets.
How we can declare our own generic class? This is the basic syntax:
 public class OurClass<T>
Where T is a data type we would use in the future code. Also it is important to know that we can declare more than one argument in the generics.
For more about generics look at MSDN.
Anonymous methods are used when we want to pass a code block as a delegate parameter. It is also a very simple to learn.
Here is the basic example of anonymous method:
 button1.Click += delegate(Object sender, EventArgs e)
 //code that is activated when button1 is clicked
Ofc, there is a way to write a similar code without using delegate keyword (using Lamda Expression) but that is not for this post.
From this example you can write any event handler you want (and that is the part of your code).
Be aware that if you want to use code from the example, you will have to write it inside the main part of the code (main part of the code differs from the templates: WinForms, WPF,…), if you want to avoid errors, because IntelliSense will not recognize your object where you want to join the event of that object.
For more information about Anonymous methods look at MSDN.
Nullable types are most easiest feature of C# 2.0 to learn. We know that Int32 type holds the range of integer values that we can assign to it from -2147483648 to 2147483647. But what if we want to declare a new integer (Int32 type in this case) and we want of it to be empty but also we want of it to be initialize. In that case we are using “null” value which is not representing zero value.
It is a good practice to do that, so I recommend you to do that in your code in the future.
For more information about the nullable types look at MSDN.
http://feeds.wordpre...rdpress.com/77/ Posted Image


  • 1

Recent Entries

Recent Comments

0 user(s) viewing

0 members, 0 guests, 0 anonymous users

Search My Blog