2

I recently created a generic Matrix<T> class that acts as a wrapper around a List<List<T>> collection. As far as I can tell, this class is working perfectly. I am running into a slight problem though regarding the default values of the T's.

I create an instance of Matrix<int>(3, 3), which creates a 3x3 matrix of ints, all defaulted to 0 using default(T). I know that value types (which include primitives) default to a 0 equivalent, and reference types default to null. I was wondering if it was possible to change this default value so that if a value type is passed into the Matrix, it would be populated with 5's for example, instead of 0's.

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

I suspect changing the default value is not possible, and I will have to loop through the Matrix cell by cell after it has been instantiated, but I wanted to ask on here just in case before I do that.

4

6 回答 6

3
 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's no way of setting what default(T) evaluates to.

于 2009-10-21T23:35:03.977 回答
1

我对实现的理解default(T)是,默认情况下,运行时会在应用程序请求时将内存清零,而 C# 只是分配空间而不会覆盖零。碰巧的是,非数字类型的默认值(例如空引用,false)在内存中被表示为零。这可能会导致一些奇怪的行为;例如,default(MyEnumType)即使您从未将枚举值指定为零,也将为零。

于 2009-10-21T23:50:01.390 回答
1

您可以创建一个结构来封装它的值,并且只使用您想要的默认值的偏移量来公开它:

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);
   }

}

现在您可以声明一个初始化为默认值 (0) 的变量,它将返回带有偏移量的值:

DefaultFiveInteger x;
Console.Write(x);

输出:

5
于 2009-10-21T23:53:20.303 回答
1

There's no way to change the default value like how you're describing.

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 an msdn article on it, although it's not much more descriptive than what's already been said unfortunately.

于 2009-10-21T23:31:24.193 回答
0

结合 Guffa 和 DougJones 的想法,您可以抵消属性支持成员。

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; }
    }
}
于 2011-09-17T17:46:20.587 回答
0

Well since you're looking at structs already, you could simulate a default value as follows:

public struct MyInt
{
    private int _defaultInt;
    public int DefaultInt
    {
        get
        {
            if (_defaultInt == 0)
                return 5;
            else
                return _defaultInt;
        }
        set
        {
            _defaultInt = value;
        }
    }
}
于 2009-10-21T23:40:20.877 回答