The above method takes no parameter and returns nothing. It only prints the Current Date and Time on the console using the DateTime Class in the System namespace.
Instantiating the class
In C# a class is instantiated (making its objects) using the new keyword.
You can also declare the reference and assign an object to it in different steps. The following two lines are equivalent to the above line
Note that is very similar to using implicit data types except for the object is created with the new operator while implicit data types are created using literals
Another important thing to understands is the difference between reference and object. The line
only declares the reference the Student of type Student which at this point does not contain any object (and points to the default null value) so if you try to access the members of class (Student) through it, it will throw a compile time error 'Use of unassigned variable theStudent'. When we write
then a new object of type Student is created at the heap and its reference (or handle) is given to theStudent. Only now is it legal to access the members of the class through it.
Accessing the members of a class
The members of a class (fields,methods and properties) are accessed dot '.' operator against the reference of the object like this:
Let's now make our Student class with some related field, methods and then instantiate it in the Main() method.
using System;
using System.Collections.Generic;
using System.Text;
namespace sachinchaudhary
{
class Student
{
// fields
string name;
int age;
int markofMaths;
int markofEnglish;
int markofScience;
int totalMarks = 300;
int obtainedMarks;
double percentage;
// methods
void CalculateTotalMarks()
{
obtainedMarks = markofMaths + markofEnglish + markofScience;
}
void CalculatePercentage()
{
percentage = (double)obtainedMarks / totalMarks * 100;
}
double GetPercentage()
{
return percentage;
}
// Main method or entry point of program
static void Main(string[] args)
{
// creating new instance of Student
Student st = new Student();
// setting the values of fields
st.name = "Nitin Sir";
st.age = 20;
st.markofEnglish = 80;
st.markofMaths = 99;
st.markofScience = 96;
// calling functions;
st.CalculateTotalMarks();
st.CalculatePercentage();
double stPercentage = st.GetPercentage();
// calling and retrieving value
// returned by the function
Student st1 = new Student();
st1.name = "Sachin Chaudhary";
st1.age = 18;
st1.markofEnglish = 77;
st1.markofMaths = 100;
st1.markofScience = 99;
st1.CalculateTotalMarks();
st1.CalculatePercentage();
double st1Percentage = st1.GetPercentage();
Console.WriteLine("{0} of {1} years age got {2}% marks",st.name,st.age,st.percentage);
Console.WriteLine("{0} of {1} years age got {2}% marks",st1.name, st1.age, st1.percentage);
}
}
}
Here, we started by creating an object of the Student class (st), we then assigned name, age and marks of the student. Later, we called methods to calculate totalMarks and percentage, then we retrieved and stored the percentage in a variable and finally printed these on a console window.
We repeated the same steps again to create another object of type Student, set and printed its attributes. Hence in this way, you can create as many object of the Student class as you want. When you compile and run this program it will display: