通用数组中的索引器
你好 我创建了一个通用数组,它适用于 Int、String、Float 甚至我自己的名为 Customers 的类型。
通用数组具有 Add()、Sort()、ShowAll() 函数,这些函数适用于 Int、String 甚至 Customer 类型 除非我尝试为 CustomerType 使用 showAll() 方法,该方法显示我通过 ADD() 方法添加的所有值。
输出是这样的 GenericArray.Customer
不是我想要的值。
我已经通过
public class GArray
但现在我无法创建 Int,Float 类型的通用数组。
这是类的 ADD 和 ShowAll 方法,
public void Add(T temp)
{
if (index >= values.Length)
{
T[] tempArray = new T[values.Length + 1];
Array.Copy(values, tempArray, values.Length);
values = tempArray;
}
values[index] = temp;
index++;
}
public void ShowAll()
{
for (int i = 0; i < values.Length; i++)
{
Console.WriteLine(values[i]);
}
}
我添加的值
static void Main(string[] args)
{
GArray<Customer> customers = new GArray<Customer>(3);
customers.Add(new Customer(101, "xyz"));
customers.Add(new Customer(59, "abc"));
customers.ShowAll();
}
我已经和我的朋友谈过了,他说我必须自己创建索引器。有人可以帮助我如何在这种情况下创建索引器,该索引器适用于 customerType 或任何类型。
Hi
i have created a Generic Array that works fine for Int,String, Float or even my Own type named Customers.
Generic Array has functions Add(), Sort(), ShowAll() thats working fine for Int, String, and even Customer Type
except when i try to showAll() method for CustomerType that shows all the values that i have added through ADD() method.
output is something likeGenericArray.Customer
not the values where as i wanted to have the values .
i have solved it through
public class GArray<T> where T : Customer
but now i cant create Generic Array of type Int,Float .
here is the ADD and ShowAll method of Class
public void Add(T temp)
{
if (index >= values.Length)
{
T[] tempArray = new T[values.Length + 1];
Array.Copy(values, tempArray, values.Length);
values = tempArray;
}
values[index] = temp;
index++;
}
public void ShowAll()
{
for (int i = 0; i < values.Length; i++)
{
Console.WriteLine(values[i]);
}
}
the values m adding
static void Main(string[] args)
{
GArray<Customer> customers = new GArray<Customer>(3);
customers.Add(new Customer(101, "xyz"));
customers.Add(new Customer(59, "abc"));
customers.ShowAll();
}
i have talked with my frnd and he said that i have to create indexer my self . can some one help me how can i create indexer in this case that works fine for customerType or any Type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为,如果我理解这个问题(输出类似于 GenericArray.Customer,而不是我想要的值),您应该在客户定义中添加:
我解释一下:当您使用 < code>Console.WriteLine(values[i]) 你告诉 C# 写入控制台 Customer 对象...然后它会写出类的名称,因为这是默认行为。
在 Customer 类中定义要转换的默认字符串,以便您可以...
I think,If I understand the question (output is something like GenericArray.Customer, not the values where as i wanted to have the values) you should add in Customer definition:
I explain: when you use
Console.WriteLine(values[i])
you tell C# to write to console Customer object... and it writes out then name of the class, as it's the default behaviour.Defining in Customer class the default string to be converted to makes what you please...
我认为你的问题是你没有在你的客户类中重写 ToString 。这样做——它将定义对象如何在控制台中显示。
I think your problem is that you have not overridden ToString in your customer class. Do that -- it will define how the objects should be displayed in the console.
暂时先把你的实际问题放在一边,我想提一下,在数组实现中没有
ShowAll
方法的位置。为什么数组应该与控制台应用程序绑定?难道您不想有一天将它重新用于 Windows 窗体应用程序而不需要重写它吗?接下来,.NET 已经有了一个 List,它可以根据需要进行动态分配。如果你确实想自己再写一次,至少要以更大的步长分配数组(每次 n*2)。
要从数组(不属于它的位置)中删除
ShowAll
方法,您应该考虑采用以下方法之一:a) 创建适用于任何
IEnumerable的扩展方法;
(列表、数组、集合等):用法:
b) 或者,更加抽象并创建一个
ForEach
扩展方法,您将在其中传递任意delegate< /code> 执行实际工作:
用法:
Your actual problem aside for a moment, I would like to mention that there is no place for a
ShowAll
method in an array implementation. Why should an array be tied to a console application? Wouldn't you want to reuse it for a Windows Forms application oneday without the need to rewrite it?Next, .NET already has a
List<T>
which does dynamic allocation as necessary. If you do want to write it again yourself, at least allocate the array in bigger steps (n*2 each time).To remove the
ShowAll
method from the array (where it doesn't belong), you should consider taking one of the following approaches:a) Create an extension method which works for any
IEnumerable<T>
(a List, Array, Collection, whatever):Usage:
b) Or, be even more abstract and create a
ForEach
extension method where you will pass an arbitrarydelegate
to perform actual work:Usage: