A(N) _____exposes Its Data as Read/write, Read-only, or Write-only Properties
Belongings in C# is a fellow member of a class that provides a flexible machinery for classes to expose private fields. Internally, C# properties are special methods called accessors. A C# belongings have two accessors, become property accessor and set property accessor. A go accessor returns a holding value, and a ready accessor assigns a new value. The value keyword represents the value of a property.
Backdrop in C# and .Net have various access levels that is defined by an admission modifier. Properties can be read-write, read-only, or write-only. The read-write holding implements both, a get and a prepare accessor. A write-only property implements a fix accessor, but no get accessor. A read-only belongings implements a get accessor, merely no ready accessor.
In C#, properties are cipher only a natural extension of data fields. They are usually known as 'smart fields' in C# community. We know that information encapsulation and hiding are the two fundamental characteristics of any object oriented programming linguistic communication. In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like individual, public, protected, internal etc information technology is possible to control the accessibility of the class members.
Commonly, inside a class, we declare a information field as individual and volition provide a set of public Set up and Get methods to access the information fields. This is a practiced programming practice since the data fields are not directly accessible exterior the class. We must use the set up/get methods to admission the data fields.
An example, which uses a set of set up/go methods, is shown beneath.
- using System;
- course MyClass
- {
- private int x;
- public void SetX( int i)
- {
- x = i;
- }
- public int GetX()
- {
- return x;
- }
- }
- class MyClient
- {
- public static void Principal()
- {
- MyClass mc =new MyClass();
- mc.SetX(10);
- int xVal = mc.GetX();
- Panel.WriteLine(xVal);
- }
- }
The output from above listing is shown below.
But C# provides a built in mechanism chosen properties to practise the higher up. In C#, properties are defined using the belongings declaration syntax. The general form of declaring a holding is as follows.
- <acces_modifier> <return_type> <property_name>
- {
- get
- {
- }
- set
- {
- }
- }
Where <access_modifier> tin be private, public, protected or internal. The <return_type> can be whatsoever valid C# type. Note that the first part of the syntax looks quite like to a field proclamation and second function consists of a get accessor and a set accessor.
For instance, the above program can be modified with a property X as follows.
- class MyClass
- {
- private int x;
- public int Ten
- {
- get
- {
- return 10;
- }
- set
- {
- x = value;
- }
- }
- }
The object of the class MyClass can access the belongings X every bit follows.
- MyClass mc = new MyClass();
mc.X = 10; // calls set accessor of the property 10, and pass 10 as value of the standard field 'value'.
This is used for setting value for the data member ten.
Console.WriteLine(mc.X);// displays 10. Calls the become accessor of the belongings X.
The complete program is shown below.
- using System;
- form MyClass
- {
- private int x;
- public int X
- {
- get
- {
- return x;
- }
- ready
- {
- x = value;
- }
- }
- }
- class MyClient
- {
- public static void Main()
- {
- MyClass mc =new MyClass();
- mc.X = 10;
- int xVal = mc.X;
- Console.WriteLine(xVal);
- }
- }
Remember that a property should have at least ane accessor, either set or go. The fix accessor has a free variable available in it called value, which gets created automatically past the compiler. We can't declare any variable with the name value inside the set accessor.
We can do very complicated calculations inside the fix or get accessor. Even they can throw exceptions.
Since normal data fields and properties are stored in the same memory space, in C#, it is not possible to declare a field and property with the aforementioned proper name.
Static Properties
C# also supports static properties, which belongs to the course rather than to the objects of the grade. All the rules applicable to a static fellow member are applicable to static properties as well.
The post-obit program shows a course with a static holding.
- using Organization;
- form MyClass
- {
- private static int x;
- public static int Ten
- {
- become
- {
- return 10;
- }
- set
- {
- x = value;
- }
- }
- }
- class MyClient
- {
- public static void Main()
- {
- MyClass.X = ten;
- int xVal = MyClass.X;
- Panel.WriteLine(xVal);
- }
- }
Call up that set/get accessor of static property tin can access just other static members of the form. As well ,static properties are invoking by using the class name.
Properties & Inheritance
The backdrop of a Base of operations form can be inherited to a Derived form.
- using System;
- class Base of operations
- {
- public int X
- {
- go
- {
- Console.Write("Base of operations Get" );
- return 10;
- }
- set
- {
- Panel.Write("Base Set up" );
- }
- }
- }
- class Derived : Base
- {
- }
- class MyClient
- {
- public static void Main()
- {
- Derived d1 =new Derived();
- d1.X = 10;
- Panel.WriteLine(d1.Ten);
- }
- }
The output from above listing is shown below.
The in a higher place plan is very straightforward. The inheritance of properties is only like inheritance whatsoever other member.
Properties & Polymorphism
A Base class holding can be polymorphically overridden in a Derived course. But recall that the modifiers like virtual, override etc are using at property level, not at accessor level.
- using System;
- form Base of operations
- {
- public virtual int X
- {
- get
- {
- Panel.Write("Base GET" );
- return x;
- }
- prepare
- {
- Console.Write("Base Fix" );
- }
- }
- }
- class Derived : Base
- {
- public override int Ten
- {
- become
- {
- Panel.Write("Derived Become" );
- return ten;
- }
- set
- {
- Panel.Write("Derived SET" );
- }
- }
- }
- grade MyClient
- {
- public static void Main()
- {
- Base b1 =new Derived();
- b1.10 = 10;
- Console.WriteLine(b1.10);
- }
- }
The output from higher up list is shown beneath.
Abstract Backdrop
A property inside a class tin can be declared as abstract past using the keyword abstract. Recall that an abstract holding in a form carries no code at all. The get/set accessors are simply represented with a semicolon. In the derived class we must implement both set and get assessors.
If the abstract class contains only set accessor, we can implement only set in the derived form.
The following program shows an abstruse holding in action.
- using Organization;
- abstruse class Abstruse
- {
- public abstruse int Ten
- {
- become ;
- set ;
- }
- }
- form Concrete : Abstruse
- {
- public override int X
- {
- go
- {
- Console.Write(" Go" );
- return 10;
- }
- fix
- {
- Panel.Write(" SET" );
- }
- }
- }
- form MyClient
- {
- public static void Master()
- {
- Concrete c1 =new Concrete();
- c1.10 = 10;
- Console.WriteLine(c1.X);
- }
- }
The output from to a higher place listing is shown below.
The properties are important features added in language level inside C#. They are very useful in GUI programming. Recollect that the compiler actually generates the appropriate getter and setter methods when it parses the C# belongings syntax.
Properties Access Modifiers
Admission modifiers defines the access level of a property whether a property can be accessed by any caller programme, inside an assembly, or just within a class.
The following tabular array describes access level modifiers.
- public - The type or member can exist accessed by any other code in the same assembly or some other assembly that references it.
- Private - The type or fellow member tin can exist accessed only by code in the same course or struct.
- protected - The type or member tin be accessed just by lawmaking in the same class, or in a class that is derived from that class.
- internal - The blazon or member can be accessed by any code in the same assembly, but not from another associates.
- protected internal - The type or member can exist accessed past any lawmaking in the assembly in which it is declared, or from within a derived class in another assembly.
- private protected - The type or fellow member can exist accessed only within its declaring assembly, by code in the same class or in a blazon that is derived from that course.
Automatically Implemented Properties
A typical implementation of a public belongings looks similar Listing . The default implementation of a belongings needs a getter and setter.
- private string proper noun;
- public cord Proper name
- {
- become { return this .proper noun; }
- set { this .name = value; }
- }
Auto-implemented properties in C# makes code more readable and clean if there is no additional calculation needed. The to a higher place code of List tin be replaced by the following 1 line of lawmaking in Listing
- public string Name { get ; set ; }
In instance of auto-implemented properties, the compiler creates a individual field variable that can but be accessed through the property's getter and setter.
Lawmaking listed in List is a class with several motorcar-initialized properties.
- using System;
- grade Author {
- public cord Name {
- become ;
- set ;
- }
- public string Publisher {
- get ;
- ready ;
- }
- public string Book {
- get ;
- set ;
- }
- public Int16 Year {
- get ;
- gear up ;
- }
- public double Price {
- go ;
- set ;
- }
- public string PriceInString {
- go {
- return string .Format( "${0}" , Price);
- }
- }
- public string Names {
- become ;
- }
- public double AuthorCount {
- get ;
- individual set ;
- } = 99;
- public Author( string name, string publisher, string book, Int16 year, double toll) {
- Name = name;
- Publisher = publisher;
- Book = book;
- Year = yr;
- Price = price;
- }
- public string AuthorDetails() {
- return string .Format( "{0} is an writer of {1} published by {two} in yr {3}. Cost: ${4}" , Name, Book, Publisher, Year, Toll);
- }
- public double CostOfThousandBooks() {
- if (Price > 0) render Toll * 1000;
- return 0;
- }
- }
The code in Listing is creates an instance of the class and calls its methods and backdrop.
- course Program
- {
- static void Main()
- {
- Author writer =new Author( "Mahesh Chand" , "Apress" , "Programming C#" , 2003, 49.95);
- Panel.WriteLine(writer.AuthorDetails());
- Console.WriteLine("Author published his volume in {0}" , writer.Year);
- author.Price = 50;
- Console.WriteLine(writer.CostOfThousandBooks().ToString());
- Console.ReadKey();
- }
- }
A(N) _____exposes Its Data as Read/write, Read-only, or Write-only Properties
Source: https://www.c-sharpcorner.com/article/understanding-properties-in-C-Sharp/