SlideShare a Scribd company logo
1 of 11
www.learncsharptutorial.com
Learn Concept of Class and Object in C#
In this article we will learn classes and objects in C# programming.
Till now in the past two articles we have seen all the labs which was using functional programming. From now in coming all the
articles we will do the programming using classes and objects. As this is professional approach of doing the programming. With
classes and objects approach, code it reduces code reading complexity and it improves readability and also offers re-usability.
Concept of Class and Object
In simple words class is the general form like a person who becomes an employee which can be termed as class person or class
employee in programming world.
While an object is more specific which addresses real world. By adding or removing characteristic of class, different objects can
be created.
Consider the below example where it is shown a person or employee as a class template and three employees or persons as
objects by varying their skill set(characteristic) their position or designation can be changed. If a person is just normal employee
is going to be a worker. If an employee who has skills of typing than he/he can became typist. Similarly if am person has skill set
of management than he is manager.
www.learncsharptutorial.com
Learn Concept of Class and Object in C#
So we will consider same employee as a class and the worker, typist and manager are multiple objects using employee as class
blueprint. Employee class will have basic properties Name, Age, Address and Contact Number. While objects created of employee
class will have all the properties of it and will also have its own properties which makes them unique different objects of the real
world. So prime use of class and object concept is generating reusability of the code.
www.learncsharptutorial.com
Learn Concept of Class and Object in C#
Now after understanding concept of class and object next we will revamp our following existing functional code programming
and transform it into class and object.
www.learncsharptutorial.com
Learn Concept of Class and Object in C#
In order to change existing code to class and object do the following changes as mentioned in below steps: -
1) Create class “Person” outside and below to the end scope of class program delete all existing code written within try braces.
2) Declare two variables one as “public string Name” and other “public int Age”. Let us learn more about syntax first word is an
access modifier set to “public” and can be accessed outside the class person which means it can even be accessed from “class
program”. Second word of the syntax is “string” which is a data type which accepts alphanumeric and numeric characters.
Third word is the variable name “Name” and this variable should be declared with accessor, here declared with get & set
accessor within the braces. Similarly following syntax line is of public int Age with get & set accessor within the braces.
www.learncsharptutorial.com
Learn Concept of Class and Object in C#
3) Next line is “public bool Valid()” method which will validate that value given by the user through the keyboard is valid or not.
In this method we will first will validate using IF condition whether Name entered by user is containing some characters i.e. it is
not empty. So the statement is
If (Name.Length == 0)
{
return false;
}
If it does not contain values then it should treated false and program execution further will be stopped. As data type for this
method is Boolean which means value returned would be true or false. With True program will execute further line of the code.
Next will write statement which will validate value entered in the Age field. If value is entered greater than 100 it will return
false value and program will stop executing further and vice-versa.
4) Line written after the end brace of IF condition is to keep program moving further if the values are true and denoted by the
text written “return true”.
5) Now come to class program under the braces of try statement delete existing code and write first line of code to create the
object of the class person. So write the statement as
www.learncsharptutorial.com
Learn Concept of Class and Object in C#
Person obj = new Person()
Which will create a class Person object denoted by “obj”. Now as we have created object “obj” we can use all the variables
created for class person.
6) Write a line which console application will prompt user to enter name, the code line goes this way
Console.WriteLine(“Enter Name”);
Please Note: Do not forget to include semi colon at the end of statement or else there will be compilation error seen.
7) Text entered by user will be then read and displayed on the console screen prompt with this statement
obj.Name = Console.ReadLine();
8) Next write a line console application will prompt user to enter name, the code line goes this way
Console.WriteLine(“Enter Age”);
9) Write this statement
www.learncsharptutorial.com
Learn Concept of Class and Object in C#
obj.Age = Convert.ToInt16(Console.ReadLine());
to keep displaying value entered by user. As the text accepted from user is “string” form and Age variable accept only “int” data
type so for that there is need of conversion of data type from “string” to “int”.
10) Next is to write the IF condition by passing obj.Valid() method as parameter which will check for input value entered by the
user in both the Name and Age variable. If found things correct it will display the output, below is the display output statement
or it will display the output written under the braces of ELSE condition which will ask to fill in the correct values.
Console.WriteLine(“Name” + obj.Name + “Age” + obj.Age);
11) Rest other code will remain same including the exception code.
Console.WriteLine(“Enter Correct Values”);
www.learncsharptutorial.com
Learn Concept of Class and Object in C#
www.learncsharptutorial.com
Learn Concept of Class and Object in C#
Now rebuilt the code to check whether any error found during compilation. After the build is processed it will show the build
succeeded message at the bottom as shown in the following image.
www.learncsharptutorial.com
Learn Concept of Class and Object in C#
Now press ctrl+F5 on the keyboard and check the execution of the program on the console application.
Once the console application is opened it will first ask you to make entry no. of records which you want to display, here will enter
3 records.
After entering “Name” and “Age” of each record it will display output for each record until it finishes three records input.
www.learncsharptutorial.com
So our agenda of this article for converting existing code of functional programming to class-object model is transformed
successfully. Hope each steps of transformation is clear suggesting all reader to practice above steps on their own which will help
them to get topic practically understood.
With this lab we also have following learning video for you from our
fresher’s Learn C# is 100 hrs series: -
Learn Concept of Class and Object in C#

