skip to Main Content

I’m trying to consume a Web API. I created a class so that I can deserialize the data I get back. The problem is that the returned object has these two properties:

public string id { get; set; }
public string set_id { get; set; }

The set_id related to "sets". The compiler is throwing an error on the set; from the id property, saying that it already contains a definition for set_id.

CS0102 The type ‘MyClass’ already contains a definition for ‘set_id’

Is there any way to solve this without renaming the property?

2

Answers


  1. I would recommend using JsonPropertyName

    For example:

    using System.Text.Json.Serialization;
    
        namespace ConsoleApp1
        {
            public class Class1
            {
                public string id { get; set; }
                [JsonPropertyName("set_id")]
                public string setId { get; set; }
            }
        }
    

    this attribute based on your json library , if you use newtonsoft it would be JsonProperty


    Lets findout why we can’t have a 2 variable with names id and set_id together

    For understanding this we are going to see IL generated code by c# compiler

    for class with 1 property id generated IL looks like this :

    .class public auto ansi beforefieldinit ConsoleApp1.Test
        extends [System.Runtime]System.Object
    {
        .custom instance void System.Runtime.CompilerServices.NullableContextAttribute::.ctor(uint8) = (
            01 00 01 00 00
        )
        .custom instance void System.Runtime.CompilerServices.NullableAttribute::.ctor(uint8) = (
            01 00 00 00 00
        )
        // Fields
        .field private string '<id>k__BackingField'
        .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
            01 00 00 00
        )
        .custom instance void [System.Runtime]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [System.Runtime]System.Diagnostics.DebuggerBrowsableState) = (
            01 00 00 00 00 00 00 00
        )
    
        // Methods
        .method public hidebysig specialname 
            instance string **get_id** () cil managed 
        {
            .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
                01 00 00 00
            )
            // Method begins at RVA 0x20b8
            // Header size: 1
            // Code size: 7 (0x7)
            .maxstack 8
    
            IL_0000: ldarg.0
            IL_0001: ldfld string ConsoleApp1.Test::'<id>k__BackingField'
            IL_0006: ret
        } // end of method Test::get_id
    
        .method public hidebysig specialname 
            instance void set_id (
                string 'value'
            ) cil managed 
        {
            .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
                01 00 00 00
            )
            // Method begins at RVA 0x20c0
            // Header size: 1
            // Code size: 8 (0x8)
            .maxstack 8
    
            IL_0000: ldarg.0
            IL_0001: ldarg.1
            IL_0002: stfld string ConsoleApp1.Test::'<id>k__BackingField'
            IL_0007: ret
        } // end of method Test::set_id
    
        .method public hidebysig specialname rtspecialname 
            instance void .ctor () cil managed 
        {
            // Method begins at RVA 0x20c9
            // Header size: 1
            // Code size: 8 (0x8)
            .maxstack 8
    
            IL_0000: ldarg.0
            IL_0001: call instance void [System.Runtime]System.Object::.ctor()
            IL_0006: nop
            IL_0007: ret
        } // end of method Test::.ctor
    
        // Properties
        .property instance string id()
        {
            .get instance string ConsoleApp1.Test::get_id()
            .set instance void ConsoleApp1.Test::set_id(string)
        }
    
    } // end of class ConsoleApp1.Test
    

    In this code you can see getter and setter method are transformed to some 2 methods named set_id and get_id (set_id = set , get_id = get)

    Now we have set_id and get_id reserved so we can’t have other field with name set_id or get_id

    Login or Signup to reply.
  2. The code generated by C# compiler creates setters and getters.
    set_id will be created for id property you have.
    I would simply rename your set_id to setId or IdOfSet …etc.

    Generally, for property names in C#, it is not a good idea to have underscores if you want to follow the accepted code style patterns.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search