C# Basic Concept and Overview

 Table of Contents:

  1. Introduction
  2. Program Structure
  3. Data Types
  4. Variables
c# basic concepts

Introduction

C# is a modern, general-purpose, object-oriented programming language developed by Microsoft and approved by European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO). 

C# was developed by Anders Hejlsberg and his team during the development of .Net Framework. C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows use of various high-level languages on different computer platforms and architectures.

C# is a popular programming language that is used to develop a wide range of applications, including web, mobile, desktop, and games. It is a modern, object-oriented language that is designed to be easy to use and read.

The following reasons make C# a widely used professional language –

  • It is a modern, general-purpose programming language.
  • It is object oriented.It is component oriented.
  • It is easy to learn.
  • It is a structured language.
  • It produces efficient programs.
  • It can be compiled on a variety of computer platforms.
  • It is a part of .Net Framework.

Here are some basics to get you started with C#:

  • C# is a case-sensitive language, which means that variables and keywords are treated differently depending on the case of their letters.
  • A C# program consists of one or more classes, which contain data and methods that operate on that data.
  • A C# program starts execution with the main method, which is the entry point of the program.
  • C# uses data types to represent different kinds of data, such as integers, floating-point numbers, and strings.
  • Variables are used to store data in a C# program. They must be declared with a specific data type, and they can be assigned a value when they are created or at a later point in the program.
  • Operators are used to perform operations on data in a C# program. These include arithmetic operators (such as + and -), comparison operators (such as > and <), and logical operators (such as && and ||).
  • Control structures, such as loops and conditional statements, are used to control the flow of execution in a C# program.
  • C# supports object-oriented programming, which means that it has features such as inheritance, polymorphism, and encapsulation.

C# Program Structure

Here is the of program structure. It is demonstrated using example by creating Hello World program. It is the basic footstep in learning programming.

Example;

  public class HelloWorld {
       public static void Main(string[] args) {
            Console.WriteLine("Hello World");
      }
   }
      

The main method in C# is defined as a static method because it is the entry point of the program and it needs to be accessible without creating an instance of the class in which it is defined.

The ‘static’ keyword indicates that the Main method is a static method, and the void keyword indicates that it does not return a value.


In addition to being static, the ‘main’ method must also be marked with the public access modifier, which makes it accessible to the runtime when the program starts.

 

I hope this helps clarify why the ‘main’ method is defined as a static method in C#! Let me know if you have any further questions.

Data Types

C# is a strongly-typed language. It means we must declare the type of a variable that indicates the kind of values it is going to store, such as integer, float, decimal, text, etc.

The following declares and initialized variables of different data types.

string str = "Hello World!!";
int num = 100;
float f = 10.2f;
char c = 'A';
bool b = true; 
      

Let us see with the example and know how to use data types in programming.

  class Program
  {
    static void Main(string[] args)
    {
      int myNum = 5;               // integer (whole number)
      double myDoubleNum = 5.99d;  // floating point number
      char myLetter = 'D';         // character
      bool myBool = true;          // boolean
      string myText = "Hello";     // string
      Console.WriteLine(myNum);
      Console.WriteLine(myDoubleNum);
      Console.WriteLine(myLetter);
      Console.WriteLine(myBool);
      Console.WriteLine(myText);
    }
  }
      

Variables

A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable's memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable.

Defining Variables

Syntax for variable definition in C# is −

  	<data_type_name> <variable_name>
  

Here, data_type must be a valid C# data type including char, int, float, double, or any user-defined data type, and variable_list may consist of one or more identifier names separated by commas.

Some valid variable definitions are shown here −

int i, j, k;
char c, ch;
float f, salary;
double d;
      

Initializing Variables

Variables are initialized (assigned a value) with an equal sign followed by a constant expression. The general form of initialization is −

  	variable_name = value;
  

Variables can be initialized in their declaration. The initializer consists of an equal sign followed by a constant expression as −

  	<data_type_name> <variable_name> = value;
  

Some examples are −

int d = 3, f = 5;    /* initializing d and f. */
byte z = 22;         /* initializes z. */
double pi = 3.14159; /* declares an approximation of pi. */
char x = 'x';        /* the variable x has the value 'x'. */
      

It is a good programming practice to initialize variables properly, otherwise sometimes program may produce unexpected result.

The following example uses various types of variables −


Let us see with the example and know how to use variables in programming.

Example,

   public class Program {
     public static void Main(string[] args) {
         short a;
         int b ;
         double c;

         /* actual initialization */
         a = 10;
         b = 20;
         c = a + b;
         Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
         Console.ReadLine();
      }
   }
      

Post a Comment

Previous Post Next Post