Is it possible to change the default for a primitive data type?

I recently created a generic Matrix <T> class that acts as a wrapper around a <List <T β†’> collection. As far as I can tell, this class works fine. I ran into a little problem though regarding the defaults for T.

I am instantiating Matrix <int> (3, 3), which creates a 3x3 matrix from ints, all defaults to 0 using the default (T). I know that value types (including primitives) are 0 by default, and reference types are by default equal to zero. I was wondering if this default could be changed so that if the type of the value is passed into a matrix it will be filled with, for example, 5, not 0.

I tried to create my own struct (value type), but due to the fact that I could not use parameterless constructors inside structs, I cannot find a way to change the default value from 0.

I suspect that it is not possible to change the default and I will have to scroll the Matrix cell through the cell after it is created, but I wanted to ask here just in case before I do that.

+2


source to share


6 answers


 public Matrix(int width, int height) : this(width, height, default(T)) {}
 public Matrix(int width, int height, T defaultValue)
 {
     List<T> rows = new List<T>(height);
     for (int i = 0; i < height; i++)
     {
         List<T> columns = new List<T>(width);
         for (int j = 0; j < width; j++)
         { columns.Add(defaultValue); }
         rows.Add(columns);
     }
     // store `rows` wherever you are storing it internally.
 }

      



But, as Joseph says, there is no way to determine what is evaluating default(T)

.

+3


source


There is no way to change the default, for example as you describe.

var someInt = default(int); //this will always be 0, you can't change it
var someReference = default(SomeClass); //this will always be null

      



Here's the msdn article , although it's not much more descriptive than what has already been said unfortunately.

+1


source


You can create a struct that encapsulates its value and exposes it only with the offset of the default value you want:

public struct DefaultFiveInteger {

   private int _value;

   public DefaultFiveInteger(int value) {
      _value = x - 5;
   }

   public static implicit operator int(DefaultFiveInteger x) {
      return x._value + 5;
   }

   public static implicit operator DefaultFiveInteger(int x) {
      return new DefaultFiveInteger(x);
   }

}

      

Now you can declare a variable that is initialized with a default value (0) and it will return a value with an offset:

DefaultFiveInteger x;
Console.Write(x);

      

Output:

5

      

+1


source


Ok, since you're already looking at structs, you can simulate the default like this:

public struct MyInt
{
    private int _defaultInt;
    public int DefaultInt
    {
        get
        {
            if (_defaultInt == 0)
                return 5;
            else
                return _defaultInt;
        }
        set
        {
            _defaultInt = value;
        }
    }
}

      

0


source


My understanding of the implementation default(T)

is that the runtime, by default, dumps memory out of memory when an application requests it, and C # just allocates space without overwriting zeros. It just so happens that the default values ​​for non-numeric types (e.g. null reference, false) are represented as zeros in memory. This can lead to some strange behavior; for example default(MyEnumType)

will be zero even if you've never specified the enum value to be zero.

0


source


By combining the ideas of Guffa and DougJones, you could offset the property support element.

public struct MyInt
{
    private const int INT_DEFAULT = 5;
    private int _defaultInt;
    public int DefaultInt
    {
        get { return _defaultInt + INT_DEFAULT; }
        set { _defaultInt = value - INT_DEFAULT; }
    }
}

      

0


source







All Articles