Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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# 如何在公共接口中使用带有主接口的接口?_C#_.net_Interface - Fatal编程技术网

C# 如何在公共接口中使用带有主接口的接口?

C# 如何在公共接口中使用带有主接口的接口?,c#,.net,interface,C#,.net,Interface,所以我只想向TestVal传递一个字符串,进行验证并调用TestvalRes来返回它是否有效,如果无效,为什么?因此验证将在第一个公共接口TestVal中完成,但是我仍然需要在Main()中调用它,对吗?首先,我建议遵循C#命名约定,分别命名接口ITestVal和ITestValRes 其次,静态方法不能调用同一类中的实例方法(不创建实例并使用实例)。您需要创建类的实例,并将应用程序流的控制权传递给: //Program.cs public interface TestVal { //I

所以我只想向TestVal传递一个字符串,进行验证并调用TestvalRes来返回它是否有效,如果无效,为什么?因此验证将在第一个公共接口TestVal中完成,但是我仍然需要在Main()中调用它,对吗?

首先,我建议遵循C#命名约定,分别命名接口
ITestVal
ITestValRes

其次,静态方法不能调用同一类中的实例方法(不创建实例并使用实例)。您需要创建类的实例,并将应用程序流的控制权传递给:

//Program.cs
public interface TestVal
{
    //Input Param 
    string Input { get; }

    //will return output
    TestValRes ValidateRe(string input);
}



class MyClass : ITestVal
{
    static void Main(string[] args)
    {
        var instance = new MyClass();
        instance.Run();
    }

    public void Run()
    {
        ValidateRe("test");
    }

    public ITestValRes ValidateRe(string input)
    {
        return null; // return an instance of a class implementing ITestValRes here.
    }
}


//TestvalRes.cs
public interface TestvalRes
{

    string Input { get; }

    bool IsValid { get; }
}

否则你怎么称呼它?您所做的只是定义任何实现TestVal的东西都将实现一个名为
ValidateRe
的方法。你什么也没做。另外,如果您想遵循正常的C#命名约定,我建议您将接口命名为ITestVal和ITestValRes。很好,非常感谢。我现在的问题是-如何在Main方法中调用TestVal?
class MyClass : ITestVal
{
    static void Main(string[] args)
    {
        var instance = new MyClass();
        instance.Run();
    }

    public void Run()
    {
        ValidateRe("test");
    }

    public ITestValRes ValidateRe(string input)
    {
        return null; // return an instance of a class implementing ITestValRes here.
    }
}