Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# class property definitions

Tags:

c#

Will I create any problems if I make all my class properties structure members like the following code does?

    private struct Properties
    {
        public int p1;
        public int p2;
    }
    private Properties P;
    public int p1 { get { return P.p1; } set { P.p1 = value; } }
    public int p2 { get { return P.p2; } set { P.p2 = value; } }

I did the analogous thing in VB for years, but then speed was not important. Now I am just getting started with C# on real time projects where speed matters. Thanks for any feedback!

like image 942
OldRoboCoder Avatar asked Dec 19 '25 16:12

OldRoboCoder


1 Answers

Yes. The problem will be unnecessary code. You could just shorten your code like this, and it will still function the same:

public int p1 { get;set; }
public int p2 { get;set; }

If you wanted to set breakpoints on getter or setter, you could use a backing private field like so:

private int _p1;
public int P1
{
    get { return _p1; }
    set { _p1 = value; }
}

private int _p2;
public int P2
{
    get { return _p2; }
    set { _p2 = value; }
}
like image 152
wingerse Avatar answered Dec 24 '25 10:12

wingerse



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!