locked
Regarding special character in C# class property RRS feed

  • Question

  • User-1961814522 posted

    Hi,

    I have a class like below

    public class Root
            {                      

                public string content {get; set;}

                public string recipient {get; set;}

                 public string sender { get; set; }

                 public string type { get; set; }
             }

    When i assign values to class property the type string property gets "@" special character prefixed and becomes @type.

    Can someone suggest how this can be fixed.

    Thanks,

    Ashvin

    Tuesday, October 6, 2020 9:24 PM

All replies

  • User475983607 posted

    Your question is not clear.  Can you show code that illustrates this problem.

    Tuesday, October 6, 2020 9:57 PM
  • User-1961814522 posted

    Hi mgebhard,

    I initialize the below class with property values

                 Root smsmodel = new Root ();
                 smsModel.content = "This is sent from latest C# code";
                 smsModel.recipient = "RShankar";
                 smsModel.sender = "Ashvin";
                 smsModel.type = "transactional";


    When i look at the class after it is initialized, I see @ prefixed before type property as below

    I have to remove the @ character before the type property, I cannot rename the type property

    testimage

    Thanks,

    Ashvin

    Wednesday, October 7, 2020 4:29 AM
  • User1535942433 posted

    Hi ashvinvee,

    As far as I think,You can't. It is a reserved keyword.Contrast to "contextual keywords" which usually means "we added this later, so we needed it to work in some pre-existing scenarios".So,I suggest you don't use 'type'.

    Best regards,

    Yijing Sun

    Wednesday, October 7, 2020 6:09 AM
  • User475983607 posted

    "Type" is a class in C#.  The "@" distinguishes between the type and the identifier "type".    

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/

    For the second time, can you share code that illustrates the problem you are facing using type?  Include any errors.

    Wednesday, October 7, 2020 11:30 AM
  • User-474980206 posted

    in C# the "@" prefix for an identifier is used if the identifier is a reserved word.

    public foo
    {
        public string class {get; set;}   // syntax error

    public foo
    {
        public string @class {get; set;}   // valid
        public string @bar {get; set;}     // valid but "@" not required

    for some reason, the debugger believes type is a keyword.

    Wednesday, October 7, 2020 10:23 PM