Работа интерфейса IEnumerable и ошибка преобразования типов - C#
Формулировка задачи:
у меня есть клас:
заполняю его так:
хочу его виводить вот так:
и видает ошибку:
class DAD2
{
public string name { get; set; }
public int code { get; set; }
public double size { get; set; }
public double price { get; set; }
public DAD2(){}
public DAD2(string name, int code, double size, double price)
{
this.name=name;
this.code=code;
this.size=size;
this.price=price;
}
}DAD2[] p = { new SON2("Арка", 1100, 144.5, 150),new SON2("Стіл", 1100, 77.7, 450),
new SON2("Підлога", 1102, 10, 550),new SON2("Вікно", 1100, 714.9, 350),
new SON2("Атеро", 1100, 74.9, 50)}; foreach(DAD2 p3 in p)
dataGrid3.ItemsSource = p3;Ошибка 1 Не удается неявно преобразовать тип "lab_3.SON2" в "System.Collections.IEnumerable". Существует явное преобразование (возможно, пропущено приведение типов) D:\РОМАН\5-курс\edisk_files\Lab_Kovalchuk\Готові лаби\lab_№3\lab_3\lab_3\Window2.xaml.cs 38 41 lab_3
как мне исправить? я знаю надо переопределить методpublic IEnumerator GetEnumerator() но мне не работает((( помогите плиз...Решение задачи: «Работа интерфейса IEnumerable и ошибка преобразования типов»
textual
Листинг программы
using System;
using System.Collections;
using System.Collections.Generic;
namespace IEnumirableT
{
internal class MyClass : IEnumerable<int>
{
private int[] _x;
public MyClass(int length)
{
_x = new int[length];
}
public IEnumerator<int> GetEnumerator()
{
for (int i = 0; i < _x.Length; i++)
yield return _x[i];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
internal class Program
{
private static void Main()
{
MyClass m = new MyClass(10);
foreach (int i in m)
Console.WriteLine(i);
Console.ReadKey();
}
}
}