Ticker

6/recent/ticker-posts

C# (C Sharp) - Access modifiers

Why to use access modifiers?

Access modifiers are an integral part of object-oriented programming. They support the concept of encapsulation, which promotes the idea of hiding functionality. Access modifiers allow you to define who does or doesn't have access to certain features. 

In C# there are 5 different types of Access Modifiers. 

Modifier 
Description 
 public  There are no restrictions on accessing public members. 
 private  Access is limited to within the class definition. This is the default access modifier type if none is formally specified
 protected  Access is limited to within the class definition and any class that inherits from the class 
 internalAccess is limited exclusively to classes defined within the current project assembly
 protected internal Access is limited to the current assembly and types derived from the containing class. All members in current project and all members in derived class can access the variables. 

Properties 

Properties are the special type of class members that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and help to promote the flexibility and safety of methods. Encapsulation and hiding of information can also be achieved using properties. It uses pre-define methods which are “get” and “set” methods which helps to access and modify the properties. 

Accessors: The block of “set” and “get” is known as “Accessors”. It is very essential to restrict the accessibility of property. There are two type of accessors i.e. get accessors and set accessors. There are different types of properties based on the “get” and set accessors: 

1. Read and Write Properties: When property contains both get and set methods. 

2. Read-Only Properties: When property contains only get method. 

3. Write Only Properties: When property contains only set method. 

The syntax for Defining Properties:   

<access_modifier> <return_type> <property_name> 

get 

// body 

}

set 

// body 

}

//Read Only Property

Get Accessor: It specifies that the value of a field can access publicly. It returns a single value and it specifies the read-only property. 

class Geeks 

// Declare roll_no field 

private int roll_no; 

// Declare roll_no property 

public int Roll_no 

get 

return roll_no; 

}

}

//Write-only Property 

Set Accessor: It will specify the assignment of a value to a private field in a property. It returns a single value and it specifies the write-only property. 

class Geeks 

// Declare roll_no field 

private int roll_no; 

// Declare roll_no property 

public int Roll_no 

get 

return roll_no; 

set 

roll_no = value; 

}

}


"access modifiers in c#"

"access modifiers in c++"

"default access modifier in c#"

"access modifiers in c# with examples"

"internal access modifier in c#"

"protected internal c#"