CS Basic Syntax

C # is a programming language that focuses on objects. The program consists of several objects that interact through actions in object - oriented programming methodology. Methods are the actions an object can take. Objects of the same type, or in the same class, are said to be of the same type.

Let us consider, for example, an object of the Rectangle. It has attributes like width and length. Depending on the design, the values of these attributes may be acceptable, the area calculations and the details displayed.

Let's look at the rectangular class implementation and discuss fundamental C# syntax



Rectangle Class Code

using System;

namespace RectangleApplication {
   class Rectangle {
      // member variables
      double length;
      double width;
      public void Acceptdetails() {
         length = 5.5;    
         width = 4.5;
      public double GetArea() {
         return length * width; 
      public void Display() {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
   class ExecuteRectangle {
      static void Main(string[] args) {
         Rectangle rec = new Rectangle();

The following result is obtained when the above code is compiled and executed



In any C # programme, the first statement is

using System;

The using keyword is used to add namespaces to the program. Multiple statements can be made in a program.


For declaring a class, the class keyword is used. The class keyword define the class Rectangle (above). The braces ({}), known as delimiters, are used to indicate start and end of a class body.


Comment are apart of the program and are used to explain code. Compliers ignor comment entries. The following symbols are used to give a comment entry in C#.

// Comment text

In C# programs, the multiline comments start by /* and end with */ as shown below.

/* This is a sample programto display informtion */



Variables are used to store the data. Variables are the memory where we will strore data. It act as a container and will store the data which can vary at run time. In the preceding code, class Rectangle has two variables called length and width. These member variables are used for storing the data for an Rectangle.

A function are known as building block of a program, these are set of statement that performs a specific task in response to a message. The functions of a class are called member function in C#. Member function are declared inside a class. The function declaration introduces the function in the class and the function definition contains the function code.

To use class and the members of a class, a developer need to create an object of the class. Objecs interact with each other by passing messages and by responding to the received messages. Objects use methods to pass messages. In C#, the task of passing messages can be accomplished by using member functions. All the objects of a class share the same copy of the member functions but they maintain a separate copy of the member variables in memory. All the objects of a class share the same copy of the member functions but
they maintain a separate copy of the member variables in memory. The sharing of member functions and non sharing of member variables of classes and objects.

An identifier is a name used for the identification of a class, variable, function or other user - defined item. The following are the basic rules to name classes in C#:

  • The first character of the identifier can not be a digit. A name must begin with a letter that can be followed by the letter sequence, digits (0 to 9) or underline.
  • There should be no embedded space or symbol, for example - + ! @ # % ^ & * ( ) [ ] { } . ; : " ' / and \ . An underscore (_) may be used, however.
  • It's not a keyword of C#.