Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# constructor with new and get integer

Tags:

c#

This is a structure on Unity 5.5.0 I'm pretty new to c# and do not understand properties and structures well.

This gives an error on assigning during this.X.

I'm assuming that you can't change values on a structure and the keyword this refers to the structure's property

Backing field for automatically implemented property 'Point.X' must be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer. (CS0843) (Assembly-CSharp) [LN 15]

The 'this' object cannot be used before all of its fields are assigned to (CS0188) (Assembly-CSharp) [LN 16]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public struct Point 
{

    // Don't undertstand why "public int X, Y" works fine but not this        
    public int X { get; set; }

    public int Y { get; set; }

    //public int X,Y;

    public Point(int x, int y) 
    { 
        // LN 15
        this.X=x;
        this.Y=y;
    }
}
like image 561
J DB Avatar asked Feb 16 '26 03:02

J DB


1 Answers

This is an oddity of some versions of C#; the compiler sometimes does not correctly detect that the structure has been fully initialized on all code paths through the constructor.

The fix is to say

public Point(int x, int y) : this()  // default initialization
{ 
  this.X = x;
  this.Y = y;
}

which tells the compiler "the fields are initialized to their default values before they are initialized in the constructor".

This was a bit of a failure of language design when we did C# 3; it's by no means clear that this is what you have to do. Sorry about that.

Since I have your attention, let's address this other point you raised:

I'm assuming that you can't change values on a structure

You can change values on a structure, but you should not. Structures should be:

  • small
  • logically values
  • immutable

So my advice would be to make the setters private (with private set;)

You've made a mutable point structure, which is a bad practice. Make an immutable point structure, and treat them as immutable mathematical objects, not as collections of variables.

Also:

the keyword this refers to the structure's property

The keyword this is an alias to the variable which holds the value of the structure instance. This is subtle; let me give you an example. If we say:

Point a = new Point(1, 2);

that means:

  • Make a new temporary storage variable the size of one point.
  • Invoke the two-argument constructor, making this an alias for that temporary storage variable, and copying 1 and 2 into formal parameters x and y.
  • After the constructor returns, copy the value in the temporary variable into variable a.

Now, you might note that it seems like it would be more efficient to do:

  • Invoke the two-argument constructor, making this an alias for a, and so on.

And it would be more efficient; that is called a copy elision optimization and C# does it when it knows that it is impossible for the user to notice a difference. (Challenge: construct a C# program where the user could determine whether copy elision was used or not; C# will not copy-elide in such a program!)

Similarly if you then did:

struct Point
{
   ... constructors, whatever ...
   public double DistanceFromOrigin()
   {
     return Math.Sqrt(this.X * this.X + this.Y * this.Y);
   }

When you call a.DistanceFromOrigin(), this becomes an alias for a; they are the same variable just with two different names.

This is not how reference types work. In a reference type, this is a copy of the reference. In a value type, this is an alias to a variable.

These are subtle points.

like image 86
Eric Lippert Avatar answered Feb 17 '26 16:02

Eric Lippert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!