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

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Recently uploaded (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

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#