What is the C# Equivalent of java's transient keyword? [duplicate] - c#

This question already has answers here:
How to exclude some members from being serialized to Json?
(3 answers)
Closed 7 years ago.
I'm using System.Web.Script.Serialization.JavaScriptSerializer to serialize an object to JSON, but there are some fields within the object which don't need to be serialized. In java I would mark them with the 'transient' keyword. Is there an equivalent keyword or standard practice in C#?

Put [ScriptIgnore] attribute on the property and it will not be serialized.
See: ScriptIgnoreAttribute Class

Related

JsonConvert - Ignore [PropertyName("FooBar")]? [duplicate]

This question already has answers here:
How to ignore JsonProperty(PropertyName = "someName") when serializing json?
(2 answers)
Closed 5 years ago.
Is there a way to serialize a object ignoring the [PropertyName("FooBar")]?
Details: My object has enormous property names that are read all over the frontend... the problem is that it causes me to waste about 35% of space... when you take caching in consideration, this is a huge problem
You can accomplish this by creating a contract resolver, which inherits from DefaultContractResolver, and using that resolver in the front end where you want to ignore the attributes.

What are [..] called above enum / class definition? [duplicate]

This question already has answers here:
What does the [Flags] Enum Attribute mean in C#?
(14 answers)
What does square bracket [] mean in the below code?
(2 answers)
what is [] brackets in .net? [duplicate]
(7 answers)
Closed 6 years ago.
Sorry for the silly question, but I came across the following C# code and I'm wondering what the [Flags] portion is and what it does.
[Flags]
public enum UserFlags
{
//...
}
Thank you in advance.
It's a class attribute. There are also method attributes for example.
You can even write your own
They are usually used to define meta information or behaviour about a class or method and can be read using reflection.

Json.NET prevent specific type(s) from serializing without attributes [duplicate]

This question already has answers here:
Exclude property from serialization via custom attribute (json.net)
(7 answers)
Closed 9 years ago.
I tried searching google and stackoverflow for this simple question but had no luck.
When using Json.NET I want to serialize an object with all of the fields except a specific type.
I don't want to mark every field/property with JsonIgnore to prevent serialization instead what I want to do is just mark the specific type as "NonSerialized". This of course won't work. Is there anyway to achieve it without marking properties and fields with attributes?
I tried resolving this using DefaultContractResolver but didn't manage to make it work.
You could try to use the JavaScriptSerializer class and implement a JavaScriptConverter which excludes the properties you don't need. You can then register that converter on the JavaScriptSerializer using its RegisterConverters method.

Formatting output of JavaScriptSerializer [duplicate]

This question already has answers here:
How to set formatting with JavaScriptSerializer when JSON serializing?
(4 answers)
Closed 7 years ago.
I am using JavaScriptSerializer for a list - and it works:
string codeString = (new JavaScriptSerializer()).Serialize(ramGraphList);
Although the output is formatted as follows:
[["Time","Value"],["08/07/2012 12:43:28","5270"],["08/07/2012 12:44:32","5277"]];
The problem is that the values "5270" and "5277" are not strings, they should be treated as int's hence they have to be unquoted.
Is there an effective way of achieve this?
Expected output:
[["Time","Value"],["08/07/2012 12:43:28",5270],["08/07/2012 12:44:32",5277]];
I believe that it's not possible to do this with the JavaScriptSerializer class without wraping it or modifying the class. However, have you considered using DataContractJsonSerializer or JSON.Net?

What is Polymorphism? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Try to describe polymorphism as easy as you can
What is polymorphism?
Please read MSDN which covers it in reference to c#,
Basically a derived class inherits from another class it gets all its methods,events and properties, and every type is polymorphic in .NET since they all have Object as their base class.

Categories