Properties in C# provide a flexible mechanism to handle private fields. They are named members of classes, structures etc. The member variables or methods are called fields. Properties use accessors to read, write or manipulate the values of private fields.
Some of the salient points on properties are given as follows:
The get and set accessors are used in properties. The get accessor(reading) returns the property value while a set accessor(writing) sets a new value.
An example that demonstrates the use of get and set accessor is given as follows:
Source Code: Program to implement get and set accessor in C#
public int Data { get { return this.data; } set { this.data = value; } }
A program that demonstrates properties using both get and set accessors is given as follows:
using System; namespace PropertiesDemo { class Example { char data; public char Data { get { return this.data; } set { this.data = value; } } } class Example { static void Main() { Example obj = new Example(); obj.Data = 'A'; Console.WriteLine("Implementing Properties in C#"); Console.WriteLine("The data value is: {0}", obj.Data); } } }
The output of the above program is as follows:
Implementing Properties in C# The data value is: A
Now let us understand the above program.
The class Example contains a char data type data. The get accessor returns the value of data while the set accessor sets the data to value. The code snippet for this is given as follows:
class Example { char data; public char Data { get { return this.data; } set { this.data = value; } } }
In the function Main(), the object obj of class Example is defined. The value of data is stored as ‘A’. Then this value is printed. The code snippet for this is given as follows:
static void Main() { Example obj = new Example(); obj.Data = 'A'; Console.WriteLine("Implementing Properties in C#"); Console.WriteLine("The data value is: {0}", obj.Data); }
Avery good write-up. Please let me know what are the types of C# libraries used for AI development.
very satisfied!!
Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).
Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz
The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation
Leave a Reply
Your email address will not be published. Required fields are marked *