Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.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# - Fatal编程技术网

C# 如何为对象/类设置默认返回的类属性?

C# 如何为对象/类设置默认返回的类属性?,c#,C#,我正在尝试实现我自己的列表类。只是为了培训,为了更好地理解事情是如何运作的 我有点像: public class TestClass { public int[] List; // Add(), ... } int[] list = new int[10]; 我希望能够像这样检索数组(它是TestClass的属性): var testClass = new TestClass(); int[] list = testClass; // without the ".List"

我正在尝试实现我自己的列表类。只是为了培训,为了更好地理解事情是如何运作的

我有点像:

public class TestClass {
    public int[] List;
    // Add(), ...
}
int[] list = new int[10];
我希望能够像这样检索数组(它是
TestClass
的属性):

var testClass = new TestClass();
int[] list = testClass; // without the ".List" list would point to the array
var testClass = new TestClass();
int[] list = testClass.List;
var list = new TestClass();
foreach(var oneElement in list.List)
不是这样的:

var testClass = new TestClass();
int[] list = testClass; // without the ".List" list would point to the array
var testClass = new TestClass();
int[] list = testClass.List;
var list = new TestClass();
foreach(var oneElement in list.List)
与c#内置泛型列表类的使用方式相同

我如何才能做到这一点(如果这是可能的话)

更新

我将“列表”更改为
int[]
,希望这会有所帮助

我知道我可以这样做:

public class TestClass {
    public int[] List;
    // Add(), ...
}
int[] list = new int[10];
但是我需要
TestClass
,因为我需要关于数组的一些其他(扩展)属性和更多的自定义方法

更新2

也许这会让事情更清楚一些

我试图找出泛型
List
类在这种情况下的工作原理:

var list = new List<T>();
foreach(var oneElement in list)

我希望能够以与
.NET
C
列表
类检索其“底层数组”相同的方式检索我的数组

如果允许
var list
具有类型
IEnumerable
ICollection
IList
,则只需实现接口
IEnumerable
ICollection
IList

公共类TestClass:IList
{
公共SomeTypeOrgeneric[]列表;
// ...
//IList成员
}

免责声明

我添加这一点只是因为OP需要它,但我认为在这种(或大多数其他)情况下,它不是合适的解决方案


我希望能够得到如下列表:

var testClass = new TestClass();
int[] list = testClass; // without the ".List" list would point to the array
var testClass = new TestClass();
int[] list = testClass.List;
var list = new TestClass();
foreach(var oneElement in list.List)
一个问题是您使用的是
var
。编译器将为
list
使用最合理的类型,即
TestClass
,因为这就是
TestClass
的类型

如果希望
list
为列表类型,可以添加隐式强制转换运算符

public class TestClass<T> {
    public T[] List ;
    // ...

    public static implicit operator List<T>(TestClass<T> t)
    {
       return t.List.ToList();
    }
}

还要注意,“列表”是只读的。您正在检索内部列表的副本。我怀疑您是想实现
List
实现的接口(和所有方法)方法,而不是将基础集合作为副本公开。

听起来您可能想创建一个索引器。您可以按如下方式进行操作:

public class TestClass
{
    public object this[int i]
    {
        get
        {
            // put your code here to return object i from the list
        }
        set
        {
            // put your code here to set object i in the list.
            // A variable named "value" contains the incoming value.
        }
    }
}
var testClass = new TestClass();
testClass[0] = 100; // any object really
var item = testClass[0];
您现在可以访问列表中的项目,如下所示:

public class TestClass
{
    public object this[int i]
    {
        get
        {
            // put your code here to return object i from the list
        }
        set
        {
            // put your code here to set object i in the list.
            // A variable named "value" contains the incoming value.
        }
    }
}
var testClass = new TestClass();
testClass[0] = 100; // any object really
var item = testClass[0];

您可能还想看看泛型类,它们在创建自定义容器时很有用。

要在foreach语句中使用您的类,您应该实现
IEnumerable
。这相当直截了当:

public class TestClass : IEnumerable<int>
{
    private static readonly int[] Empty = new int[0];

    public int[] List;

    public IEnumerator<int> GetEnumerator() 
    {
        int[] array = List == null ? Empty : List;
        return array.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerator.GetEnumerator()
    {
        return GetEnumerator();
    }
}

如果您想要其他类似列表的功能,您可能应该实现
IList
。还有一个基类,
System.Collections.ObjectModel.Collection
,它为您实现了所有这些。它具有可重写的方法,因此在添加或删除项时可以更新类中的其他状态<代码>列表没有可重写的方法,因此它作为基类的使用是有限的。

您的
列表
字段是一个数组(即固定大小的集合),而不是列表(即可变大小的集合)。“c#内置通用列表类的t已经完成”如何完成?是否要将您的类型隐式转换为
SomeTypeOrGenericT[]
?@DStanley。这可能吗?我想检索数组。语句“就像内置的泛型列表类一样”没有多大意义,因为
list
类从不允许您访问底层数组。您正在尝试实现
列表的哪些功能?索引器?在foreach语句中使用?您希望编译器如何知道您是要分配类还是底层数组?语法是相同的,因此我希望您不能完全按照要求执行此操作。
IList
接口的哪一部分使这成为可能?@JoSmo不是部分。继承。有了接口,我就能做到这一点吗<代码>var testClass=新testClass();var list=testClass
获取数组?@JoSmo应该是什么类型的
列表
?您可以将数组分配给上述任何类型的变量。@JoSmo对于更新1,这绝对是不可能的。您可以重写
隐式
运算符,但它将返回原始数组的副本。至于更新2,您可以从
IEnumerable
继承。我想要检索整个数组,而不仅仅是一个元素。也许这可以用
公共对象来完成这个{
?我从来没有使用过这个语法,只是询问。并学习新的东西。@JoSmo在这个例子中,testClass本身将表示容器(它实际上不是一个数组,但其行为类似)我只是在演示如何访问该容器中的特定项。如果您想要容器本身的副本,也可以说
var container=testClass
。@Downvoter:感谢您在没有解释的情况下通过downvote进行驱动,特别是因为自我发布答案以来,该问题已被显著编辑。请在中查看更新2我的问题。我希望这将有助于澄清问题。