开发者

Can you omit the parenthesis from attributes with no params?

开发者 https://www.devze.com 2022-12-22 19:38 出处:网络
I noticed when I have [Serializable] instead of [Serializable()开发者_如何学运维], the code still compiles.

I noticed when I have [Serializable] instead of [Serializable()开发者_如何学运维], the code still compiles.

Is there a rule for this that you can omit the parentheses? Is it a good practice? It seems more readable to me unless I am missing something.


There's no particular rule regarding best practices that I've seen anywehere on the subect.

Generally, I find that people who hand-type their attributes omit the empty parens, but code generation almost always explicitly includes them.


You can miss off the () when the attribute constructor you want to use has no parameters, and you don't want to specify any optional parameters (property values).

See here for more details


Yes omitting the attribute is legal from a language standpoint. I don't know of any best practices surrounding it but in general I find developers omit the parens if they're not necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号