More Related Content

What's hot

Programming in c++
Programming in c++Programming in c++
Programming in c++
Baljit Saini
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 

What's hot (20)

Programming in c++
Programming in c++Programming in c++
Programming in c++
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]
 
class and objects
class and objectsclass and objects
class and objects
 
Oop in c++ lecture 1
Oop in c++  lecture 1Oop in c++  lecture 1
Oop in c++ lecture 1
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programming
 
Object Oriented Programming using C++ Part I
Object Oriented Programming using C++ Part IObject Oriented Programming using C++ Part I
Object Oriented Programming using C++ Part I
 
Inheritance
InheritanceInheritance
Inheritance
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
C# Summer course - Lecture 2
C# Summer course - Lecture 2C# Summer course - Lecture 2
C# Summer course - Lecture 2
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
Introduction to c ++ part -2
Introduction to c ++   part -2Introduction to c ++   part -2
Introduction to c ++ part -2
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 

Similar to Learn Concept of Class and Object in C# Part 3

New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
rashmita_mishra
 
M.tech oops through_c++_labmanual1 (1)
M.tech oops through_c++_labmanual1 (1)M.tech oops through_c++_labmanual1 (1)
M.tech oops through_c++_labmanual1 (1)
yuvanalagadapati
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
mwillmer
 

Similar to Learn Concept of Class and Object in C# Part 3 (20)

Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
New features in C# 6
New features in C# 6New features in C# 6
New features in C# 6
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
Chapter3: fundamental programming
Chapter3: fundamental programmingChapter3: fundamental programming
Chapter3: fundamental programming
 
EEE 3rd year oops cat 3 ans
EEE 3rd year  oops cat 3  ansEEE 3rd year  oops cat 3  ans
EEE 3rd year oops cat 3 ans
 
C questions
C questionsC questions
C questions
 
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
 
Intro To C++ - Class 05 - Introduction To Classes, Objects, & Strings
Intro To C++ - Class 05 - Introduction To  Classes, Objects, & StringsIntro To C++ - Class 05 - Introduction To  Classes, Objects, & Strings
Intro To C++ - Class 05 - Introduction To Classes, Objects, & Strings
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Cordovilla
CordovillaCordovilla
Cordovilla
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
csharp.docx
csharp.docxcsharp.docx
csharp.docx
 
Object Oriented Programming (Advanced )
Object Oriented Programming   (Advanced )Object Oriented Programming   (Advanced )
Object Oriented Programming (Advanced )
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
 
M.tech oops through_c++_labmanual1 (1)
M.tech oops through_c++_labmanual1 (1)M.tech oops through_c++_labmanual1 (1)
M.tech oops through_c++_labmanual1 (1)
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
 
Oops concepts in c++ documentation
Oops concepts in c++ documentationOops concepts in c++ documentation
Oops concepts in c++ documentation
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

