Combining multiple attributes in C#

前端 未结 3 2032
旧巷少年郎
旧巷少年郎 2020-12-17 07:32

Is there a functional difference between the following syntax...

[Foo, Bar]
public class Baz {}

...and this syntax?

[Foo]
[         


        
相关标签:
3条回答
  • 2020-12-17 08:10

    I typically stack attributes. But I also mainly use these with WCF where the parameter list can get pretty big.

    [OperationContract()]
    [WebGet(...)]
    string MyMethod(string input);
    
    0 讨论(0)
  • 2020-12-17 08:14

    There is no functional difference. It's a question of convenience and style.

    Most often, I tend to see attributes on their own lines as a way to keep them separate and easy to read. It's also nice to be able to use the line comment // to remove attributes individually.

    [A]
    [B]
    //[C] disabled
    public class Foo {} 
    
    0 讨论(0)
  • 2020-12-17 08:17

    From a readability standpoint separate attributes are preferred.

    Think about the case where you are passing some type of parameter

     [Foo(typeof(int)), Bar(typeof(decimal), MessageTemplate="Bar")]
    

    versus

     [Foo(typeof(int))]
     [Bar(typeof(decimal), MessageTemplate="Bar")]
    

    I would also argue, if you could combine them into one, they should be one tag.

    0 讨论(0)
提交回复
热议问题