Sunday, 15 July 2012

c# - Indexer with default parameters -


I know that this is not a real world issue, but I wonder: do you have all the default parameters without paradigm Can use an indexer with / tricks?

How to call for example:

  public int [string value = "", int sth = 5] {get {return 0; } Set {}}   

Without clearly providing the parameters?

I feel like myobject [] , but this is clearly wrong.

Why is the compiler warned about making no sense to me?

Why does the compiler warn about making no sense to me?

Because it really makes sense, not just in C # For example, a default property indexed in COM with the not argument is easily supported. And C # is a language that lets you implement the com server is very easy to implement, just implement the [ComVisible (true)] feature is very popular using the COM server in C #, the Office Interop general example is .

This friction exists because there is an important problem with such properties syntax is Chinese lot sweet, but like any sugar it can produce rotten teeth. A crappy syntax is ambiguity when you use such a property, say, a scripting language:

  New Foo obj = nothing as dim obj   

What was the intention here? What should be assigned ? Or should the default property be assigned? Both are completely legitimate, the compiler can not know who is the best. For Visual Basic Languages ​​(VBSSPT, VBA, VB6), this ambiguity was addressed by adding additional keywords in the language. You want to use the set when you mean object, or nothing to assign to the default property. This many ambitious programmers have got their start in the script, the notion of "objects" is not very clear just for them. And of course very easy to do Oh, this is a major bug generator.

This problem has had a very harmful effect on the intertop core, in the COM properties, two senders, proputed and proprietary reef will look back at the IDispatch :: Invoke () method. .NET languages ​​have eliminated it, at least not because CLI does not allow this, they insist that a default property should have any argument now it is clear for the compiler, It can tell that object assignment intentions have occurred since the index RR is not used.

It was difficult to understand the C # team that it should be changed back, they absolutely hate ambiguity, as well as they should have urged them to basically only one indexed The property can be and this property should be and the only argument will be there. Finally, they bowed down to version 4, programmers who wrote office interop codes for many reformers. But it is only necessary, in fine interop scenarios, but still not allowed in the C # code. You must call get_Item () hard work or of course, just do not write code like this ...

No comments:

Post a Comment