Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Custom getter/setter without private variable

I learned c# recently, so when I learned to write properties, I was taught to do it like this:

public string Name { get; set; } 

Auto properties are great! But now I'm trying to do something a little more complicated, so I need to write a custom pair of accessors.

private string _Name; public string Name {     get { return _Name; }     set { _Name = value } } 

I know the compiler makes a private instance variable down in it's murky depths when one uses autos, but I'm spoiled and don't want that private variable sitting around looking pointless.

Is there a way to use custom accessors without a private variable?

like image 213
Tin Can Avatar asked Dec 20 '11 15:12

Tin Can


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C or C++ same?

While C and C++ may sound similar, their features and usage differ. C is a procedural programming language that support objects and classes. On the other hand C++ is an enhanced version of C programming with object-oriented programming support.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.


2 Answers

Properties don't need backing variables (fields) at all. While they can be used for encapsulating simple fields you can also use them to access other data.

public Decimal GrandTotal { get { return FreightTotal + TaxTotal + LineTotal; } } 

or

public string SomeStatus { get { return SomeMethodCall(); } } 

If the goal is to simply encapsulate some field with a property you would need some sort of backing field if you are not using automatic properties.

like image 144
jhappoldt Avatar answered Oct 20 '22 09:10

jhappoldt


The answer is No, you cannot do that. It is because of recursion. (See line numbers 9 and 7):

Line 1  :   public string Name Line 2  :   { Line 3  :     get Line 4  :     { Line 5  :         return FirstName + " "  + LastName; Line 6  :     } Line 7  :     set Line 8  :     { Line 9  :         Name = value; // <-- Goes back to Line 7 Line 10 :     } Line 11 :   } 
like image 22
tika Avatar answered Oct 20 '22 09:10

tika