How can I use the property {get; }?

I've noticed that Microsoft.Xna.Framework.Rectangle

struct

there are many properties that are just public int Bottom { get; }

or public Point Center { get; }

. I, for my life, cannot understand what is happening here. I've tried to reproduce this in some of my own structures, but I can't figure out how to give it a meaning in the first place without the keyword set;

. What does it do Rectangle

struct

for {get;}

?

+3


source to share


3 answers


The reason that Rectangle.Bottom

is not set, is that this is a calculated value Top + Height

. If you installed this, what would you like? Change y position? Change the height? It is impossible to know. Therefore, you have to decide for yourself and change Top

or Height

based on what you really want.

The idea behind properties is not only to have a variable, but to set or get it. If it were, we could just use public variables and that. Rather, the idea is to allow validation and calculated properties.

public int Bottom
{
  get { return Top + Height; }
}

      



As you can see, there is no need to set it to anything as it will infer its value based on other values.

(Of course, internally it probably won't use other properties, but rather actual variables due to performance)

+1


source


This means that the base value that the property gives you access cannot be set later .. you can only "get" the base value.

When you create Rectangle

, you must pass several values ​​to it:

public Rectangle (int x, int y, int width, int height)

      

My guess (despite the original code) is that the property values ​​( Center

, Bottom

etc.) are set in the constructor. You cannot change them afterwards .. either look for another property to set (i.e. X

or Y

), or create a new Rectangle

one based on an existing one.



var newRect = new Rectangle(oldRect.X, oldRect.Y, oldRect.Width, oldRect.Height);

      

By comparison, here's some of the source code from a framework System.Drawing.Rectangle

that is probably pretty close to what you're dealing with. Note that you can set certain values ​​through the constructor, which are then stored in private variables and available (but only changed in some) properties.

public struct Rectangle
{
    public static readonly Rectangle Empty = new Rectangle();

    private int x;
    private int y;
    private int width;
    private int height;

    public Rectangle(int x, int y, int width, int height)
    {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public int X
    {
        get { return x; }
        set { x = value; }
    }

    public int Left
    {
        get { return X; }
    }

    public int Y
    {
        get { return y; }
        set { y = value; }
    }

    public int Top
    {
        get { return Y; }
    }

    ...
    ...
}

      

+1


source


Consider the following:

    private int myVar;

    public int MyProperty
    {
        get { return myVar; }
    }

      

Here you see an example taken directly from Visual Studio C # Snippets showing how to implement the get-only property. You need to set the field, but it cannot be done through a property because this property is called read - just a property or a property without a setter method . The purpose of such properties is to make a contractual statement about your object "this property cannot be set".

This is similar to having a private setter, however you cannot apply access modifiers in an interface definition. Therefore, this syntax serves a specific purpose in defining data contracts and object interfaces, which means that "this property cannot be contractually set, and no subclass can publish a public setter as part of that contract."

As a third party, you can get around access modifiers using reflection , but this is not a common case (and 99% of the .NET developers out there probably don't know this fact.)

Usually , backing fields are set through a constructor, through reflection, or as part of an object's initialization.

It is also the basic syntax and forms the basis of modern syntactic sugar. Consider the following property definition:

    public int MyProperty { get; set; }

      

This is completely syntactic sugar and not valid for the C # 1.0 compiler. Today compile-time is created on your behalf . Thus, the following syntax is only valid for interface definitions (since it will never return a meaningful value otherwise.)

    public int MyProperty { get; }

      

The above (invalid) attempt to create a read-only property using the newer auto-property .

Literature:

0


source







All Articles