Skip to main content

Posts

Showing posts with the label Generics

C# 2.0 - Generics

Este é um assunto já bastante discutido pela internet fora. Todos sabemos o que são Generics , e para que são usados, certo? Não. Existe uma variedade de razões pelas quais o leitor pode não saber o que são G enerics, e algumas delas são: O leitor é novo na linguagem, O leitor não é novo na linguagem, mas nunca se deu ao trabalho de tentar aprender. Se o leitor nunca aprendeu generics, isso   não significa que nunca terá de aprender. Se reconhece este snippet, então saiba que já estamos a lidar com generics . Portanto, qual é a vantagem de utilizar generics ? Type safety Generics permite-nos apanhar erros na compilação   em vez de em  runtime,  obrigando o programador a respeitar as regras de type safety que fazem do C# uma linguagem estática. Isto leva-nos a uma produtividade acelerada porque não temos que correr o programa para saber que cometemos um erro. Por exemplo, se no snippet acima tentássemos adicionar um novo item do tipo  string, o comp...

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...

The evolution of C# Part II - Generics

This is a well discussed matter throughout the internet. Everyone knows about generics, what they do and what they are used for, right? No. There is a variety of reasons why you wouldn't know about generics, and some of them are: You are new in the language You are not new in the language but never cared to learn it Simply because you never learned the basics about generics, it doesn't mean you've never used it. If you recognize this snippet, then you are already dealing with generics: So, what is the advantage of generics? Type safety Generics will allow you to catch the errors at compile time rather than runtime by enforcing the rules of type safety that make C# a statically typed language. This leads to increased productivity because you don't have to run the code to know that something is wrong. For example, if in the snippet above you tried to add a new item of type string, a compiler exception would be thrown. Typically, in C# 1.0, you would use an...