简体   繁体   中英

Is there any way to Write same property name in partial class in C#?

My Auto Generated Class

public partial class Address
{
    public int Id { get; set; }

    public string Name { get; set; }
}

And I want following output. And I don't take same property name in my partial class. so how can i achieve my result? is there any other way.

public partial class Address

{
   public string Name  => Name + " ";
}

This isn't possible and really doesn't make any sense. A partial class is just a way to split up a single class. In your example, this is equivalent to having

public partial class Address
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Name  => Name + " ";
}  

Which is obviously not going to compile. Even if it did, the fact that Name is trying to access itself is going to immediately cause issues.

According to your comment, you just need to add a space to the end of the Name property. Since that class is auto-generated, you'll need to add a new property with a different name, such as:

public partial class Address
{
   public string NamePlusSpace  => Name + " ";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM