Skip to main content

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);
            Console.WriteLine(Convert.ToString(ca.getarea()));
            Console.ReadKey();
        }
    }
}


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.

Comments

Popular posts from this blog

difference between structure and union in C Language

In c language article we will see the difference between union and structure. Both are the user define datatype in c language. See the table which is mentioned below: ASP.NET Video Tutorial Series Structure Union1.The keywordstruct is used to define a structure 1. The keyword union is used to define a union. 2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum ofsizes of its members. The smaller members may end with unused slack bytes. 2. When a variable is associated with a union, thecompiler allocates thememory by considering the size of the largest memory. So, size of union is equal to the size of largest member. 3. Each member within a structure is assigned unique storage area of location. 3. Memory allocated is shared by individual members of union. 4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset v…

Difference between Linear search and Binary Search in c language

SQL Video Channel : Download all SQL Video



Binary Search Linear Search Works only on sorted items. such as  1,2,3,4,5,6  etc
Works on sorted as well as unsorted items. 12,4,5,3,2,1 etc Very efficient if the items are sorted Very efficient if the items are less and present in the beginning of the list. such as Suppose your list items are : 12,3,4,5,1 and you want to search 12 number then you get beginning in the list. Works well with arrays and not on linked lists. Works with arrays and linked lists.
Number of comparisons are less More number of comparisons are required if the items are present in the later part of the array or its elements are more.

Memory representation of Linked List Data Structures in C Language

Memory representation of Linked List

             In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.

               Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:


      Suppose next node is allocated at an address 506, so the list becomes,



  Suppose next node is allocated with an address with an address 10,s the list become,


The other way to represent the linked list is as shown below:




 In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of next node. The last node …