Javascript required
Skip to content Skip to sidebar Skip to footer

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.

C# Properties

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.

  1. using  System;
  2. course  MyClass
  3. {
  4. private int  x;
  5. public void  SetX( int  i)
  6.     {
  7.         x = i;
  8.     }
  9. public int  GetX()
  10.     {
  11. return  x;
  12.     }
  13. }
  14. class  MyClient
  15. {
  16. public static void  Principal()
  17.     {
  18.         MyClass mc =new  MyClass();
  19.         mc.SetX(10);
  20. int  xVal = mc.GetX();
  21.         Panel.WriteLine(xVal);
  22.     }
  23. }

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.

  1. <acces_modifier> <return_type> <property_name>
  2.    {
  3.       get
  4.    {
  5.    }
  6.       set
  7.    {
  8.    }
  9. }

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.

  1. class  MyClass
  2. {
  3. private int  x;
  4. public int  Ten
  5.     {
  6. get
  7.         {
  8. return  10;
  9.         }
  10. set
  11.         {
  12.             x = value;
  13.         }
  14.     }
  15. }

The object of the class MyClass can access the belongings X every bit follows.

  1. 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.

  1. using  System;
  2. form  MyClass
  3. {
  4. private int  x;
  5. public int  X
  6.     {
  7. get
  8.         {
  9. return  x;
  10.         }
  11. ready
  12.         {
  13.             x = value;
  14.         }
  15.     }
  16. }
  17. class  MyClient
  18. {
  19. public static void  Main()
  20.     {
  21.         MyClass mc =new  MyClass();
  22.         mc.X = 10;
  23. int  xVal = mc.X;
  24.         Console.WriteLine(xVal);
  25.     }
  26. }

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.

  1. using  Organization;
  2. form  MyClass
  3. {
  4. private static int  x;
  5. public static int  Ten
  6.     {
  7. become
  8.         {
  9. return  10;
  10.         }
  11. set
  12.         {
  13.             x = value;
  14.         }
  15.     }
  16. }
  17. class  MyClient
  18. {
  19. public static void  Main()
  20.     {
  21.         MyClass.X = ten;
  22. int  xVal = MyClass.X;
  23.         Panel.WriteLine(xVal);
  24.     }
  25. }

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.

  1. using  System;
  2. class  Base of operations
  3. {
  4. public int  X
  5.     {
  6. go
  7.         {
  8.             Console.Write("Base of operations Get" );
  9. return  10;
  10.         }
  11. set
  12.         {
  13.             Panel.Write("Base Set up" );
  14.         }
  15.     }
  16. }
  17. class  Derived : Base
  18. {
  19. }
  20. class  MyClient
  21. {
  22. public static void  Main()
  23.     {
  24.         Derived d1 =new  Derived();
  25.         d1.X = 10;
  26.         Panel.WriteLine(d1.Ten);
  27.     }
  28. }

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.

  1. using  System;
  2. form  Base of operations
  3. {
  4. public virtual int  X
  5.     {
  6. get
  7.         {
  8.             Panel.Write("Base GET" );
  9. return  x;
  10.         }
  11. prepare
  12.         {
  13.             Console.Write("Base Fix" );
  14.         }
  15.     }
  16. }
  17. class  Derived : Base
  18. {
  19. public override int  Ten
  20.     {
  21. become
  22.         {
  23.             Panel.Write("Derived Become" );
  24. return  ten;
  25.         }
  26. set
  27.         {
  28.             Panel.Write("Derived SET" );
  29.         }
  30.     }
  31. }
  32. grade  MyClient
  33. {
  34. public static void  Main()
  35.     {
  36.         Base b1 =new  Derived();
  37.         b1.10 = 10;
  38.         Console.WriteLine(b1.10);
  39.     }
  40. }

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.

  1. using  Organization;
  2. abstruse class  Abstruse
  3. {
  4. public abstruse int  Ten
  5.     {
  6. become ;
  7. set ;
  8.     }
  9. }
  10. form  Concrete : Abstruse
  11. {
  12. public override int  X
  13.     {
  14. go
  15.         {
  16.             Console.Write(" Go" );
  17. return  10;
  18.         }
  19. fix
  20.         {
  21.             Panel.Write(" SET" );
  22.         }
  23.     }
  24. }
  25. form  MyClient
  26. {
  27. public static void  Master()
  28.     {
  29.         Concrete c1 =new  Concrete();
  30.         c1.10 = 10;
  31.         Console.WriteLine(c1.X);
  32.     }
  33. }

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.

  1. private string  proper noun;
  2. public cord  Proper name
  3. {
  4.    become  { return this .proper noun; }
  5.    set  { this .name = value; }
  6. }

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

  1. 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.

  1. using  System;
  2. grade  Author {
  3. public cord  Name {
  4. become ;
  5. set ;
  6.     }
  7. public string  Publisher {
  8. get ;
  9. ready ;
  10.     }
  11. public string  Book {
  12. get ;
  13. set ;
  14.     }
  15. public  Int16 Year {
  16. get ;
  17. gear up ;
  18.     }
  19. public double  Price {
  20. go ;
  21. set ;
  22.     }
  23. public string  PriceInString {
  24. go  {
  25. return string .Format( "${0}" , Price);
  26.         }
  27.     }
  28. public string  Names {
  29. become ;
  30.     }
  31. public double  AuthorCount {
  32. get ;
  33. individual set ;
  34.     } = 99;
  35. public  Author( string  name, string  publisher, string  book, Int16 year, double  toll) {
  36.         Name = name;
  37.         Publisher = publisher;
  38.         Book = book;
  39.         Year = yr;
  40.         Price = price;
  41.     }
  42. public string  AuthorDetails() {
  43. return string .Format( "{0} is an writer of {1} published by {two} in yr {3}. Cost: ${4}" , Name, Book, Publisher, Year, Toll);
  44.     }
  45. public double  CostOfThousandBooks() {
  46. if  (Price > 0) render  Toll * 1000;
  47. return  0;
  48.     }
  49. }

The code in Listing is creates an instance of the class and calls its methods and backdrop.

  1. course  Program
  2. {
  3. static void  Main()
  4.   {
  5.         Author writer =new  Author( "Mahesh Chand" , "Apress" , "Programming C#" , 2003, 49.95);
  6.         Panel.WriteLine(writer.AuthorDetails());
  7.         Console.WriteLine("Author published his volume in {0}" , writer.Year);
  8.         author.Price = 50;
  9.         Console.WriteLine(writer.CostOfThousandBooks().ToString());
  10.         Console.ReadKey();
  11.     }
  12. }

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/