Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

class being a reserved keyword, how to create a property named class inside a class

My API Response json has below fields when I paste JSON as classes it created the one of the field as _class as class is a reserved keyword

  • Actual

public class Imp
{

    public string Name { get; set; }
    **public string _class { get; set; }**
    public Address address { get; set; }
    public decimal highest { get; set; }
}

but my requirement is to have field as class only, Could anyone suggest how to achieve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • Desired

public class Imp
{

    public string Name { get; set; }
    public string class { get; set; } 
    public Address address { get; set; }
    public decimal highest { get; set; }
}

>Solution :

public string @class {get;set;}

However, convention would be to call it Class instead, in which case: no problem in the first place. If serialization is the problem, most serializers have attributes to help with that:

[JsonProperty("class")]
public string Class {get;set;}

(or whatever your serializer uses)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading