*Experts* Nerseus Posted March 30, 2006 *Experts* Posted March 30, 2006 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 Quote "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* Nerseus Posted April 4, 2006 Author *Experts* Posted April 4, 2006 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 Quote "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
Administrators PlausiblyDamp Posted April 5, 2006 Administrators Posted April 5, 2006 Also you can use inheritance to do the same thing class Customer { /* Details Omitted */ } class CustomerList : List { } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mskeel Posted April 5, 2006 Posted April 5, 2006 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. Quote
Leaders snarfblam Posted April 5, 2006 Leaders Posted April 5, 2006 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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.