C # .Net 4.0 Named and Standard Options

What value do you think the name and default parameters will have in C # .Net 4.0?

What would be useful for them (which has not yet been achieved with overloading and redefinition)?

+3
source share
6 answers

This can make constructors easier, especially for immutable types (which are important for streaming) - see here for a full discussion . Not as good as it should be, but better than having a lot of overload. You obviously cannot use object initializers with immutable objects, so the usual one:

new Foo {Id = 25, Name = "Fred"}

not available; I will agree:

new Foo (Id: 25, Name: "Fred")

, , . , IMO, () .

COM , COM- - .


; , ? - / ( ); :

[XmlElement("foo", Namespace = "bar")]

( ctor, "foo" ) . , :

SomeMethod("foo", SecondArg = "bar");

( , )

... , , SecondArg? SecondArg SomeMethod "bar" SecondArg "bar" .

, # 3.0:

    static void SomeMethod(string x, string y) { }
    static void Main()
    {
        string SecondArg;
        SomeMethod("foo", SecondArg = "bar");
    }

, SecondArg , , varialble ..

.

</" > - 280Z28: , , . , . , , , {} . .

[AttributeUsage(AttributeTargets.Class)]
public sealed class SomeAttribute : Attribute
{
    public SomeAttribute() { }

    public SomeAttribute(int SomeVariable)
    {
        this.SomeVariable = SomeVariable;
    }

    public int SomeVariable
    {
        get;
        set;
    }
}

/* Here the true ambiguity: When you add an attribute, and only in this case
 * there would be no way without a new syntax to use named arguments with attributes.
 * This is a particular problem because attributes are a prime candidate for
 * constructor simplification for immutable data types.
 */

// This calls the constructor with 1 arg
[Some(SomeVariable: 3)]
// This calls the constructor with 0 args, followed by setting a property
[Some(SomeVariable = 3)]
public class SomeClass
{
}

+4

API Office!:)

Office API , , / . , # .

+1

, , .

Exception. " ", " ". , , , , innerException? , innerException, innerException, ?

2 , null innerException , . .

, Exception 4 , .

+1

COM-.

# 4 VB.Net . ref #.

0

The brevity of the code is the obvious that comes to mind. Why define multiple overloads when you can define one function. In addition, if you have two identically typed parameters, it is not always possible to build a complete set of overloads that you may need.

0
source

Also this will not compile:

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
sealed class MyAttribute : Attribute
{
    public MyAttribute(object a = null)
    {

    }
}


class Test
{
    [My] // [My(a: "asd")]
    int prop1 { get; set; }
}

while it does:

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
sealed class MyAttribute : Attribute
{
    public MyAttribute()
    {

    }

    public object a { get; set; }
}

class Test
{
    [My] // [My(a=null)]
    int prop1 { get; set; }
}
0
source

Source: https://habr.com/ru/post/1705417/


All Articles