Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you apply a attribute to multiple fields in C#?

This doesn't seem possible, but I'll ask anyway... Is it possible in C# to apply a single attribute to multiple fields at once?

public class MyClass {
     [SomeAttribute]
     public int m_nVar1;
     [SomeAttribute]
     public int m_nVar2;
     public int m_nVar3;
}

Is there a short-hand method to put the "SomeAttribute" on m_Var1 & m_Var2, but not on m_nVar3? Currently, we are placing the attributes before each field, but it would be nice to put all the fields using a attribute inside a block.

like image 225
Richard Lyle Avatar asked May 23 '12 18:05

Richard Lyle


1 Answers

Yes, it's possible:

[SomeAttribute]
public int m_nVar1, m_nVar2;

(but obviously only if the types are the same)

like image 139
Roman Starkov Avatar answered Oct 14 '22 13:10

Roman Starkov