简体   繁体   English

协方差泛型C#

[英]Covariance Generics c#

This is an example taken from MSDN , for covariance in generics in C#. 这是来自MSDN的示例,用于C#中泛型的协方差。

I am unable to print FullName, can I know why the output is not printing? 我无法打印FullName,我可以知道为什么不打印输出吗?

// Simple hierarchy of classes.  
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Employee : Person { }

public class Print
{
    // The method has a parameter of the IEnumerable<Person> type.  
    public static void PrintFullName(IEnumerable<Person> persons)
    {
        foreach (Person person in persons)
        {
            Console.WriteLine("Name: {0} {1}",
            person.FirstName, person.LastName);
        }
    }
}
class Program
{
    public static void Main()
    {
        IEnumerable<Person> employees = new List<Person>();
        Person person = new Person();
        person.FirstName = "nuli";
        person.LastName = "swathi";
        Print.PrintFullName(employees);
        Console.ReadKey();
    }
}

Because your employees list is empty. 因为您的employees列表为空。

You should add your Person instance to the employees list, then you'll see it printed. 您应该将Person实例添加到employees列表,然后将其打印出来。

ie

class Program
{
    public static void Main()
    {
        IList<Person> employees = new List<Person>(); //you need to declare this as a List/IList to get the `Add` method
        Person person = new Person();
        person.FirstName = "nuli";
        person.LastName = "swathi";

        //add this line
        employees.Add(person);

        Print.PrintFullName(employees);
        Console.ReadKey();
    }
}

Is it possible you forgot the string.format? 您是否有可能忘记了string.format? Try to use this: 尝试使用此:

Console.WriteLine( String.format("Name: {0} {1}", person.FirstName, person.LastName))

instead of this: 代替这个:

Console.WriteLine("Name: {0} {1}", person.FirstName, person.LastName);

or even better this: 甚至更好:

Console.WriteLine($"Name: {person.FirstName} {person.LastName}");

ref: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated 参考: https : //docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/tokens/interpolated

EDIT: Since its a mdn example its more likely the list is empty.. 编辑:由于其mdn示例其更有可能的列表为空。

Just use a List, List implements IEnumerable. 仅使用List,List实现IEnumerable。 There were a few mistakes within your Program class, You can find the corrected code below: 您的Program类中存在一些错误,您可以在下面找到更正的代码:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Employee : Person { }

class Print
{
    // The method has a parameter of the IEnumerable<Person> type.  
    public static void PrintFullName(IEnumerable<Person> persons)
    {
        foreach (Person person in persons) {
            Console.WriteLine(string.Format("Name: {0} {1}", person.FirstName, person.LastName)); // needs to format the string for it to work, hence string.format().....
        }
    }
}
public class Program
{
    public static void Main()
    {
        List<Person> employees = new List<Person>(); // no need to convert it to an IEnumerable object as List already implements IEnumerable<>
        Person person = new Person();
        person.FirstName = "nuli";
        person.LastName = "swathi";
        employees.Add(person);  // You have to populate the list first
        Print.PrintFullName(employees);   
        Console.ReadLine();     // Changed from Console.ReadKey()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM