C#
How to Download and Install Visual Studio for C#
Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft. It is used...
C# is based on the C++ programming language. Hence, the C# programming language has in-built support for classes and objects. A class is nothing but an encapsulation of properties and methods that are used to represent a real-time entity.
For example, if you want to work with employee's data in a particular application.
The properties of the employee would be the ID and name of the employee. The methods would include the entry and modification of employee data.
All of these operations can be represented as a class in C#. In this chapter, we will look at how we can work with classes and objects in C# in more detail.
In this tutorial, you will learn-
Let's first begin with classes.
As we discussed earlier classes are an encapsulation of data properties and data methods.
To get a better understanding of class and objects, let's look at an example below of how a class would look like.
The name of the class is "Tutorial". The class has the following properties
A class also comprises of methods. Our class has the following methods,
Below is a snapshot of how an object might look like for our Tutorial class. We have 3 objects, each with their own respective TutorialID and TutorialName.
Let's now dive into Visual Studio to create our class. We are going to build upon our existing console application which was created in our earlier chapter. We will create a class in Visual Studio for our current application.
Let's follow the below-mentioned steps to get this example in place.
Step 1) The first step involves the creation of a new class within our existing application. This is done with the help of Visual Studio.
Step 2) The next step is to provide a name for the class and add it to our solution.
If the above steps are followed, you will get the below output in Visual Studio.
Output:-
A class named Tutorial.cs will be added to the solution. If you open the file, you will find the below code added to the class file.
Code Explanation:-
For the moment, our class file does not do anything. In the following topics, we will look into more details on how to work with the class.
We have already seen how fields and methods are defined in classes in the earlier topic.
For our Tutorial class, we can have the following properties.
Our Tutorial class can also have the below-mentioned methods.
Let's now see how we can incorporate fields and methods in our code.
Step 1) The first step is to ensure the Tutorial class has the right fields and methods defined. In this step, we add the below code to the Tutorial.cs file.
Code Explanation:-
Note: let's take an example and assume our Program.cs file calls the SetTutorial with the parameters "1" and ".Net". The below steps would be executed as a result of this,
Step 2) Now let's add code to our Program.cs, which is our Console application. The Console application will be used to create an object of the "Tutorial class" and call the SetTutorial and GetTutorial methods accordingly.
(Note:- An object is an instance of a class at any given time. The difference between a class and an object is that the object contains values for the properties.)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Tutorial
{
int TutorialID;
string TutorialName;
public void SetTutorial(int pID,string pName)
{
TutorialID=pID;
TutorialName=pName;
}
public String GetTutorial()
{
return TutorialName;
}
static void Main(string[] args)
{
Tutorial pTutor=new Tutorial();
pTutor.SetTutorial(1,".Net");
Console.WriteLine(pTutor.GetTutorial());
Console.ReadKey();
}
}
}Code Explanation:-
If the above code is entered properly and the program is run the following output will be displayed.
Output:
From the output, you can clearly see that the string ".Net" was returned by the GetTutorial method.
Summary
Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft. It is used...
What is ArrayList in C#? The ArrayList collection is similar to the Arrays data type in C#. The...
Flow Control and conditional statements Flow control and conditional statements are available in...
C# is a general purpose programming language which encompasses various disciplines like...
In this tutorial, you will learn- Access Modifiers Constructor Access Modifiers Access Modifiers or...
What is Queue in C#? The Queue is a special case collection which represents a first in first out...