Click here to Skip to main content
15,922,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what's wrong with named object ?
[no name] 16-Nov-14 10:15am    
Nothing is wrong with named objects. Anonymous types just gives you some more degree of freedom.
Kornfeld Eliyahu Peter 16-Nov-14 10:25am    
Start reading here:

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();
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.

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
And then discard it, it's pointless to build a new "real" class.
Share this answer
musharaf.ali 16-Nov-14 10:30am    
Then how to access method without naming object?
OriginalGriff 16-Nov-14 10:38am    
If you use var types, then the system sorts that all out for you - because it "knows" what type the variable is, and so what properties and methods it has.
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:[^],[^].

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

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900