开发者

Why can't you call methods with c# object initializer syntax? [closed]

开发者 https://www.devze.com 2023-03-10 10:29 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

Why can't you call methods with c# object initializer syntax?

It seems to me that the property setters are called in the order that they are set in开发者_开发知识库 the syntax, so why not allow calls to methods as well? If there is a good reason, I'm missing it.

EDIT

I realize the semantic differences between methods and properties and the technical similarities. The purpose of this question is to probe for a good technical reason that they did not include the feature.

this. __curious_geek, I hear what you are saying, but I'm sure there are some features they haven't included because it wasn't technically feasable.

That's all I'm after. The overwhelming unwelcoming tone is heard loud and clear. Stackoverflow is no longer a "Question and Answer site" but instead a "Defend your question site".

Edit 2

Sample usage:

var mySuperLongVariableNameThatIDontWantToTypeOverAndOverAgainAndIsntThatTheWholePointAnyway  = new Thingy
    {
        Name = "Marty McFly",
        AddChildren("Biff","Big Bird","Alf"),
        // 1000 other properties and method calls.....
    }


The answer is in the name -- object initializer syntax is syntactic sugar to visually group the object's initial state. Methods change the object state, so once it's changed, it's no longer the initial state.

For example: say you buy a car. It is a red coupe with 55,000 miles on it. Then, you decide to drive it. It ends up with 55,500 miles on it. It has changed from its initial state:

var c = new Car() {Color = "Red",
                   Style = Styles.Coupe,
                   Mileage = 55000};
// c.Mileage is 55,000
c.Drive();
// c.Mileage is 55,500

In this somewhat contrived example, the method has a side effect and thus changes the object from its initial 55,000mi state to a 55,500mi state. This is not the same thing as buying a car with 55,500 miles on it.


If you really want to do this, you could cheat I suppose...

class C {
    int BadProperty {
        set {
            SomeMethod(value);
        }
    }

    void SomeMethod(int value) {
        // here is where you do really, really bad things
    }
}

Then call it like this!

var fail = new C { BadProperty = 1 };


what if the method fails ? The basic idea is, it's just a syntactic sugar. Eric Lippert is many time asked about "Why does C# not support feature X?". His answer is always

"because no one designed, specified, implemented, tested, documented and shipped that feature." - Eric Lippert.


This is all about orders, the Class have to be initialized with all the fields and all declared methods, before it can be guaranteed to run a method safely.


You can call methods with named parameters, if that is what you are asking about:

someMethod(param1: "Hello World", param2: "Some Other Value");
0

精彩评论

暂无评论...
验证码 换一张
取 消