C# is an object-oriented programming language. Everything in C# is associated with classes and objects, along with its member fields and methods. C# allows us to create objects. To create an object, first of all, the object should be defined in the program which is done by creating a class. A class is a code template that is used to define a new object type. Within a class, the object’s member fields and methods are defined and when an object is created, its member fields and methods are determined by the class in which it is created.
Create Class
To create a class in C#, class keyword is used. It starts with the keyword followed by class name and a pair of curly braces { }. All its member fields and methods goes inside the braces:
1 2 3 4 5 6 7 |
//defining a class class className { access_specifier field; access_specifier method; }; |
access_specifier: It defines the access type of the class member fields and methods. There are four access specifier (public, protected, internal, private) which defines the six accessibility levels as follows:
- public
- protected
- internal
- protected internal
- private
- private protected
The following table shows the access to members permitted by each level:
Accessibility Level | public | protected | internal | protected internal | private | private protected |
---|---|---|---|---|---|---|
Entire Program | Y | N | N | N | N | N |
Containing Class | Y | Y | Y | Y | Y | Y |
Current Assembly | Y | N | Y | Y | N | N |
Derived Types | Y | Y | N | Y | N | N |
Derived Types within current Assembly | Y | Y | Y | Y | N | Y |
Create Object
To create an object, new keyword followed by class name must be assigned to a variable (object name). To access the member of the class, dot . operator is used. Please see the syntax below:
Syntax
1 2 3 4 5 6 7 8 |
//creating an object className objectName = new className(); //access class member (member fields and methods) objectName.field objectName.method(parameters) |
Example 1:
In the example below, a class (new object type) called Circle is created. An object called MyCircle of the class Circle is created in the main method. The object has only one integer field radius which is accessed in the main method using dot . operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; class Circle { //class field int radius = 10; static void Main(string[] args) { Circle MyCircle = new Circle(); Console.WriteLine(MyCircle.radius); } } |
The output of the above code will be:
10
Class Methods
Class method must be defined inside the class definition. It is defined in the same way as a normal method is defined in C#.
Example: Class method defined inside class
In the example below, a class method called area is defined inside the class Circle. A class method is accessed in the same way as a class field, i.e, using dot . operator. Here, the area() class method returns the area of the circle object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; class Circle { //class field int radius = 10; //class method public double area() { return 22/7.0*radius*radius; } static void Main(string[] args) { Circle MyCircle = new Circle(); Console.WriteLine(MyCircle.area()); } } |
The output of the above code will be:
1 2 3 |
314.285714285714 |
Example: Following is the example of creating objects in the c# programming language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; namespace cce { class Program { static void Main(string[] args) { Users user = new Users("Ton Black", 22); user.GetUserDetails(); Console.WriteLine("Press Enter Key to Exit.."); Console.ReadLine(); } } public class Users { public string Name { get; set; } public int Age { get; set; } public Users(string name, int age) { Name = name; Age = age; } public void GetUserDetails() { Console.WriteLine("Name: {0}, Age: {1}", Name, Age); } } } |