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 8283959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:55:37+00:00 2026-06-08T10:55:37+00:00

Possible Duplicate: What is Difference between Property and Variable in C# I started working

  • 0

Possible Duplicate:
What is Difference between Property and Variable in C#

I started working with C# a few weeks ago, and this is something that has really been bugging me. C# allows these so-called ‘magic’ getters and setters, also known as ‘syntactical sugar’. So, I can do something like this:

public int myInt { get; set; }

But from an encapsulation standpoint, this is pointless. For one, the data member is public, and I can get/set it using the dot operator. However, if I do this:

private int myInt { get; set; }

I can’t access it at all, as myInt is inaccessible due to protection level. What is this actually doing? I thought this was supposed to be an easy way to accomplish data encapsulation, so I wouldn’t have to do this:

private int myInt;
public void setMyInt(int i) { myInt = i; }
public int getMyInt() { return myInt; }

But it’s not. As near as I can tell, I’m just making these variables public. I thought maybe I would be able to do something like

public int myInt { get; }

So the client could get it, but not set it, but no, public access is still allowed. So what gives?

EDIT
I’m not trying to do anything specific, I just want to understand how this actually works. To clarify:

Making the variable public doesn’t accomplish encapsulation, especially when I can access it with the dot operator. Writing getters and setters for a private variable allows you to make changes to the variable, but gives you greater control over how that actually happens.

  • 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-06-08T10:55:39+00:00Added an answer on June 8, 2026 at 10:55 am

    The purpose is that you maintain encapsulation through future modification.

    If you initially write your class using automatic getters and setters:

    public int Count { get; set; }
    

    Then it’ll maintain precisely the same external interface if you then change it to

    public int Count {
        get { /* very complicated logic */ }
        set { /* even more complicated logic */ }
    }
    

    The automatic ones are just to help you with the simple properties at first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Difference between int[] array and int array[] I was sure that this
Possible Duplicate: Difference between class property mVar and instance variable self.mVar I am new
Possible Duplicate: Is there a difference between an instance variable and a property in
Possible Duplicate: Difference between pointer variable and reference variable in C++ I saw this
Possible Duplicate: Is there a difference between an "instance variable" and a "property" in
Possible Duplicate: Difference between Property and Field in C# I thought that basic properties
Possible Duplicate: what is the difference between match_parent and fill_parent property in android FILL_PARENT
Possible Duplicate: Difference between (function(){})(); and function(){}(); I am trying to understand a few
Possible Duplicate: Difference between Color.red and Color.RED I have seen that the Java class
Possible Duplicate: Difference between pointer variable and reference variable in C++ When should I

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.