Skip to main content

Posts

Showing posts from November, 2013

C# 2 - Static Members

In C# 2 there was a new addition in the functionality called static classes and members . When i say "members", i am referring to methods, properties, events and so on. I have a strong opinion about this modifier which i will share with you along the article, but let's start by looking at what "static" means. Take this code as example: Above we can see that we define two properties and one is static. In the main method we then create an instance of Person and set the normal property ( Name ). Now notice the Person.NumberOfBrains  assignment. This is the static property we created in the class. Notice that the access is made at class level , instead of instance level.  This means that there will only be one NumberOfBrains  value throughout all Person instances, but there will be a Name  value per each instance  of Person . Of course, the example is not the best, but you get the idea. Static applies when all  instances share the same value. There are some ru

C# 2.0 - Anonymous Methods

Let's get back to my series on the evolution of the C# language. Everybody is familiar with what a delegate is, right? We know that delegates are a big part of the events system in .Net. We register a method on that delegate ( called handler ) that is invoked when the delegate is invoked. In other words, a delegate simply points to a method. The advantage here is that, as the delegate wraps the method and is an object, we can simply pass it to a method as a parameter! Think about this: This is how we use a delegate in events, for instance. The class exposes the delegate and we register our method to listen it. When the delegate is called inside the class, all the registered methods will be called (because of this, we call it a Multicast Delegate ). This works well, because all you have to do is have a method with the required signature. In the example above, we needed to declare two methods just to make a Console.WriteLine. Isn't this too much? I think it's a bit to

C# 2.0 - Nullable Types

It's 3 am and i don't feel like sleeping. I've been helping a friend to work on his code and now i'm only partially tired. Well, why not continue with my series on the evolution of C#? Let's do it! So, this time i'm going to talk about nullable types . Everybody used them already, but back in .Net 1.0 they didn't exist. How did you represent a "no value present" value? Sure, string can have null, but null is different than empty, right? How do you represent an empty numeric type then? You check if it is bigger than 0? Well for some cases that's just how developers did it. Either that or create a constant value that represents a "no value". But, to me, that is not expressive enough . Basically, a nullable type is only a non-nullable type wrapped in the System.Nullable struct. This is a generic struct, so it makes use of the Generic features of C# 2.0. The struct is very simple, it contains only a HasValue and a Value proper