Learn Concept of Class and Object in C# Part 3

  • 1. www.learncsharptutorial.com Learn Concept of Class and Object in C# In this article we will learn classes and objects in C# programming. Till now in the past two articles we have seen all the labs which was using functional programming. From now in coming all the articles we will do the programming using classes and objects. As this is professional approach of doing the programming. With classes and objects approach, code it reduces code reading complexity and it improves readability and also offers re-usability. Concept of Class and Object In simple words class is the general form like a person who becomes an employee which can be termed as class person or class employee in programming world. While an object is more specific which addresses real world. By adding or removing characteristic of class, different objects can be created. Consider the below example where it is shown a person or employee as a class template and three employees or persons as objects by varying their skill set(characteristic) their position or designation can be changed. If a person is just normal employee is going to be a worker. If an employee who has skills of typing than he/he can became typist. Similarly if am person has skill set of management than he is manager.
  • 2. www.learncsharptutorial.com Learn Concept of Class and Object in C# So we will consider same employee as a class and the worker, typist and manager are multiple objects using employee as class blueprint. Employee class will have basic properties Name, Age, Address and Contact Number. While objects created of employee class will have all the properties of it and will also have its own properties which makes them unique different objects of the real world. So prime use of class and object concept is generating reusability of the code.
  • 3. www.learncsharptutorial.com Learn Concept of Class and Object in C# Now after understanding concept of class and object next we will revamp our following existing functional code programming and transform it into class and object.
  • 4. www.learncsharptutorial.com Learn Concept of Class and Object in C# In order to change existing code to class and object do the following changes as mentioned in below steps: - 1) Create class “Person” outside and below to the end scope of class program delete all existing code written within try braces. 2) Declare two variables one as “public string Name” and other “public int Age”. Let us learn more about syntax first word is an access modifier set to “public” and can be accessed outside the class person which means it can even be accessed from “class program”. Second word of the syntax is “string” which is a data type which accepts alphanumeric and numeric characters. Third word is the variable name “Name” and this variable should be declared with accessor, here declared with get & set accessor within the braces. Similarly following syntax line is of public int Age with get & set accessor within the braces.
  • 5. www.learncsharptutorial.com Learn Concept of Class and Object in C# 3) Next line is “public bool Valid()” method which will validate that value given by the user through the keyboard is valid or not. In this method we will first will validate using IF condition whether Name entered by user is containing some characters i.e. it is not empty. So the statement is If (Name.Length == 0) { return false; } If it does not contain values then it should treated false and program execution further will be stopped. As data type for this method is Boolean which means value returned would be true or false. With True program will execute further line of the code. Next will write statement which will validate value entered in the Age field. If value is entered greater than 100 it will return false value and program will stop executing further and vice-versa. 4) Line written after the end brace of IF condition is to keep program moving further if the values are true and denoted by the text written “return true”. 5) Now come to class program under the braces of try statement delete existing code and write first line of code to create the object of the class person. So write the statement as
  • 6. www.learncsharptutorial.com Learn Concept of Class and Object in C# Person obj = new Person() Which will create a class Person object denoted by “obj”. Now as we have created object “obj” we can use all the variables created for class person. 6) Write a line which console application will prompt user to enter name, the code line goes this way Console.WriteLine(“Enter Name”); Please Note: Do not forget to include semi colon at the end of statement or else there will be compilation error seen. 7) Text entered by user will be then read and displayed on the console screen prompt with this statement obj.Name = Console.ReadLine(); 8) Next write a line console application will prompt user to enter name, the code line goes this way Console.WriteLine(“Enter Age”); 9) Write this statement
  • 7. www.learncsharptutorial.com Learn Concept of Class and Object in C# obj.Age = Convert.ToInt16(Console.ReadLine()); to keep displaying value entered by user. As the text accepted from user is “string” form and Age variable accept only “int” data type so for that there is need of conversion of data type from “string” to “int”. 10) Next is to write the IF condition by passing obj.Valid() method as parameter which will check for input value entered by the user in both the Name and Age variable. If found things correct it will display the output, below is the display output statement or it will display the output written under the braces of ELSE condition which will ask to fill in the correct values. Console.WriteLine(“Name” + obj.Name + “Age” + obj.Age); 11) Rest other code will remain same including the exception code. Console.WriteLine(“Enter Correct Values”);
  • 9. www.learncsharptutorial.com Learn Concept of Class and Object in C# Now rebuilt the code to check whether any error found during compilation. After the build is processed it will show the build succeeded message at the bottom as shown in the following image.
  • 10. www.learncsharptutorial.com Learn Concept of Class and Object in C# Now press ctrl+F5 on the keyboard and check the execution of the program on the console application. Once the console application is opened it will first ask you to make entry no. of records which you want to display, here will enter 3 records. After entering “Name” and “Age” of each record it will display output for each record until it finishes three records input.
  • 11. www.learncsharptutorial.com So our agenda of this article for converting existing code of functional programming to class-object model is transformed successfully. Hope each steps of transformation is clear suggesting all reader to practice above steps on their own which will help them to get topic practically understood. With this lab we also have following learning video for you from our fresher’s Learn C# is 100 hrs series: - Learn Concept of Class and Object in C#