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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:44:00+00:00 2026-05-25T19:44:00+00:00

I have recently learned C# fully, approximately. However, in C++ I could implement a

  • 0

I have recently learned C# fully, approximately. However, in C++ I could implement a better encapsulation when passing objects to methods, with const modifier. But in C#, objects can be modified through the reference parameter, such as Dispose on Graphics object.

I need sometimes more encapsulation. Properties can give a better solution. However, I have found that, for example, the modification to Form.Location property or even to its X member is not allowed. Although Form.Location is get; set; property, its members can not be modified. I tried to do as it with a Point {get; set;} property but I can still modify X member.

This is also an issue when a method for example Disposes an object, as eariler mentioned.

How can I implement more encapsulation in C#?

  • 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-25T19:44:01+00:00Added an answer on May 25, 2026 at 7:44 pm

    You cannot the X property of Form.Location, because Form.Location is a property that returns a value type (Point). As soon as you access Form.Location, you get a copy of the location; thus, changing something in this copy is meaningless. Since this.Location.X = ... is an obvious mistake, the C# compiler prevents you from doing it.

    You can, however, replace the complete value of Form.Location, since its property setter is public:

    private void button1_Click(object sender, EventArgs e)
    {
        // move window to the left edge of the screen
        this.Location = new Point(0, this.Location.Y);    
    }
    

    or, alternatively,

    private void button1_Click(object sender, EventArgs e)
    {
        // move window to the left edge of the screen
        var loc = this.Location;
        loc.X = 0;               // yes, it's a mutable struct
        this.Location = loc;
    }
    

    To get back to your original question: If you want to make an object accessible, but do not want it to be modified, you will need to encapsulate it yourself. For example, if you want the consumers of your class to be able to Draw on your Graphics object but do not want them to call Dispose, just cannot expose the Graphics object directly. Instead, you need to wrap every method that the consumer is allowed to call:

    // bad, consumer can do anything with the Graphics object
    public Graphics Graphics
    {
        get { return graphics; }
    }
    
    // good, consumer can only do specific stuff
    public void Draw(...)
    {
        graphics.Draw(...);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently learned the Ruby programming language, and all in all it is
I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead
I have recently learned that sql server 2005 does not support UTF8: UTF8 problem
I'm recently have learned to use Zend Framework. I did a simple CRUD application.
I have learned recently that Export in Mathematica uses by default the Printout screen
I recently learned about code obfuscation. Its nice thing to do, when you have
Recently I have learned the basics concepts of Git. We did use a bit
Possible Duplicate: Using A* to solve Travelling Salesman Problem I have recently learned that
From an old answer I have recently learned that Google Analytics, besides the traditional
I'm fairly new to C#, and have recently learned that it's possible to emit

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.