In the previous post, we’ve started looking at how to work with text in C# and we’ve run a rather superficial analysis over the Char type. In this post, we’ll start looking at the String type which is probably what you’ll be using most of the time when you need to work with text. What […]
March, 2011Archive
In .NET, characters are always represented by 16 bits Unicode values. Programmatically, they’re represented through instances of the System.Char type. Here’s an example of a char represented in C#: var myChar = 'a'; The Char type offers several helper static methods which do several useful operations. For instance, you can call the IsLetter method to […]
As we’ve seen, constraints will help in writing “type safe generic code” (not sure if this designation really exists. if it doesn’t, then I’ve invented ). But even with generics, there are still a couple of scenarios which might caught you by surprise. Lets start with casts and with a simple example: class Generic<T> { […]
In this post, we’ll talk about one last kind of constraints: the constructor constraint. Whenever you apply a type constraint to a generic type argument, the compiler ensures that it can only be replaced by a concrete type which does expose a public default constructor. Constructor constrains are specified through the new keyword, as you […]
Besides primary constraints, you can also constrain a generic type parameter through secondary constraints. Unlike primary constraints, you can apply zero or more secondary constraints to a generic type argument. Interfaces constraints are the most common kind of secondary constraints you’ll apply in your code. They allow the compiler to ensure that a generic type […]
In the previous post, we’ve started looking at constraints. Primary constraints are one type of constraints. Currently, you can specify zero or one primary constraint when you introduce a generic type argument. Whenever you restrain a generic type argument to a non-sealed class, you’re using a type constraint. Here’s an example: public class Test { […]
Now that we have basic knowledge about generics, we can proceed and talk about constraints. When we write “generic” code, the compiler must ensure that that code will work for any type which might be used in place of the generic type argument. For instance, take a look at the following code: public class Test […]
Blog problems
It seems like I’ve been caught in the middle of some URI confusion regarding my blog. So, it’s possible that I’ve missed your comments or messages you might have sent me in these last days. If that was the case, then please resubmit them again and make sure that the URI is from http://blogs.msmvps.com/luisabreu (notice […]
New PT book project: do you want to help me with my next book project?
If you’ve been reading this blog, you probably know that my latest book on HTML5 has been released. In this last book project, I’ve run a little experiment: I’ve asked for the help of my readers for reviewing the manuscript. There were several guys which answered the call and I liked the way things worked […]
So, where can’t I define generics?
As we’ve seen, we can define generics in interfaces, classes, delegates and methods. In a previous post, I’ve already mentioned that you cannot define generics for properties. Besides properties, you can’t also define generics for indexers (aka parameterful properties), events, operators, constructors and finalizers. Notice that these members can use eventual generic type parameters defined […]
My HTML5 book is out!
I’ve been so busy that I didn’t even noticed that my latest HTML5 book was released a couple of days ago (btw, it seems like the 2nd version of the ASP.NET book is out too with an updated chapter on the new MS JQuery plugins!). If you’re interested, you can get more info at FCA’s […]
Generics, methods and type inference
Today, we’ll keep looking at generics and we’ll see how type inference is used to simplify the code we need to write to invoke a method. Lets start with a simple example: public class Test { public void DoSomething<T>( T item ) { Console.WriteLine(item + " is of type " + typeof(T)); } } And […]
In the previous post, I’ve introduced the concept of generics. In this post, we’ll keep looking at generics and we’ll talk a little bit about how we can create generic interfaces and delegates. Without generic interfaces, manipulating a value type through its interface reference would result in boxing (and less compile type safety). The some […]
Before going on, does anyone know how to improve the performance of Windows Live Writer? It’s painfully slow after I’ve updated it to the latest release and it’s upsetting me a lot. I’ve tried to disable everything, but the damn app doesn’t show any improvements…damn…ok, enough bitching… In the previous post, I’ve introduced generics. In […]
With generics, the CLR offers us another way to ensure code reuse. If you’re a C++ developer, you might be tempted to see generics as some sort of C++ templates. Even though there are certain similarities, the truth is that there are several important differences. For instance, in C++ the source code must be available […]