C sharp properties LEARNOVITA

Properties in C# | The complete Tutorial

Last updated on 16th Aug 2022, Blog, Tutorials

About author

Karan Baski (C# .Net Developer )

Karan Baski is a C# .Net Developer expert and subject specialist who has experience with Git, WPF,VB,.NET Framework,.NET Core, SVN,HTTP,.NET technology stack, WPF, C#, SQL server, Web API, ASP.NET. His articles help the learners to get insights about the Domain.

(5.0) | 19688 Ratings 2118

What is a Property in C#

A Property in C# is a member of a class that is used to set and get the data from the data field (i.e. variable) of a class.

The major important point that need to remember is that a property in C# is never used to save any data, it just acts as an interface or medium to transfer the data.

Use the Properties as they are public data members of a class, but they are special methods called accessors.

Why do we need Properties in C#

Properties in C#

To encapsulate and protect the data members (i.e. fields or variables) of a class, use properties in C#.

The Properties in C# are used as a mechanism to set and get the values of data members of a class outside of the class.

If a class contains any values in it and if you want to access those values outside of the class, then it can provide access to those values in two different ways. They are follows:

    1. 1. By saving the value under a public variable, can give direct access to the value outside of the class.
    2. 2. By saving that value in a private variable, can also give access to that value outside of the class by explaining a property for that variable.

Usage of C# Properties

    1. 1. C# Properties can be read-only or write-only.
    2. 2. Can have logic while setting the values in the C# Properties.
    3. 3. Made fields of the class private, so that fields can’t be accessed from outside class directly.
    4. 4. Now forced to use C# properties for setting or getting values.

C# Properties Example

  • using System;
  • public class Employee
  • {
  • private string name;
  • public string Name
  • {
  • get
  • {
  • return name;
  • }
  • set
  • {
  • name = value;
  • }
  • }
  • }
  • class TestEmployee{
  • public static void Main(string[] args)
  • {
  • Employee e1 = new Employee();
  • e1.Name = “Sonoo Jaiswal”;
  • Console.WriteLine(“Employee Name: ” + e1.Name);
  • }
  • }
  • Different types of Properties

    Property Types
      1. 1. Read-Only Property
      2. 2. Write Only Property
      3. 3. Read Write Property
      4. 4. Auto-Implemented Property

    What is Read-only Property in C#

    It is used to read the data from a data field i.e. read the data of a variable.

    Using this Read-Only Property, cannot set the data into a data field.

    This property will contain only a single accessor i.e. get accessor.

    Syntax:

    • AccessModifier Datatype PropertyName
    • {
    • get {return DataFieldName;}
    • }

    What is Write only Property in C#

    It is used to write the data into a data field .

    Using this Write-Only Property, I cannot read the data from the data field.

    This property will contain only a single accessor i.e. set accessor.

    Syntax:

    • AccessModifier Datatype PropertyName
    • {
    • set {DataFieldName = value;}
    • }

    Read Write Property

    It is used for both reading the data from the data field as well as writing the data into the data field of a class.

    This property will contain 2 accessors i.e. set and get.

    The set accessor is used to set or write the value to a data field and the accessor is read the data from a variable.

    Syntax:

  • AccessModifier Datatype PropertyName
  • {
  • set {DataFieldName = value;}
  • get {return DataFieldName;}
  • }
  • Advantages of using Properties in C#

      1. 1. Properties will provide the abstraction to data fields.
      2. 2. They also provide authentication to the data fields.
      3. 3. Properties can also validate the data before saving it in the data fields.

    Default Accessibility Specifier of Accessors in C#

    The default accessibility specifier of the accessor is similar to the accessibility specifier of the property.

    For example:

    • public int EmpId
    • {
    • set { _EmpId = value; }
    • get { return _EmpId; }
    • }

    The set accessor is considered as protected while the get accessor is public by default, so they are known as the asymmetric.

    Asymmetric accessors are used in the inheritance process.

    Auto-Implemented Properties in C#

    If do not have any additional logic setting and getting the data from a data field i.e. from a variable of a class, then can make use of the auto-implemented properties which introduced as part of C# 3.0.

    The Auto-Implemented Property in the C# reduces the amount of code that has to be written.

    When use auto-implemented properties, then the C# the compiler implicitly makes a private, anonymous field or variable for that property behind the scene which is going to hold the data.

    Why are properties necessary for C# real-time applications

    Declaring the class fields or variables as public and exposing those fields or variables to the outside world is bad as they do not have any control over what gets assigned and what gets returned.

    Using Properties

    Properties combined aspects of both fields and the methods.

    To the user of an object, a property appears to be a field, accessing the property needs the same syntax.

    A property is represented by one or two code blocks that represent a get accessor and/or a set accessor by the class implementer.

    The code block for the get accessor is executed when the property is read; the code block for the set accessor is executed when the property is assigned an innovative value.

    A property without a set accessor is considered a read-only.

    A property without a get accessor is considered a write-only.

    Read-write is a property that has both accessors.

    Unlike fields, properties are not classified as the variables.

    Therefore, cannot pass a property as a ref or out parameter.

    Properties have many uses: they can check data before allowing a change; they can transparently expose data on a class where that data is retrieved from the some other source, such as a database; they can take an action when data is modified, such as raising an event, or changing the value of other fields.

    Using the term abstract, a property inside the class may be designated as abstract.

    A class’s abstract properties contain no code at all.

    A semicolon makes it simple to express the get/set accessors.

    Both set and get assessors are implemented in the derived class.

    If the abstract class only has set accessors, descendant classes can only implement sets.

    Static Properties

    C# supports static properties, which belong to the class rather than to the objects of the class.

    All the rules applicable to the static member are applicable to the static properties also.

    properties & Inheritance

    The properties of a Base class can be inherited to the Derived class.

    C# Features

    C Features Programming Language

    C# is the object oriented programming language.

    It provides a features that are :

    • Simple
    • Modern programming language
    • Object oriented
    • Type safe
    • Interoperability
    • Scalable and Updateable
    • Component oriented
    • Structured programming language
    • Rich Library
    • Fast speed

    1) Simple

    C# is a simple language in the sense that it offers a structured approach with a rich set of library functions, data types etc.

    2) Modern Programming Language

    C# programming is based upon the current trend and it is very powerful and easy for building scalable, interoperable and robust applications.

    3) Object Oriented

    OOPs makes development and maintenance easy in C#, however in Procedure-oriented programming languages, it is difficult to maintain if code expands as project size increases. simple to manage if code grows as project size grows.

    4) Type Safe

    C# type safe code can access the memory location that it has permission to execute. Therefore it increases the security of the program.

    5) Interoperability

    Interoperability process provide the C# programs to do almost anything that a native C++ application can do.

    6) Scalable and Updateable

    C# is automatically scalable and an updateable programming language. For updating our application we delete the old files and update them with the new ones.

    7) Component Oriented

    It is the predominant software development methodology used to develop more robust and more scalable applications.

    8) Structured Programming Language

    Can break the program into parts using the functions So, it is simple to understand and modify.

    9) Rich Library

    C# provides a lot of inbuilt functions that made the development fast.

    10) Fast Speed

    The compilation and execution time of C# language is quick.

    Are you looking training with Right Jobs?

    Contact Us

    Popular Courses