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
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructorsPraveen M Jigajinni
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]Rome468
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
Oop in c++ lecture 1
Oop in c++  lecture 1Oop in c++  lecture 1
Oop in c++ lecture 1zk75977
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#ANURAG SINGH
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programmingNeelesh Shukla
 
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 IAjit Nayak
 
C# Summer course - Lecture 2
C# Summer course - Lecture 2C# Summer course - Lecture 2
C# Summer course - Lecture 2mohamedsamyali
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - AbstractionMichael Heron
 
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)indiangarg
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented TechnologiesTushar B Kute
 
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)Michelle Anne Meralpis
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPTkishu0005
 

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

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 modulesDurgesh Singh
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsakreyi
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp PresentationVishwa Mohan
 
Chapter3: fundamental programming
Chapter3: fundamental programmingChapter3: fundamental programming
Chapter3: fundamental programmingNgeam Soly
 
C questions
C questionsC questions
C questionsparm112
 
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...NicheTech Com. Solutions Pvt. Ltd.
 
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, & StringsBlue Elephant Consulting
 
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
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++Amresh Raj
 
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 Portfoliomwillmer
 
Oops concepts in c++ documentation
Oops concepts in c++ documentationOops concepts in c++ documentation
Oops concepts in c++ documentationfarouq umar
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Philip Schwarz
 

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
 
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
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
 

Recently uploaded

Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 

Recently uploaded (20)

Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.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#