Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. { get; set; }

{ get; set; }

Scheduled Pinned Locked Moved C#
question
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xarzu
    wrote on last edited by
    #1

    from time to time, I see some sample code that is handed to me that has a code snippet that looks like this: public string Name { get; set; } Does this actually do anything? Does it really make it so that one can set and get the "Name" string variable? I have always thought that what is required is something like this: private string name; public string Name { get { return this.name; } set { this.name = value; } } Am I correct? Please advise.

    L F S 3 Replies Last reply
    0
    • X Xarzu

      from time to time, I see some sample code that is handed to me that has a code snippet that looks like this: public string Name { get; set; } Does this actually do anything? Does it really make it so that one can set and get the "Name" string variable? I have always thought that what is required is something like this: private string name; public string Name { get { return this.name; } set { this.name = value; } } Am I correct? Please advise.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Yes, they are [Auto-Implemented Properties (C# Programming Guide) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties). They basically do the same thing as you're used to writing by hand, but with a field that has some compiler-generated name which is not a legal identifier in C# so you cannot accidentally (nor on purpose) refer to the backing field directly.

      1 Reply Last reply
      0
      • X Xarzu

        from time to time, I see some sample code that is handed to me that has a code snippet that looks like this: public string Name { get; set; } Does this actually do anything? Does it really make it so that one can set and get the "Name" string variable? I have always thought that what is required is something like this: private string name; public string Name { get { return this.name; } set { this.name = value; } } Am I correct? Please advise.

        F Offline
        F Offline
        F Xaver
        wrote on last edited by
        #3

        Thats just the shortened form, the compiler will add that private var for you, so its easyer to read and write.

        1 Reply Last reply
        0
        • X Xarzu

          from time to time, I see some sample code that is handed to me that has a code snippet that looks like this: public string Name { get; set; } Does this actually do anything? Does it really make it so that one can set and get the "Name" string variable? I have always thought that what is required is something like this: private string name; public string Name { get { return this.name; } set { this.name = value; } } Am I correct? Please advise.

          S Offline
          S Offline
          suresh446
          wrote on last edited by
          #4

          Generally, in c# Property is an extension of class variable and it provides a mechanism to read, write or change the value of class variable without effecting the external way of accessing it in our applications. check properties in c#, it will help you to understand why we required to use properties (get, set) in c#.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups