简体   繁体   English

通用类型方法

[英]Generic Type Methods

What are some practical cases of why I would need something like this: 为什么我需要这样的东西有哪些实际案例:

  public void Show<FirstType, SecondType>(FirstType first, SecondType second)
  {
      //Do Something
  }

And not just this: 而不仅仅是这个:

  public void Show(FirstType first, SecondType second)
  {
     //Do Something
  }

Thanks so much 非常感谢

This example comes from the framework, but the LINQ extension methods are implemented this way. 此示例来自框架,但LINQ扩展方法以这种方式实现。 For example, the signature for .Where is: 例如,.Where的签名是:

IEnumerable.Where<TSource> (IEnumerable<TSource>, Func<TSource, Boolean>);

This doesn't have two type arguments like your example, but shows a good use case for having one. 这没有像您的示例那样的两个类型参数,但显示了一个很好的用例。 The "Where" is not type specific, and because the predicate given later uses the same type argument you get type safety when performing comparisons. “Where”不是特定于类型的,并且因为稍后给出的谓词使用相同的类型参数,所以在执行比较时会获得类型安全性。

This far preferable to a .Where for every possible type, hence the use of generics. 这远远优于a。对于每种可能的类型,因此使用泛型。

Because you'll need to create another method for something like this, which might be duplication in some cases. 因为你需要为这样的东西创建另一种方法,在某些情况下可能会重复。 To avoid duplicating logic, you use generics when the types have some behavior in common. 为避免重复逻辑,当类型有一些共同的行为时,您使用泛型。 Also, it's typesafe so no need to box/unbox when you use generics. 此外,它是类型安全的,因此当您使用泛型时不需要装箱/取消装箱。

public void Show(ThirdType third, FourthType fourth)
  {
     //Do Something
  }

I can give you a simple use of generics, for example, the method that compares two variables of the same type and returns true or false depending on the equality: 我可以简单地使用泛型,例如,比较相同类型的两个变量并根据相等性返回true或false的方法:

public static bool Equals<T>(T Variable1, T Variable2)
{
    return Variable1.Equals(Variable2);
}

This method is now reusable for any parameter type. 此方法现在可以重用于任何参数类型。 T stands for type and is usually written this way, but not necessarily. T代表类型,通常以这种方式编写,但不一定。 Using this class you can compare equality of any type you define in your main method without boxing/unboxing types inside the method. 使用此类,您可以比较在main方法中定义的任何类型的相等性,而无需在方法内部装箱/取消装箱类型。 Also your equality compare method is safe from wrong type input once you use it in your main method. 另外,在main方法中使用它时,您的等式比较方法对于错误的类型输入是安全的。 Example of using this method would be something like this: 使用此方法的示例如下所示:

public static void Main(string[] args)
{
    if (VarCompare<int>(10, 10))
    {
        Console.WriteLine("Inputs are equal.");
    }
    else
    {
        Console.WriteLine("Inputs aren't equal.");
    }
}

By simply changing your if condition to VarCompare<string>("A", "B") you could compare two string types instead of integers. 通过简单地将if条件更改为VarCompare<string>("A", "B")您可以比较两种字符串类型而不是整数。

A good example of when you would want two generic parameters with different types is Dictionary<TKey, TValue> 当你想要两个具有不同类型的通用参数时,一个很好的例子是Dictionary<TKey, TValue>

http://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx

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

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