Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

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:

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:

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:

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted

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:

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:

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:

class Customer { /* Details Omitted */ }

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

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
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.

  • Leaders
Posted
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.
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...