【问题标题】:C# conditional multiple type checking with one out variableC# 使用一个输出变量进行条件多类型检查
【发布时间】:2021-10-21 02:52:25
【问题描述】:

我打赌在互联网上的某个地方必须有一些答案,但我在过去 30 分钟内没有找到它,我不知道如何正确地问这个问题。

我想将一些输入与多个对象类型进行比较,并将其放在同一个变量中,无论它是哪个对象。

if(input is typeOne || input is typeTwo)

在 C# 中,您可以像这样在 if 语句中初始化变量:

if(input is typeOne inputeTypeOne){
  //i can use inputTypeOne here now
}

是否有可能做以下事情:

if(input is typeOne OR typeTwo inputType){
  // use inputType no matter if typeOne or typeTwo
}

【问题讨论】:

  • 如果typeOnetypeTwo 有共同的接口,那么为什么他们没有正式实现这个接口呢?所以你可以使用if(input is CommonInterface inputeType)
  • TypeOneTypeTwo 有什么共同点?如果答案是“什么都没有”,那么您不能将它们放在同一个变量中(好吧,无论如何都没有任何用处)。
  • @Jamiec 好吧.. 我想你可以把它们放在object ;)
  • @CaiusJard 在这一点上你不能用它做任何有用的事情(因此括号中的部分)
  • @Jamiec 也有例外,但如果您已经知道如何使用object,我希望您已经知道答案。

标签: c# conditional-statements


【解决方案1】:

您可以创建一种在两个类之间通用的代理接口:

namespace ConsoleApp
{
internal class Program
{
        private static void Main(string[] args)
        {
            TypeOne one = new TypeOne();
            TypeTwo two = new TypeTwo();
            var t = one.GetType();
            if (one is IProxyInterface)
            {
                Console.WriteLine("Use me");
            }
        }
    }
    internal class TypeOne : IProxyInterface
    { }
    internal class TypeTwo : IProxyInterface
    { }
    public interface IProxyInterface { }
}

【讨论】:

    【解决方案2】:

    不,这是不可能的。
    因为 C# 是类型安全的。
    请考虑以下示例。

    if(input is string OR int inputType)
    { 
       
    }
    

    inputType的类型是什么?
    我可以在inputType 上使用Contains 函数吗? 如果它是string,那将是可能的,但如果它是int,它应该会给出一个编译错误。
    唯一的方法是检查公共接口/父级。 像这样:

    if (input is IParrent parrent)
    {        
    }
    
    internal class TypeOne : IParrent
    { }
    
    internal class TypeTwo : IParrent
    { }
    
    public interface IParrent{ }
    

    【讨论】:

      【解决方案3】:

      是否有可能做以下事情:

         if(input is typeOne OR typeTwo inputType){
            // use inputType no matter if typeOne or typeTwo
         }
      

      是的,但前提是 typeOne 或 typeTwo 派生自相同的父级或实现相同的接口

      class Triangle: Shape ...
      class Square: Shape ...
      class Circle: Shape ...
      
      if(someObject is Shape s)
         ...
      
      

      但请务必注意,您只能将形状视为它们的共同父级s。除非您知道 s 中的实例是三角形,否则您无法访问特定于三角形的某些属性

      var someObject = new Square();
      if(someObject is Shape s) //yes
        Console.WriteLine("IsScalene: "+ s.IsScalene); //no
      

      即使你施放它也不行;仅仅因为它是一个形状,并不意味着它是一个三角形

        Console.WriteLine("IsScalene: "+ (s as Triangle).IsScalene); //no
      

      您需要确保 IF 块中的任何代码都想使用 s,确保它使用了 Shape 上可用的东西,或者它在执行之前适当地检查并转换为 Square/Triangle它

      if(someObject is Shape s) //yes
      
        Console.Write(s.NumberOfSides); //all shapes have a number of sides; the property is defined at Shape level
        ...
        if(s is Triangle t) //it isn't a triangle
          ...
        else if(s is Square q)
          ...
      

      【讨论】:

        猜你喜欢
        • 2019-09-25
        • 2020-12-20
        • 1970-01-01
        • 2013-06-11
        • 2021-07-25
        • 1970-01-01
        • 2022-10-05
        • 1970-01-01
        • 2014-03-05
        相关资源
        最近更新 更多