Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中设置默认数组值#_C#_Arrays - Fatal编程技术网

C# 如何在c中设置默认数组值#

C# 如何在c中设置默认数组值#,c#,arrays,C#,Arrays,我有一个包含一些值和一个数组的类,我需要这个数组中的元素具有默认值 像 我需要将RelationList设置为一些默认值。有人知道怎么做吗?谢谢。设置这些默认值是构造函数的工作: class SomeKindOfObject { public string OName { get; set; } public string OType { get; set; } public string Data { get; set; } Object[] Relation

我有一个包含一些值和一个数组的类,我需要这个数组中的元素具有默认值


我需要将RelationList设置为一些默认值。有人知道怎么做吗?谢谢。

设置这些默认值是构造函数的工作:

class SomeKindOfObject
{
    public string OName { get; set; }
    public string OType { get; set; }
    public string Data { get; set; }

    Object[] RelationList = new Object[5];

    // Constructor
    public SomeKindOfObject()
    {
        RelationList[0] = blabla;
    }
}
或者,如果已经放置了对象,也可以使用数组初始值设定项:

Object[] RelationList = new Object[] { blahblah, blahblah, blahblah, ect };

设置这些默认值是构造函数的工作:

class SomeKindOfObject
{
    public string OName { get; set; }
    public string OType { get; set; }
    public string Data { get; set; }

    Object[] RelationList = new Object[5];

    // Constructor
    public SomeKindOfObject()
    {
        RelationList[0] = blabla;
    }
}
或者,如果已经放置了对象,也可以使用数组初始值设定项:

Object[] RelationList = new Object[] { blahblah, blahblah, blahblah, ect };

你必须反复浏览它们

例如

class-Foo
{
私有只读对象[]_relationList=新对象[5];
公共Foo(对象默认值)
{
对于(变量i=0;i<\u relationList.Length;++i){
_relationList[i]=默认值;
}
}
}

您必须对它们进行迭代

例如

class-Foo
{
私有只读对象[]_relationList=新对象[5];
公共Foo(对象默认值)
{
对于(变量i=0;i<\u relationList.Length;++i){
_relationList[i]=默认值;
}
}
}

在构造函数中初始化属性:

class Class1
{
    public string OName { get; set;}
    public string OType { get; set; }
    public string Data { get; set; }
    public object RelationList { get; set; }

    public Class1()
    {
      // initialize your properties here
      RelationList = new object[5];
      RelationList[0] = blabla;
      RelationList[1] = blabla;
    }
}

在构造函数中初始化属性:

class Class1
{
    public string OName { get; set;}
    public string OType { get; set; }
    public string Data { get; set; }
    public object RelationList { get; set; }

    public Class1()
    {
      // initialize your properties here
      RelationList = new object[5];
      RelationList[0] = blabla;
      RelationList[1] = blabla;
    }
}

可以在对象的构造函数中设置默认值

class MyObject
{
   public string OName { get; set; }
   public string OType { get; set; }
   public string Data { get; set; }
   public Object[] RelationList = new Object[5];

   public MyObject()
   {
      RelationList[0] = 1;
      RelationList[1] = 2;
   }
}

根据具体情况,还有其他机制可以设置这些默认值(例如,在构造函数中而不是在声明中创建新数组,将它们传递给构造函数等)。

可以在对象的构造函数中设置默认值

class MyObject
{
   public string OName { get; set; }
   public string OType { get; set; }
   public string Data { get; set; }
   public Object[] RelationList = new Object[5];

   public MyObject()
   {
      RelationList[0] = 1;
      RelationList[1] = 2;
   }
}

根据具体情况,还有其他机制可以设置这些默认值(例如,在构造函数中而不是在声明中创建新数组,将它们传递给构造函数等)。

您可以使用对象初始值设定器

Object[] RelationList = new[] { 
     new Object { OName = "abc", OType = "xyz", Data = "tetst" }, 
     new Object { OName = "abc", OType = "xyz", Data = "tetst" },
     new Object { OName = "abc", OType = "xyz", Data = "tetst" },
     new Object { OName = "abc", OType = "xyz", Data = "tetst" }
 };

您可以使用对象初始值设定项

Object[] RelationList = new[] { 
     new Object { OName = "abc", OType = "xyz", Data = "tetst" }, 
     new Object { OName = "abc", OType = "xyz", Data = "tetst" },
     new Object { OName = "abc", OType = "xyz", Data = "tetst" },
     new Object { OName = "abc", OType = "xyz", Data = "tetst" }
 };

我想,
对象
可能在某个地方有另一种含义。。。你可能想重新考虑这个类名。谢谢你的建议。我重新命名了它。我想,
对象
可能在某个地方有另一种含义。。。你可能想重新考虑这个类名。谢谢你的建议。我重新命名了它。