Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6088595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:00:11+00:00 2026-05-23T12:00:11+00:00

Can somebody help me understand the get & set ? Why are they needed?

  • 0

Can somebody help me understand the get & set?
Why are they needed? I can just make a public variable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:00:11+00:00Added an answer on May 23, 2026 at 12:00 pm

    Warning: I am assuming you already know about object-oriented programming.

    What are properties?

    Properties are language elements that allow you to avoid the repetitive getXYZ() accessors and setXYZ() mutators techniques found in other languages, like Java.

    Why do they exist?

    They aim to solve the following problems:

    1. Saying get and set in the beginning of every access or mutation of a value is annoying and distracting.

      In Java, you often say:

      class person
      {
          private int _age;
          public void setAge(int value) { /*check value first, then set _age*/ }
          public int getAge() { return this._age; }
      }
      

      and then consistently say:

      if (person.getAge() > blah || person.getAge() < 10)
      {
          person.setAge(5);
      }
      

      After a while, the get and set become rather annoying.

    2. Providing direct access to the actual variable breaks encapsulation, so that’s not an option.

    How are they used?

    They are used just like variables. You read/write to them just like variables.

    How are they created?

    They are created as methods. You define a pair of methods that:

    1. Return the current value of the property. Oftentimes, this is nothing more than something like the following:

      class Person
      {
          private int _age; //Declare the backing field
      
          public int Age
          {
              get { return this._age; }
              set { ... }
          }
      }
      
    2. Set the value of the property:

      class Person
      {
          public int Age
          {
              get { ... }
              set
              {
                  if (value < 0) //'value' is what the user provided
                  { throw new ArgumentOutOfRangeException(); } //Check validity
                  this._age = value;
              }
          }
      }
      

    Other notes:

    Auto-implemented Properties

    C# 3.0 introduced auto-implemented properties:

    public int Age { get; set; }
    

    This is equivalent to:

    private int _age; //The name is auto-generated
    public int Age { get { return this._age; } set { this._age = value; } }
    

    Why does it exist?

    It helps you avoiding breaking changes in client executables.

    Let’s say you’re lazy and don’t want to type the whole thing, and decide to expose a variable publicly. You then create an executable that reads from or writes to that field. Then you change your mind and decide that you in fact needed a property, so you change it to one.

    What happens?

    The depending executable breaks, because the code is no longer valid.

    Auto-implemented properties help you avoid that, without extra redundancy in your initial code.

    Indexers

    Indexers extend the property syntax to let you index objects (surprise!), just like arrays.
    For C++ users: This is similar to overloading operator [].

    Example:

    private int[] _elements;
    
    public int this[int index] //Indexed property
    {
        get { return this._elements[index]; }
        set
        {
            //Do any checks on the index and value
            this._elements[index] = value;
        }
    }
    

    You then use them like obj[5] = 10;, which is equivalent to calling the set method of obj‘s indexer.
    In fact, System.Collections.Generic.List<T> is indexed:

    var list = new List<int>();
    list.Add(10);
    list[0] = 5;  //You're indexing list, as though it were an array!
    

    Isn’t that neat? 🙂

    Anything else?

    There are many more features to properties, not all of which are available in C#:

    • Parametrized properties, of which indexers are a special kind
    • Getter/setter access modifiers (in C#)
    • Multiple getters or setters (not in C#)
    • Et cetera
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can somebody help me to suggest how can I get the play list from
Can somebody help me understand what the real difference is and why the MVVM
Can somebody help me understand how this pointed corner was actually generated in css
Can somebody please help me understand why my custom JComponent 'Bar', only displays when
Can somebody help me with this. There is HTML code: <h3> <label> <input type=checkbox
Sure it's a simple question, but I can't fix it, can somebody help me?
Hopefully somebody can help me out here. I'm working within an embedded ActionScript2 and
hopefully somebody can help The table structure is as follows: tblCompany: compID compName tblOffice:
I hope somebody can help me. I've been trying to write a custom html
I am trying to understand the difference/disadvantages of strcpy and strncpy. Can somebody please

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.