C# - Basic Syntax

C# supports object-oriented features like Class, Object, Encapsulation, Polymorphism, Data abstraction, Inheritance etc. In Object-Oriented Programming technology, we works on real time entity(known as object), these entity related to each other also communicate by message passing. If we discuss about class, a class consists of data member and member function (known as encapsulation). Using Object we can call method also initialize class data members. In later session we will discuss about object oriented features. In this article we will design basic structure of class and object.For example, let us consider a circle object. It has attributes like radius. Depending upon the design, it may need ways for accepting the values of this attribute, calculating area and display details.

Let us look at an implementation of a circle class and discuss C# basic syntax, on the basis of our observations in it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace circle
    class circle_area
         private static float pi = 3.14F;
         private int radius;
        // initialize datamember in the constructor.
         public circle_area(int radi)
             radius = radi;

        // calcuate area of the circle.
         public double getarea()
             return pi * radius * radius;

    class Program
        static void Main(string[] args)
            circle_area ca = new circle_area(5);

When the above code is compiled and executed, it produces the following result:
circle area with 5 radius - output
The First line of the program define the system Namespace, a single program can hold multiple using statement. The System Namespace consists of many class like Console class. The Second line of the program hold NameSpace declaration, according to my previous article a single Namespace can contain multiple classes, in this program we have two class first one is circle_area and second one is program. In later session we will discuss, how a Namespace consists of another Namespace, deligates and structure. The third line of the program define class declaration, In this program we have two classes and each class hold at least single member function.

Comments in C#

Comments are used for designing user friendly program. Compilers ignore the comment entries. we can use single line as well as multi-line comments in the c# program The multi-line comments in C# programs start with /* and terminates with the characters */ as shown below:

/* Program define how to
calculate circle area with static keyword*/
Single-line comments are declared by double slash like //, according to above mentioned code, which is
// initialize Data Member in the constructor.

Data Member

It shows the properties of an object, suppose you have a object employee so create a class, which covers employee details like Employee_id, Employee_Name, etc. These properties known as data members. In this example we have two properties for circle class, these are pi and radius. Basically these variable are used for storing data.

Member Functions

It shows the behavior of the class, in which we can perform some action related to object properties. In this example we have one member function getarea(), which returns calculated area in double type.

Class and how to declare it:

Class is a user defined data type or you can say it is a composite data type. we have many definition of a class like "Class is a container of data member and member function" and second one is "class is a instance of a variable". 
Design a new class in c#- synatx of class
class class_name 
// Data member;
// Member function;    

Ok, Now understand what is in this program:
First start with main () method because its the entry point of the program. Here we have a object , which is created by new keyword. Also assign the instance name , which is ca. According to constructor theory, When we create a object ,constructor call automatically. Assign the Data Member by the constructor.  
 After assign the data member we can perform some operation on it. Now, we calculate area of the circle using getarea() method. This method is called from object ca.


Popular posts from this blog

difference between structure and union in C Language

Difference between Linear search and Binary Search in c language

Difference between static and dynamic websites