通用数组中的索引器

发布于 2024-11-07 03:54:40 字数 1381 浏览 4 评论 0原文

你好 我创建了一个通用数组,它适用于 Int、String、Float 甚至我自己的名为 Customers 的类型。

通用数组具有 Add()、Sort()、ShowAll() 函数,这些函数适用于 Int、String 甚至 Customer 类型 除非我尝试为 CustomerType 使用 showAll() 方法,该方法显示我通过 ADD() 方法添加的所有值。

输出是这样的 GenericArray.Customer

不是我想要的值。

我已经通过

public class GArray解决了这个问题其中 T : Customer

但现在我无法创建 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 like
GenericArray.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最美不过初阳 2024-11-14 03:54:40

我认为,如果我理解这个问题(输出类似于 GenericArray.Customer,而不是我想要的值),您应该在客户定义中添加:

public override string ToString()
{
    // return something you want to show to identify your customer
    // e.g. return Name;  
    return ...           
}

我解释一下:当您使用 < 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:

public override string ToString()
{
    // return something you want to show to identify your customer
    // e.g. return Name;  
    return ...           
}

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...

我们只是彼此的过ke 2024-11-14 03:54:40
public T this[int index]
{
  get {return values[index]; }
}
public T this[int index]
{
  get {return values[index]; }
}
余厌 2024-11-14 03:54:40

我认为你的问题是你没有在你的客户类中重写 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.

信愁 2024-11-14 03:54:40

暂时先把你的实际问题放在一边,我想提一下,在数组实现中没有 ShowAll 方法的位置。为什么数组应该与控制台应用程序绑定?难道您不想有一天将它重新用于 Windows 窗体应用程序而不需要重写它吗?

接下来,.NET 已经有了一个 List,它可以根据需要进行动态分配。如果你确实想自己再写一次,至少要以更大的步长分配数组(每次 n*2)。

要从数组(不属于它的位置)中删除 ShowAll 方法,您应该考虑采用以下方法之一:

a) 创建适用于任何 IEnumerable的扩展方法;(列表、数组、集合等):

 public static class EnumExt
{
     public static void ShowAll<T>(this IEnumerable<T> list)
     {
         foreach (T item in list)
            Console.WriteLine(item);
     }
}

用法:

int[] array = new int[] { 1,2,3};
array.ShowAll();

b) 或者,更加抽象并创建一个 ForEach 扩展方法,您将在其中传递任意 delegate< /code> 执行实际工作:

public static class EnumExt
{
     public static void ForEach<T>(this IEnumerable<T> list, Action<T> action)
     {
         foreach (T item in list)
            action(item);
     }
}

用法:

int[] array = new int[] { 1,2,3};
// now you are reusing the iterator
// for any action you want to execute
array.ForEach(Console.WriteLine);
// or
array.ForEach(item => Console.WriteLine("My item is: " + item));

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):

 public static class EnumExt
{
     public static void ShowAll<T>(this IEnumerable<T> list)
     {
         foreach (T item in list)
            Console.WriteLine(item);
     }
}

Usage:

int[] array = new int[] { 1,2,3};
array.ShowAll();

b) Or, be even more abstract and create a ForEach extension method where you will pass an arbitrary delegate to perform actual work:

public static class EnumExt
{
     public static void ForEach<T>(this IEnumerable<T> list, Action<T> action)
     {
         foreach (T item in list)
            action(item);
     }
}

Usage:

int[] array = new int[] { 1,2,3};
// now you are reusing the iterator
// for any action you want to execute
array.ForEach(Console.WriteLine);
// or
array.ForEach(item => Console.WriteLine("My item is: " + item));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文