Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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# 如何返回interface Infocard的实例?_C#_Interface - Fatal编程技术网

C# 如何返回interface Infocard的实例?

C# 如何返回interface Infocard的实例?,c#,interface,C#,Interface,我目前正在为我的计算机科学课程做一项作业,但是我遇到了一个阻碍我进一步发展的障碍。我正在尝试返回信息卡界面,我不确定如何返回 public interface IInfoCardFactory { IInfoCard CreateNewInfoCard(string category); IInfoCard CreateInfoCard(string initialDetails); string[] CategoriesSupported

我目前正在为我的计算机科学课程做一项作业,但是我遇到了一个阻碍我进一步发展的障碍。我正在尝试返回信息卡界面,我不确定如何返回

public interface IInfoCardFactory
    {
        IInfoCard CreateNewInfoCard(string category);
        IInfoCard CreateInfoCard(string initialDetails);
        string[] CategoriesSupported { get; }
        string GetDescription(string category);
    }


public IInfoCard CreateNewInfoCard(string category)
        {

            ......
        }

希望下面的代码会有用

public IInfoCard CreateNewInfoCard(string category)
{
    return new InfoCard();
}

public interface IInfoCard
{
    int foo { get; set; }
}

public interface IInfoCardFactory : IInfoCard
{
    IInfoCard CreateNewInfoCard(string category);
    IInfoCard CreateInfoCard(string initialDetails);
    string[] CategoriesSupported { get; }
    string GetDescription(string category);
}

public class InfoCard : IInfoCardFactory
{
    public IInfoCard CreateNewInfoCard(string category)
    {
        throw new NotImplementedException();
    }

    public IInfoCard CreateInfoCard(string initialDetails)
    {
        throw new NotImplementedException();
    }

    public string[] CategoriesSupported
    {
        get { throw new NotImplementedException(); }
    }

    public string GetDescription(string category)
    {
        throw new NotImplementedException();
    }

    public int foo
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
}

您可以使用接口作为函数返回类型,但大多数情况下都返回类的实现,接口返回类型的优点是您可以返回接口的任何实现 例如:

您可以像这样使用该接口作为函数返回类型:

public ISampleInterface MyFunction(bool t)
{
   return t : new ImplementationClass1() ? ImplementationClass2();
}

该代码意味着您可以根据自己的选择返回接口的不同实现

您的问题不明确,请详细说明……您选择一个实现并将其发送回:)
public ISampleInterface MyFunction(bool t)
{
   return t : new ImplementationClass1() ? ImplementationClass2();
}