Some new C# Generics features

Nerseus

Danner
Joined
Oct 22, 2002
Messages
2,547
Location
Arizona, USA
Not much is said about the new ?? operator (maybe it's not an operator - who knows?) but it's there in C#. It's meant to work as a coallesce on nullable types, which can be declared with the new ? syntax.

For example:
C#:
int? x = null;
// Same as
Nullable<int> y = null;

The new syntax of "int?" declares a generic Nullable type, but easier. Most people know about this. What I just learned is that to return a "default" value of a null int, you can use the new ?? syntax. For example:
C#:
int? x = null;
int y = x ?? 0;

This says, if x is not null, assign the value to y. If it is null, use the righthand side, or 0 in this case. You can use any expression in place of 0, including a property, method call, etc. - including cascading ?? operations. For example:
C#:
int? x = null;
Nullable<int> y = null;
int z = x ?? y ?? 0; // Sets z to 0

y = 5;
int z = x ?? y ?? 0; // Sets z to 5

-nerseus
 
Also, you can take advantage of the "using" statement to create collection "classes". For example, you can use a generic List<> to create a collection of Customers. You can do this as easily as:
C#:
class Customer { /* Details Omitted */ }

// This is in a separate file from class Customer
class Test
{
    private List<Customer> customerList;
}

If you have this a lot, you may want to create a class like so:
C#:
class Customer { /* Details Omitted */ }

// This is in a separate file from class Customer
class CustomerList : List<Customer> { /* No code - simple class */ }

// This is in a separate file from class Customer
class Test
{
    private CustomerList customerList;
}

But you can also use the "using" statement to create a fake class named CustomerList. For example:
C#:
class Customer { /* Details Omitted */ }

// This is in a separate file from class Customer
using CustomerList=List<Customer>
class Test
{
    private CustomerList customerList;
}

-ner
 
nerseus said:
But you can also use the "using" statement to create a fake class named CustomerList.
That looks sort of like a typdef statement in C++.

If you were to use the using statement to create a CustomerList type and you later decided you needed a CustomerList Class with more/different functionality than a standard List<>, would swapping the type out be trivial in your Test Class? If so, the using statement sounds like a brilliant idea and a good convention to follow. You would only need to make one (delete using statement) or two (possibly include CustomerLists's namespace) changes to your Test Class file and then everything should continue to work, no problem. Very pragmatic and flexible.
 
Yes, swapping the class name in a using statement could essentially transform your CustomerList from one class to another. It is kind of like an intentionally handicapped macro so you don't shoot yourself in the foot (maybe the term typedef is more accurate, I don't know what the word means in terms of C++). It simply defines an alias for another class. Combined with generics it becomes particularly handy.
 
Back
Top