Jake
Jake

Reputation: 16837

Named parameters confusion

In the following code: (WebMethod attribute in web service)

[WebMethod(CacheDuration = 300)]
public string GetData(string Id)
{

}

The intellisense for the WebMethod attribute class constructor shows the second overload as named parameters. My doubt is that aren't named parameter values specified using a colon (:) after the parameter name. How come an equal to operator is used here ?

Thanks.

Upvotes: 1

Views: 354

Answers (2)

Manual5355
Manual5355

Reputation: 982

I believe it has to do with WebMethod's properties.

Look at this VALID code:

class TestAttribute : Attribute
{
    public int MyProperty { get; set; }
}
class Program
{
    [Test(MyProperty=300)]
    public void method1()
    {
    }
}

If you look at the Metadata for WebMethod, there is a property called "CacheDuration".

Upvotes: 0

Cameron
Cameron

Reputation: 98846

You're using a third syntax particular to attributes, in which you can specify fields to initialize on the attribute instance using a Name = Value syntax in the constructor call.

[WebMethod(cacheDuration: 300)]

would work just as well, but it's using standard named-parameter syntax instead of attribute field-initialization syntax. Remember that attributes were around before named parameters existed.

Upvotes: 5

Related Questions