is there a difference between [Serializable] and [Serializable()] in c#?

馋奶兔 提交于 2020-01-21 07:09:05

问题


I've bumped into examples using either of both notations. I can't find anything about it what tells which one is the common one, why 2 notations are allowed, and if there is actually any subtle difference between the two.

anyone an idea?


回答1:


Nope, no functional difference.

Why the 2 different styles, you ask? The first notation is allowed for brevity. The 2nd notation is allowed because some attributes take parameters:

[Category("Foobar related methods.")]
public void Foo()
{
}

Also note that [Serializable] is really just short-hand for [SerializableAttribute()] - C# lets you omit the Attribute suffix as well as the empty constructor parens.




回答2:


No, there is no difference. [Serializable] is just syntactic sugar for [Serializable()] because the C# syntax lets you miss out the constructor brackets if there is a default attribute constructor.

Note that both are really syntactic sugar for [SerializableAttribute()] as attribute declarations also let you miss the Attribute suffix.




回答3:


both uses the default c'tor, there is no difference at all.



来源:https://stackoverflow.com/questions/1770643/is-there-a-difference-between-serializable-and-serializable-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!