Default value on value-types

I have implemented a value type that use a string value as value container.

I need to initialize this string with a "" value insead of null.

Is it possible to explicitly initialize a value type Is there a way to run a default parameterless constructor

Thanks,

Felice R.



Answer this question

Default value on value-types

  • shrek2006

    Hi Sergey,

    Don't You Know If is it possible to override a constructor

    The Matthew answer is good, but is inefficient because for each property access I run the check "is null".

    Initializing value-type on something like a "constructor" I set the default value one time!

    Any hints

    Thank You.

    Felice


  • helpstring


    public struct MyStruct
    {
    public readonly static MyStruct Empty = new MyStruct();

    private int _int = -1; // Default -1
    private DateTime _date = DateTime.Now; // Default DateTime.Now
    }



    This should do the trick.


  • jam123jam123

    You need to accept that you can't do this. You can't define your own parameterless constructor, and you can't set initial values for fields.

    Do you have very strong reasons for wanting this to be a value type, by the way

    Jon


  • Mario Aoun

    use an "Empty " pattern:

    public struct MyStruct
    {
    string _s;
    int _i;

    public string S
    {
    get { return _s; }
    set { _s = value; }
    }

    public int I
    {
    get { return _i; }
    set { _i = value; }
    }

    public static MyStruct Empty
    {
    get
    {
    MyStruct result = new MyStruct();
    result._s = "";
    result._i = 0;
    return result;
    }
    }

    }

    public class Test
    {
    MyStruct _test = MyStruct.Empty;
    }



  • WimB

    Meybe this is a good read:
    http://www.dotnetconsult.co.uk/weblog/commentview.aspx/03805a0c-525f-4b7d-b0a0-f5f2773b4a7c


  • Michiel Wories - MSFT

    Hi Jon,

    I added the parameterless constructor but I got the following compiler error:

    CS0568 - Struct cannot contains explicit parameterless constructors

    Any hints

    Thanks.

    Felice


  • Pinakin

    Hi PJ,

    If You try assigning a value to an instance variable, You get the following:

    CS0573 cannot have instance field initializers in structs

    Any hints

    Felice


  • Barnabas

    You cannot overcome that directly. But perhaps an indirect approach will work:

    (1) Only allow access to the string field through a public property.
    (2) In that property, have code something like this:

    string MyProperty
    {
    get
    {
    if (this.myString == null)
    this.myString = new string;
    return this.myString;
    }
    }


  • Smacksnr

    Yup, that will work - so long as you (i.e. the original poster) are aware that if you create a new array of this type, then the string value will be null for each of the elements.

    Jon


  • rscp

    Hi!

    You can't use fields initializers on structs. Compiler won't pass it.

    2Felice: you should do as Matthew says (property and getter method) or you should convert struct into class or you may create a default structure instance and copy it to all new structures when you creating them.



  • mallio

    Hi Felice,

    Value types have no default constructor. This is sad, but true.

    Property will make performance hit, this is true. But in most cases this is not visible. Still this is most easiest way for your case.

    If your struct have other parameters, then you can make constructor with that parameters or at least with the string parameter.

    struct MyStruct

    {

    public string Text;

    public MyStruct(string text)

    {

    Text = text;

    }

    }

    But in that case you still can't get rid of default constructor. So the only way to make this string empty - use "new MyStruct("")" and never use "new MyStruct()".

    BTW, why you can't use classes



  • Joshua Morgan

    Sergey wrote:

    Value types have no default constructor.

    That's not true. All value types have a default constructor, and it's always provided for you and can't be overridden.

    From the C# spec:

    "All value types implicitly declare a public parameterless instance constructor called the default constructor."

    and

    "
    Because every value type implicitly has a public parameterless instance constructor, it is not possible for a struct type to contain an explicit declaration of a parameterless constructor. 2A struct type is however permitted to declare parameterized instance constructors"

    Jon


  • Charley Vasconcelos

    There is always a parameterless constructor for value types, and it only ever sets the fields to the default values for the type - you can't specify instance variable initializers within value types.

    The reason for this (I believe) is efficiency - if you create an array of a million instances of this value type, with the current rules the runtime can just allocate and zero the appropriate amount of memory, without running any more code.

    Jon


  • suomi7

    Yes, Jon, you right. I misspeak myself . They are generated by compiler and always exist, but they can't be made by hands.

  • Default value on value-types