In this article we will learn classes and objects in C# programming.
Learn C# Step By Step :- Part 1.
Learn C# Step By Step: - Part 2
Learn C# Part 4: Synchronous and Asynchronous methodology.
Learn C# Part 4b: Delegates, Multicast Delegates and Events.
Learn C# Part 5: Mini project - File Search.
Learn C# Part 6: Delegates - Lambda Expression, Expression Tree, Func<>, Action<> and Predicate<>
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.
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.
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.
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.
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)
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
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
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
9) Write this statement
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
Console.WriteLine(“Name” + obj.Name + “Age” + obj.Age);
or it will display the output written under the braces of ELSE condition which will ask to fill in the correct values.
Console.WriteLine(“Enter Correct Values”);
11) Rest other code will remain same including the exception code.
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.
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.
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.
Below is the nice video explaining start-up topic for learning C# programming to complete fresher in IT field: -