What are disadvantages of Auto-implemented properties over Normal properties in C# / VB.NET

Hi,

Can you please let me know in simple terms:

What are disadvantages of Auto-implemented properties over Normal properties in C# / VB.NET

Thanks
milani_lucieAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
the only limitation rather than disadvantage that i could figure out is that there has to be both getter and setter implemented. you cannot have one of them.
However, you don't have to make them public. It is completely fine to do something like:

public string MyString { get; private set; }

Open in new window


to simulate a read-only property (the "write" is only accessible within the class itself), or

public string MyString { private get; set; }

Open in new window


to simulate a write-only property (the "read" is only accessible within the class itself).

You can't, however, make both "get" and "set" private--this will give a compiler error.

Auto implementing properties require to target .NET 4.0.
Not entirely true. See post above: http:#35455254.

You can access the backing field with Me._PropertyName or this._PropertyName, but it is hidden from IntelliSense
Why would you want to? If you're going to access the backing field, you might as well create a private variable. At least this would be more intuitive to coders coming behind you.
0
 
Imran Javed ZiaConnect With a Mentor Consultant Software Engineer - .NET ArchitectCommented:
There are not many, but few are:

If you have to apply some custom login in set or get then you cannot do that

It is ambigous for new persons (rare in practive)

Your code will not be backword compatible (minnor issue, very rare in practice)

thanks
0
 
käµfm³d 👽Commented:
To expand on IJZ's third point, auto-implemented properties do not exist in pre-4.0 VB. They do exist in 3.0+ C#, however.
0
 
BuggyCoderConnect With a Mentor Commented:
the only limitation rather than disadvantage that i could figure out is that there has to be both getter and setter implemented. you cannot have one of them.
0
 
andr_ginConnect With a Mentor Commented:
1.) Auto implementing properties require to target .NET 4.0. If you run your software on more than one computer, try to target .NET 2.0.
2.) You cannot set them ReadOnly
3.) You can access the backing field with Me._PropertyName or this._PropertyName, but it is hidden from IntelliSense
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.