properties.md - brainchildservices/curriculum GitHub Wiki
Slide 1
C# Properties
The meaning of Encapsulation is to make sure that "sensitive" data is hidden from users. To achieve this, We have two options:
- Declare fields/variables as private
- Provide public get and set methods, through properties, to access and update the value of a private field
A property is like a combination of a variable and a method, and it has two methods: a get and a set method:
The syntax for Defining Properties:
<access_modifier> <return_type> <property_name>
{
get { // body }
set { // body }
}
Slide 2
Steps to create a general property
-
Declare the field as private
-
Copy the private field. Change private to public
-
Change the first letter of the variable to Capital and then put curlybraces {}. This will be the property name.
-
Write get method or get accessor
get { return variablename; }
Slide 3
-
Write set method or set accessor
set { variablename=value; }
-
If no set -->Its a read only property
-
If no get -->Its a write only property
Slide 4
-
As you move on, you would be using the simplest way to declare a property which is like below:
<access_modifier> <return_type> <property_name>{get; set;}
Eg:
public string Name{get;set;}
How to assign value to a property?
ObjectName.PropertyName=somevalue;
How to get the value of a property?
ObjectName.PropertyName
https://dotnetfiddle.net/RRejuP
Slide 5
-
What is a property?
Property in C# is a member of a class that provides a flexible mechanism for classes to expose private fields. -
What is the use of property?
Marking the class field public & exposing is a risky, as you will not have control what gets assigned & returned.
Program to explain the use of Property:
Program 1: Any number is allowed to enter as the Student ID
https://dotnetfiddle.net/v2ZJuY
Program 2: We are using property and making sure that only numbers above 1 are allowed.
https://dotnetfiddle.net/0PpqEc
Slide 6
Program 3: Here the address is allowed to be set to null.
https://dotnetfiddle.net/1BVojB
Program 4: We are using property to make sure that we get a meaningful exception error if the address is set to null
https://dotnetfiddle.net/6MrbGv
Program 5: We are setting a password field as public. So we are able to read it. https://dotnetfiddle.net/1KHnnm
Program 6: Passwords are sensitive details that shouldn't e visible to everyone. So we are hiding it making it a write-only property
https://dotnetfiddle.net/0tpD7b
Slide 7
-
What is encapsulation?
The meaning of Encapsulation is to make sure that "sensitive" data is hidden from users. -
What is a read only property?
Read-only means that we can access the value of a property but we can't assign a value to it. When a property does not have a set accessor then it is a read-only property. ObjectName.PropertyName is allowed(reading the value)
ObjectName.PropertyName=somevalue; is not allowed(Assignining a value)
Eg: https://dotnetfiddle.net/FqUugh
Slide 8
-
What is a write only property?
A write only property is a property that we can assign a value to but can't get that value because that property doesn't have a get accessor. ObjectName.PropertyName=somevalue; is allowed
ObjectName.PropertyName is not allowed
Eg: https://dotnetfiddle.net/8KqMvB -
What is read and write property?
Read and write property mean we can both assign value to it as well as access the value of a property. It has both get and set accessor.
Slide 9
-
In what situation can you get this error?
"The property or indexer 'Student.Password' cannot be used in this context because it lacks the get accessor"
When we try to assign value of a write-only property. That is the property doesn't have a get accessor. -
In what situation can you get this error?
"Property or indexer 'Student.PassMark' cannot be assigned to -- it is read only"
When we are trying to assign value to read-only property. That is the property doesn't have a set accessor.
Slide 10
Exercise:
Q1: Write a class School
Write property PrincipalName
Write a readonly Property ManagerName
Write a writeonly Property NumberOfStudents
Write a method PrintDetails that prints the NumberOfStudents
In Main Method,
Assign value to PrincipalName property
Print the content of PrincipalName property
Print the content of ManagerName property
Assign value to NumberofStudents property
Call the method PrintDetails method
Slide 11
Q2: Write a class Cricket
Write string property Batsman
Write a method Batting
return type int
parameters string batsmanName, int runs
In Main Method,
Assign value to property Batsman
Print property Batsman
Call method Batting
Slide 12
Q3: Write a call Bedroom
Write property TvBrand
Write a readonly Property LightBrand
Write a void method AssignBrand with 1 string parameter. The value of the parameter string is passed to the field lightBrand inside the method
Write a write only Property NoOfLights
Write a method LightDetails that returns NoOfLights
In MainMethod,
Assign the value to TvBrand
Print the value of TvBrand
Call method AssignBrand ;
Print property LightBrand
Assign value to NoOfLights property
Call method LightDetails and print the returned result
Slide 13
Q4: Classname : Shop
Field : Room : string field
Property1 : Vegetable is a string property
Property2 : Meats is a write only property
Method1 : Washing : Washing has string return type that has one string parameter
Method2 : Cutting : Cutting has int return type, Has two parameters string and int
In Main method,
- Assign value to room field. Print room field
- Assign value and print value of vegetable
- Assign value to Meats
- Print the result of method washing
- Print the result of method cutting
Slide 14
Q5: ClassName: Company
Field: Owner: Owner is string field. Declare and assign value
Property1: Client : Client is string property
Property2: Buyer : Buyer is an int read only property
Method1: Designing : Designing has string return type, two parameters 2 strings. The return includes the name of Owner and Client
Method2: Development: Development has int return type has two parameters int and string
In Main method,
- Print field Owner
- Assign value and print value of Client
- Assign value to Buyer
- Print the result of method Designing
- Print the result of method Development
Slide 15
Q6: ClassName: Mobile
Field : Body
Property: (1) Camera , (2) Speaker
Method: (1) Calling, (2) Messaging
Body is string Field
Camera is a string write only property
Speaker is a string Property
Calling has no return value, has one int parameter and it prints the parameter number as "The call is being made to number- XXXXXXXXX"
Messaging has int return type and two input parameters string and int. The string parameter assigns its value to Camera. It then prints the name of the Speaker and Camera. The method then returns triple of the integer parameter.
In Main method:
- Assign value to Camera
- Read value of Speaker
- Call the method calling
- Call the method Messaging
Reference: https://www.c-sharpcorner.com/blogs/why-we-use-properties-in-c-sharp1
https://www.w3schools.com/cs/cs_properties.asp
https://www.c-sharpcorner.com/UploadFile/3d39b4/property-in-C-Sharp/#:~:text=Read%20only%20means%20that%20we,t%20have%20a%20set%20accessor.