Reputation: 107
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!
Upvotes: 2
Views: 149
Reputation: 361
Honestly, I don't think your approach will create any problems. Speed and memory are definitely not an issue if you have one small extra struct with your properties - unless you are running your program on some device with just 64K Ram or so but I assume you don't.
So for your question - if you know what you're doing and accessing the right struct variables, then your code will be just fine.
However, as mentioned above, your approach is far from best practice.
C# is a language that uses either Auto-Properties or Properties with underlying backing fields extensively and it is considered best practice and also best maintainable if you follow this structure.
Properties also have a lot of neat advantages - further information under this link:
MSDN Property Tutorial
So, in summary, your approach is not necessarily wrong or slow, it is simply not best practice. And because we encourage best practice, I'd encourage you to not use your approach.
Upvotes: 0
Reputation: 3796
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; }
}
Upvotes: 10