Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What's the difference between applying [filed: Nonserializable()] and [Nonserializable] when being applied to an object property? I've been reading and can't find out what's the difference if there is one?

Can someone explain to me why the first attribute has the keyword filed and the method call to Nonserializable() while the second doesn't use the filed keyword and doesn't have a method call but uses the class name Nonserializable if that is a class????
Posted
Comments
Sergey Alexandrovich Kryukov 8-Jul-12 15:07pm    
Could you possibly be a little more accurate, and read help before you ask a question?
If you don't care about your spelling, you cause us to spend extra time on understanding your question and to do guesswork. Nobody wants it, and you want it less than all other people.
--SA

You do not say where you get your information and samples from, but this MSDN entry[^] is reasonably clear about the usage.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jul-12 15:05pm    
This time, this is not exactly related. OP is confused (and even unable to spell it correctly) with the keyword used to disambiguate the target of the attribute application.

I explained it in my answer, please see.
--SA
Richard MacCutchan 9-Jul-12 4:07am    
I think it is related; OP is confused about serialization and I provided a link to MSDN to help clarify. I also asked where he got his original information from.
Sergey Alexandrovich Kryukov 11-Jul-12 16:03pm    
Well, I have to agree now; in this sense, it is related, so, on a second though, I voted 5.
Anyway, first thing to do is to help OP with a big confusion about the attribute usage, that's why I tried to explain it in my answer.
--SA
Somebody tried to fool you. There is no such attribute target as "filed". It will cause the warning: "'filed' is not a recognized attribute location. All attributes in this block will be ignored.".

There is no such keyword. There is a keyword "field". Can you see that this is an absolutely different word with absolutely different meaning (a field is a member of a type)? And I don't know such attribute "Nonserializable", more likely, you mean NonSerializedAttribute.

As to the round brackets, they are allowed but absolutely redundant. This is not exactly a normal method call, but this is the way to pass positional parameter to an instance of the attribute class. The parameters are the parameters of the attribute class constructor, as simple as that. For a parameterless constructor, no brackets are needed at all. (And the named arguments are created as the public propertied of an attribute class.)

In certain cases, you need a keyword to disambiguate the target of the attribute application. This is the case where such keyword is needed: "assembly", "field", "parameter", etc. Please see:
http://msdn.microsoft.com/en-us/library/b3787ac0%28v=vs.90%29.aspx[^].

In the case of [NonSerialized], such qualification is redundant.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900