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. Selecting the correct code

Selecting the correct code

Scheduled Pinned Locked Moved C#
csharphelp
8 Posts 3 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.
  • B Offline
    B Offline
    Brian_TheLion
    wrote on last edited by
    #1

    I have been studying C#. When writing C# code I seem to have three types of code I could use for variables but are not certain when I need to use the code. I could type this Code One

    public string Name
    {
    get;
    set;
    }

    or I could type Code Two

    public string Name
    {
    get
    {
    return name;
    }
    set
    {
    name = value;
    }

    or I could type Code Three

    public void SetLength (double len)
    {
    length = len
    }

    And sometimes Code One and Code Three are used. I think that one of these codes are used if I make the Variables private instead of public. Any help would be welcome thanks Brian

    B L 2 Replies Last reply
    0
    • B Brian_TheLion

      I have been studying C#. When writing C# code I seem to have three types of code I could use for variables but are not certain when I need to use the code. I could type this Code One

      public string Name
      {
      get;
      set;
      }

      or I could type Code Two

      public string Name
      {
      get
      {
      return name;
      }
      set
      {
      name = value;
      }

      or I could type Code Three

      public void SetLength (double len)
      {
      length = len
      }

      And sometimes Code One and Code Three are used. I think that one of these codes are used if I make the Variables private instead of public. Any help would be welcome thanks Brian

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      I'd like to suggest you do some study of what Fields, and Properties, are in C#. Note that in your examples above you never declare variables 'name and 'length. Start here: [^], [^], [^]. And, listen to Jon Skeet: [^] Properties are actually a form of Method, technically called an "accessor." Before auto-properties came along ... in C# 3.0 ... ('set and 'get only), this was a canonical pattern for Properties:

      private int _mInt; // private backing store

      public int MInt // public property
      {
      get { return _mInt; } set { _mInt = value; }
      }

      Now, when you write:

      public int MInt { get; set; }

      The private backing field is created behind the scenes. These two examples are, functionally, identical. Some basic research and experimenting, now, will really benefit you.

      «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

      B 1 Reply Last reply
      0
      • B BillWoodruff

        I'd like to suggest you do some study of what Fields, and Properties, are in C#. Note that in your examples above you never declare variables 'name and 'length. Start here: [^], [^], [^]. And, listen to Jon Skeet: [^] Properties are actually a form of Method, technically called an "accessor." Before auto-properties came along ... in C# 3.0 ... ('set and 'get only), this was a canonical pattern for Properties:

        private int _mInt; // private backing store

        public int MInt // public property
        {
        get { return _mInt; } set { _mInt = value; }
        }

        Now, when you write:

        public int MInt { get; set; }

        The private backing field is created behind the scenes. These two examples are, functionally, identical. Some basic research and experimenting, now, will really benefit you.

        «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

        B Offline
        B Offline
        Brian_TheLion
        wrote on last edited by
        #3

        Thanks Bill. I'm studying some code examples. Thanks for letting me know about the upgrade in C# causes things to happen in the background. Maybe some code examples are for the older version of C#. In your reply you said Start here: [^], [^], [^]. And, listen to Jon Skeet: [^] I'm, not certain if there was a site address that you wanted me to go to? Brian

        B 1 Reply Last reply
        0
        • B Brian_TheLion

          Thanks Bill. I'm studying some code examples. Thanks for letting me know about the upgrade in C# causes things to happen in the background. Maybe some code examples are for the older version of C#. In your reply you said Start here: [^], [^], [^]. And, listen to Jon Skeet: [^] I'm, not certain if there was a site address that you wanted me to go to? Brian

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #4

          Each of those ^ links should take you to relevant content ... if they're broken, let me know. May I suggest re-framing your concern from "Selecting the correct code" to getting a good grasp of C# language fundamentals so that, in the future, you can make wise choices about what semantics and structures to use ? :) You have the possibility of 'static variables, as well, and, the options for access: 'public, 'private, 'protected, 'internal. C# is a very rich, and deep, language: for better, and worse, there are multiple ways to implement many fundamental structures, and relationships. cheers, Bill

          «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

          B 2 Replies Last reply
          0
          • B BillWoodruff

            Each of those ^ links should take you to relevant content ... if they're broken, let me know. May I suggest re-framing your concern from "Selecting the correct code" to getting a good grasp of C# language fundamentals so that, in the future, you can make wise choices about what semantics and structures to use ? :) You have the possibility of 'static variables, as well, and, the options for access: 'public, 'private, 'protected, 'internal. C# is a very rich, and deep, language: for better, and worse, there are multiple ways to implement many fundamental structures, and relationships. cheers, Bill

            «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

            B Offline
            B Offline
            Brian_TheLion
            wrote on last edited by
            #5

            Hi Bill. I'm trying to get a good understanding of C# and these are some of the things that beginners to C$ have trouble with but I'm hoping that the more I study the code and try writing some small programs of my own then everything should fall into place. I'm more use to using Basic (in the past) which had global variables. Brian

            B 1 Reply Last reply
            0
            • B BillWoodruff

              Each of those ^ links should take you to relevant content ... if they're broken, let me know. May I suggest re-framing your concern from "Selecting the correct code" to getting a good grasp of C# language fundamentals so that, in the future, you can make wise choices about what semantics and structures to use ? :) You have the possibility of 'static variables, as well, and, the options for access: 'public, 'private, 'protected, 'internal. C# is a very rich, and deep, language: for better, and worse, there are multiple ways to implement many fundamental structures, and relationships. cheers, Bill

              «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

              B Offline
              B Offline
              Brian_TheLion
              wrote on last edited by
              #6

              Thanks Bill for the useful links. Lots of useful information at these links. Brian

              1 Reply Last reply
              0
              • B Brian_TheLion

                I have been studying C#. When writing C# code I seem to have three types of code I could use for variables but are not certain when I need to use the code. I could type this Code One

                public string Name
                {
                get;
                set;
                }

                or I could type Code Two

                public string Name
                {
                get
                {
                return name;
                }
                set
                {
                name = value;
                }

                or I could type Code Three

                public void SetLength (double len)
                {
                length = len
                }

                And sometimes Code One and Code Three are used. I think that one of these codes are used if I make the Variables private instead of public. Any help would be welcome thanks Brian

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

                When you use <pre> tags, please make sure you select the correct language from the dropdown list that appears when you click the code button above the edit box. And also make sure that the checkbox marked "Treat my content as plain text, not as HTML" below the edit box, is unchecked.

                1 Reply Last reply
                0
                • B Brian_TheLion

                  Hi Bill. I'm trying to get a good understanding of C# and these are some of the things that beginners to C$ have trouble with but I'm hoping that the more I study the code and try writing some small programs of my own then everything should fall into place. I'm more use to using Basic (in the past) which had global variables. Brian

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #8

                  A combination of study, and coding small-scale examples that use the information and concepts you are studying, is an excellent way to learn. You can search CodeProject to find books that I, and other people, have recommended. This one is free (in English): [^] ... [^]

                  «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                  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