LA.NET [EN]

March, 2011Archive

Mar 30

So, you know everything about text, right?– part II

Posted in Basics, C#, CLR       Comments Off on So, you know everything about text, right?– part II

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 […]

Read the rest of this entry »

Mar 28

So, you know everything about text, right?– part I

Posted in Basics, C#, CLR       Comments Off on So, you know everything about text, right?– part I

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 […]

Read the rest of this entry »

Mar 26

Last considerations about generics

Posted in Basics, C#, CLR       Comments Off on Last considerations about generics

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> { […]

Read the rest of this entry »

Mar 23

Generic and constructor constraints

Posted in Basics, C#, CLR       Comments Off on Generic and constructor constraints

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 […]

Read the rest of this entry »

Mar 23

Generics and secondary constraints

Posted in Basics, C#, CLR       Comments Off on Generics and secondary constraints

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 […]

Read the rest of this entry »

Mar 21

Generics and primary constraints

Posted in Basics, C#, CLR       Comments Off on Generics and primary constraints

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 { […]

Read the rest of this entry »

Mar 21

Generics: getting started with constraints

Posted in Basics, C#, CLR       Comments Off on Generics: getting started with constraints

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 […]

Read the rest of this entry »

Mar 21

Blog problems

Posted in Trivia       Comments Off on 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 […]

Read the rest of this entry »

Mar 20

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 […]

Read the rest of this entry »

Mar 20

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 […]

Read the rest of this entry »

Mar 19

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 […]

Read the rest of this entry »

Mar 19

Generics, methods and type inference

Posted in Uncategorized       Comments Off on 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 […]

Read the rest of this entry »

Mar 19

Generic interfaces and delegates

Posted in Basics, C#, CLR       Comments Off on Generic interfaces and delegates

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 […]

Read the rest of this entry »

Mar 14

Generics: open vs closed types

Posted in Basics, C#, CLR       Comments Off on Generics: open vs closed types

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 […]

Read the rest of this entry »

Mar 14

Getting started with generics

Posted in Basics, C#, CLR       Comments Off on Getting started with generics

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 […]

Read the rest of this entry »