简体   繁体   English

扩展静态方法和非扩展静态方法之间的区别是什么

[英]What is the difference in use between extension static methods and non-extension static method

In class A I have a method: class A我有一个方法:

  public static String GetTimestamp(this DateTime value) {
            return value.ToString("yyyyMMddHHmmssffff");
        }

I call it by: 我叫它:

        String timeStamp = GetTimestamp(new DateTime());

What is the difference in use if that method looked like: 如果该方法看起来有什么区别:

  public static String GetTimestamp( DateTime value) {
                return value.ToString("yyyyMMddHHmmssffff");
            }

The point of an extension method is that you could have called it like: 扩展方法的要点是你可以像以下一样调用它:

String timeStamp = new DateTime().GetTimestamp();

to the same effect. 达到同样的效果。 That's the only change. 这是唯一的变化。

Extension Methods are intended to be used as "if they were" regular instance methods of their target types: 扩展方法旨在用作“如果它们是”其目标类型的常规实例方法:

var timeStamp = new DateTime().GetTimestamp();

other than that, there is no difference. 除此之外,没有区别。


They were introduced in C# 3.0 to create the beautiful syntax we normally see in LINQ today, such as: 它们是在C#3.0中引入的,用于创建我们通常在LINQ中看到的漂亮语法,例如:

  var soldProducts = customers.Where(x => x.IsActive)
                              .SelectMany(x => x.Orders)
                              .Where(x => x.Status == OrderStatus.Completed)
                              .Select(x => x.Product)
                              .ToList;

without extension methods, this syntax would not be possible and would require many regular method calls: 没有扩展方法,这种语法是不可能的,并且需要许多常规方法调用:

var activecustomers = Enumerable.Where(customers, x => x.IsActive);
var orders = Enumerable.SelectMany(activecustomers, x => x.Orders);
var completed = Enumerable.Where(orders, x => x.Status == OrderStatus.Completed);
var products = Enumerable.Select(completed, x => x.Product);
var soldProducts = Enumerable.ToList(products);

Not to mention that type inference is also playing a big part here, since most of these methods in System.Linq.Enumerable are actually generic methods with one or several type parameters, but the C# compiler is smart enough to infer the generic type parameters and remove the need to explicitly specify them. 更不用说type inference也在这里发挥了重要作用,因为System.Linq.Enumerable中的大多数这些方法实际上是具有一个或多个类型参数的泛型方法,但C#编译器足够智能来推断泛型类型参数和删除显式指定它们的需要。

With the extension method version, you can also call it with 使用扩展方法版本,您也可以使用

dateTime.GetTimeStamp()

if you've includeded the namespace it's declared in. 如果你已经包含了它声明的名称空间。

Extension methods allow you to treat them as though they were a method inside of the type in question. 扩展方法允许您将它们视为有问题类型的方法。 So in your first example, that method can be treated as though it is a method inside of DateTime. 因此,在您的第一个示例中,可以将该方法视为DateTime中的方法。 so you could do something like this: 所以你可以这样做:

var foo = new DateTime();
var timestamp = foo.GetTimestamp();

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

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