locked
Set C# default for Custom Type RRS feed

  • Question

  • The C# default keyword brings back the default for a specific type.  e.g. default(decimal).

     

    Can this be used for Custom types. E.g.

    public class Trip {
    
    ...
    
    public Trip SetupStandardTrip(){
    
    ...
    
    }
    
    }
    

     

    var standardTrip = default(Trip); //Invokes SetupStandardTrip() and brings back the default setup for a Trip

     

    Can this be done?

    Tuesday, October 5, 2010 1:45 PM

Answers

  • The default keyword does not invoke any constructors - it basically would set any reference type to null or any value type to 0.  Generaly, it is used in generics programming when you don't know what type T is.  If T is a value type, then setting the variable to null is illegal.


    Tom Shelton
    Tuesday, October 5, 2010 2:07 PM
  • Since "Trip" is a class, you can just make a default constructor for it:

     

    public class Trip
    {
     // Define a default constructor
     public Trip()
     {
      // Set all of your properties and fields...
      this.Destination = "Somewhere";
      this.Person = "Someone";
     }
    }
    

     

    Then, instead of using default(T), all you need to do is construct a Trip, and you'll get your "default" trip:

     

        var standardTrip = new Trip();

     

    Otherwise, using default(Trip) will, as Tom mentioned, always return null since Trip is a class.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    Tuesday, October 5, 2010 3:09 PM
  • Hi SomeDeveloperPerson,

    Welcome to MSDN Forums!

     

    Tom Shelton said right, the default can be using at to place: one is in the switch statement, and the second is in the generic. You also can go to the following article to know more about this key word using in generic.

     

    default Keyword in Generic Code (C# Programming Guide)

    Given a variable t of a parameterized type T, the statement t = null is only valid if T is a reference type and t = 0 will only work for numeric value types but not for structs. The solution is to use the default keyword, which will return null for reference types and zero for numeric value types. For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference types. For nullable value types, default returns a System.Nullable<T>, which is initialized like any struct.

     

    If there’s anything unclear or I have misunderstood you, please feel free to let me know.

     

    Have a nice day!

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

     

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    Thursday, October 7, 2010 12:58 PM

All replies

  • The default keyword does not invoke any constructors - it basically would set any reference type to null or any value type to 0.  Generaly, it is used in generics programming when you don't know what type T is.  If T is a value type, then setting the variable to null is illegal.


    Tom Shelton
    Tuesday, October 5, 2010 2:07 PM
  • Since "Trip" is a class, you can just make a default constructor for it:

     

    public class Trip
    {
     // Define a default constructor
     public Trip()
     {
      // Set all of your properties and fields...
      this.Destination = "Somewhere";
      this.Person = "Someone";
     }
    }
    

     

    Then, instead of using default(T), all you need to do is construct a Trip, and you'll get your "default" trip:

     

        var standardTrip = new Trip();

     

    Otherwise, using default(Trip) will, as Tom mentioned, always return null since Trip is a class.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    Tuesday, October 5, 2010 3:09 PM
  • Hi SomeDeveloperPerson,

    Welcome to MSDN Forums!

     

    Tom Shelton said right, the default can be using at to place: one is in the switch statement, and the second is in the generic. You also can go to the following article to know more about this key word using in generic.

     

    default Keyword in Generic Code (C# Programming Guide)

    Given a variable t of a parameterized type T, the statement t = null is only valid if T is a reference type and t = 0 will only work for numeric value types but not for structs. The solution is to use the default keyword, which will return null for reference types and zero for numeric value types. For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference types. For nullable value types, default returns a System.Nullable<T>, which is initialized like any struct.

     

    If there’s anything unclear or I have misunderstood you, please feel free to let me know.

     

    Have a nice day!

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

     

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    Thursday, October 7, 2010 12:58 PM