C#
C# and .Net Version History
In this tutorial, you will learn- .Net Framework Version History C# Version History .Net Framework...
In this tutorial, you will learn-
Access Modifiers or Access Specifiers are used to define the visibility of a class property or method. There are times when you may not want other programs to see the properties or the methods of a class. In such cases, C# gives the ability to put modifiers on class properties and methods. The class modifiers can restrict access so that other programs cannot see the properties or methods of a class.
There are 3 types of access modifiers. They are explained below.
When this access specifier is attached to either a property or a method, it means that those members cannot be accessed from any external program.
Let's take an example and see what happens when we use the private access modifier.
Let's modify the current code in our Tutorial.cs file. In the SetTutorial method, let's change the public keyword to private.
Now let's switchover to our Program.cs file. You will notice that there is a red squiggly line under the SetTutorial method.
Since we have now declared the SetTutorial method as private in our Tutorial class, Visual Studio has detected this. It has told the user by highlighting it that now this method will not work from the Program.cs file.
When this access modifier is attached to either a property or a method, it means that those members can be accessed from any external program. We have already seen this in our earlier examples.
Since we have defined our methods as public in the Tutorial class, they can be accessed from the Program.cs file.
When this access modifier is attached to either a property or a method, it means that those members can be accessed only by classes inherited from the current class. This will be explained in more detail in the Inheritance class.
Constructors are used to initializing the values of class fields when their corresponding objects are created. A constructor is a method which has the same name as that of the class. If a constructor is defined in a class, then it will provide the first method which is called when an object is created. Suppose if we had a class called Employee. The constructor method would also be named as Employee().
The following key things need to be noted about constructor methods
Let's now see how we can incorporate the user of constructors in our code. We will use constructors to initialize the TutorialID and TutorialName fields to some default values when the object is created.
Step 1) The first step is to create the constructor for our Tutorial class. In this step, we add the below code to the Tutorial.cs file.
Code Explanation:-
Now let's switchover to our Program.cs file and just remove the line, which calls the SetTutorial method. This is because we want just to see how the constructor works.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Tutorial
{
public int TutorialID;
public string TutorialName;
public Tutorial()
{
TutorialID=0;
TutorialName="Default";
}
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();
Console.WriteLine(pTutor.GetTutorial());
Console.ReadKey();
}
}
}Code Explanation:-
If the above code is entered properly and the program is executed, the following output will be displayed.
Output:
From the output, we can see that the constructor was indeed called and that the value of the TutorialName was set to "Default".
Note: Here the value "default" is fetched from the constructor.
Summary
In this tutorial, you will learn- .Net Framework Version History C# Version History .Net Framework...
C# is based on the C++ programming language. Hence, the C# programming language has in-built...
In our previous tutorial, we have learned about how we can use arrays in C#. Let's have a quick...
$20.20 $9.99 for today 4.5 (114 ratings) Key Highlights of C# Tutorial PDF 243+ pages eBook...
What is Abstract Class in C#? Abstract Class can never be instantiated and is marked by the...
What are Data Types in C#? The C# language comes with a set of Basic data types. These data types are...