声明匿名类型Object有什么好处? [英] what is the advantages of declaring anonymous type Object ?

查看:71
本文介绍了声明匿名类型Object有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命名对象有什么问题?

推荐答案

取决于你在做什么。如果您正在处理可以使用任何类型对象的代码(例如Console.WriteLine),则除了使用 object 声明之外别无选择:

Depends what you are doing. If you are working on code that can take any type of object (such as Console.WriteLine for example) you have no choice except to use object declarations:
public void MyWriteLine(string format, params object[] data)
   {
   ...
      string s = data[i].ToString();
   ...
   }

因为一切都是从对象派生的,所以该类型的变量将接受任何类作为value - 然后继承系统将根据运行时的实际值类型对必要的执行方法进行排序。



如果使用不当,则会出现问题:示例使用 ArrayList 而不是 List< T> ,因为您可以包含其中的任何对象 - 但它因为在使用实际值类型之前必须进行运行时验证和转换,才能让生活变得更加艰难。



如果可以使用命名类型,请执行此操作。当你真的需要时,只能使用对象





思考关于它,如果你在讨论实际的匿名类型而不是对象类,那么这是另一回事 - 大多数时候你使用匿名类型它是因为没有选择:例如Linq没有它们会工作得很好。



再次,它适用于:如果你从一个方法传递一个值,它应该是一个命名类型(或者外面的世界不能轻易使用它)。但是如果你打算暂时使用这个类型:

Because everything is ultimately derived from object a variable of that type will accept any class as a value - and the inheritance system will then sort out the necessary method to execute based on the actual value type at runtime.

It's a problem when you use it inappropriately: for example using an ArrayList instead of a List<T> because you can "include any object in it" - but it makes life harder down the line because you have to do runtime verification and conversion before you can use the actual values type.

When you can use named types, do. Only use object when you really need to.

[edit]
Thinking about it, if you are talking about actual Anonymous types rather than the object class, then that's a different matter - most of the time when you use anonymous types it's because there is no choice: Linq for example wouldn't work very well without them.

Again, it's down to appropriate use: if you are passing a value out of a method, it should be a named type (or the outside world can't easily use use it). But if you are going to use the type temporarily:

var results = listOfMyClass.Where(m => m.FirstName.StartsWith("M")).Select(m => new {Name = m.FirstName + " " + m.LastName, Email = m.Email});
foreach (var x in results)
   {
   // Send email
   }

然后抛弃它,建立一个新的真正的类是没有意义的。

[/ edit]

And then discard it, it's pointless to build a new "real" class.
[/edit]


命名方法有什么问题(你应该说,方法,而不是对象;只有当你从一个方法中创建一个委托实例时它才会成为一个对象,并且即使它使用的方法被命名,这个对象也没有名称) 。



唯一错误的是:你创建一个不需要名字的名字。 :-)

这个想法是:

http:/ /lurkmore.so/images/1/1b/Occam_razor.jpg [ ^ ],

http://en.wikipedia.org / wiki / Occam%27s_razor [ ^ ] 。



通过使用匿名方法,您可以编写更少的代码;更重要的是,您不会编写未使用的代码,并且阻止代码在所需范围之外使用,这是最重要的部分。例如,如果你处理异常,你可能想要阻止以任何其他方式调用处理程序,这非常重要。



另一个例子:你可以在调用方法中隐藏一些特殊方法,具有相同的目的。如果只在一个地方调用一个方法,为什么要将它暴露在其他地方呢?这在我的小文章中有解释:隐藏广告-hoc调用方法体内的方法 [ ^ ]。



如果经常被忽视,这是一个非常重要的用途。请注意,在许多事件的处理程序中,不使用传递给处理程序的两个参数中的一个。你可以隔离处理,摆脱未使用的。例如:

What's wrong with named method (you should have said, "method", not "object"; it becomes an object only when you make a delegate instance out of a method, and this object does not have a "name" even if the method it uses was named).

The only wrong thing is: you create a name where a name is not needed. :-)
The idea is:
http://lurkmore.so/images/1/1b/Occam_razor.jpg[^],
http://en.wikipedia.org/wiki/Occam%27s_razor[^].

By using anonymous method, you write less code; more importantly, you don't write unused code and you prevent code from being used outside of required scope, most important part. For example, if you handle an exception, you may want to prevent the handler to be called in any other way, which can be important enough.

Another example: you can hide some ad-hoc method in a calling method, with the same purpose. If a method is called only in one place, why exposing it anywhere else? This is explained in my small article: Hide Ad-hoc Methods Inside the Calling Method’s Body[^].

One practically important use if often overlooked. Note that in handlers of many events one of two of the arguments passed to the handler are not used. You can isolate the handling, getting rid of the unused. For example:
MyButton.Click += (sender, eventArgs) => {
    // usually, you don't need to use sender; it's a button, so what
    // moreover, this event's eventArgs is not informative; click is just a click,
    // no coordinates or any useful information
    RealClickHandler(); // no redundant information to pass
}

// ...

void RealClickHandler() {
    // no redundant information to take into account
}





-SA


这篇关于声明匿名类型Object有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