Author - StudySection Post Views - 44 views
c#

Quick Introduction to C# or C Sharp

Introduction

Categories of variables in C# are described into the following types −

  • Value types: Value type variables can be assigned a value directly and they are derived from the class System.ValueType. The value types directly contain data. Examples are char, int, and float, which stores numbers, alphabets, and floating-point numbers, respectively.
  • Reference types: Actual data is not stored in reference types variable, but they contain a reference to the variables. In other words, they refer to a memory location. Examples of built-in reference types are the object, string, and dynamic.
  • Pointer types: The memory address of another type is stored in pointer-type variables. Two symbols ampersand (&) and asterisk (*) are there to get the pointer details:
    • ampersand (&): It is Known as Address Operator. It is used to determine the address of a variable.
    • asterisk (*): It is also known as an Indirection Operator. It is used to access the value of an address.
  • Class: A class is a blueprint of a specific object.
    • Modifiers: A class can be public or internal etc and by default the modifier of the class is internal.
    • Keyword class: To declare the class, the class keyword is used.
    • Class Identifier: The identifier(or name of the class) should begin with an initial letter which should be capitalized by convention.
    • Body: The class body is surrounded by { } (curly braces).
  • Method: Methods are generally the block of codes or statements in a program that gives the user the ability to reuse the same code which ultimately saves the excessive use of memory. So basically, a method is a collection of statements that perform some specific tasks and return the result to the caller. A method can also perform some specific tasks without returning anything.
  • Variable: The variable in C# is nothing but a name given to a data value or to a memory location and all the operations done on the variable effects that memory location. In C#, all the variables must be declared before they can be used and it is the basic unit of storage in a program. During program execution, the value stored in a variable can be changed.
  • Array :An array is a group of like-typed variables that are referred to by a common name and each data item is called an element of the array. The data types of the elements may be any valid data type like int, char, float, etc. and the elements are stored in a contiguous location. Length of array specifies the number of elements present in the array. In C#, the allocation of memory for the arrays is done dynamically. Each array has an index beginning from 0 and the variables in the array are ordered.

Compiling and Executing the Program using CLI:-

  1. Open a text editor and write the program
  2. Save the file with nameoffile.cs extension.
  3. Open the command prompt tool and go to the directory where you saved the file.
  4. Type csc nameoffile.cs and press enter to compile your code.
  5. If there are no errors in your code, the command prompt takes you to the next line and generates nameoffile.exe executable file.
  6. Type nameoffile to execute your program.
  7. You can see the output on the screen.

Compiling and Executing the Program using Visual Studio

Start Visual Studio.

  1. On the menu bar, choose File -> New -> Project.
  2. Choose Visual C# from templates, and then choose Windows.
  3. Choose Console Application.
  4. Specify a name for your project and click the OK button.
  5. This creates a new project in Solution Explorer.
  6. Write code in the Code Editor.
  7. Click the Run button or press the F5 key to execute the project. A Command Prompt window appears that contains your output.

Method Overloading

Method Overloading is a type of polymorphism. It is also called “Static Polymorphism” or “Compile Time Polymorphism”. You can not define more than one method with the same name and the type of the arguments. It would be a compiler error.
Method overloading can be done by changing:

  1. The number of parameters in two methods.
  2. The data types of the parameters of methods.
  3. The Order of the parameters of methods

Method Overriding

Method Overriding is a type of polymorphism. It is also called “Dynamic Polymorphism” or “Run-Time Polymorphism”. Method Overriding is a technique that allows the invoking of functions from another class (base class) into the derived class. Creating a method in the derived class with the same signature as in the base class is called method overriding.

Interfaces

Interfaces will contain only the declaration of the members. The implementation of interface members will be given by the class who implements the interface implicitly or explicitly.

  • Interfaces specify what a class must do.
  • Interfaces can’t have private members.
  • By default, the members of Interface are public and abstract.
  • The interface will always be defined with the help of the keyword ‘interface‘.
  • Multiple inheritance is possible only with the help of Interfaces but not with classes.

Loops:

Looping in a programming language is a way to execute a statement or a set of statements multiple times depending on the result of the condition to be evaluated to execute statements.

Loops are mainly divided into two categories:

  • Entry Controlled Loops: The loops in which the condition to be tested is present at the beginning of the loop body are known as Entry Controlled Loops. Example: For loop and While loop
  • Exit Controlled Loops: The loops in which the condition to be tested is present at the end of the loop body are termed as Exit Controlled Loops.Example: do-while

Conditional statements:

C# has the following conditional statements:

  • Use if to specify a block of code which is executed, if a specified condition is true.
    if (condition)
    {
    // block executed if the condition is True
    }
  • Use else to specify a block of code which is executed, if the same condition is false
    if (condition)
    {
    // block executed if the condition is True
    }
    else
    {
    // block executed if the condition is False
    }
  • Use else if to specify a new condition to test, if the first condition is false, then this block of code is evaluated.

    if (condition1)
    {
    // block executed if condition1 is True
    }
    else if (condition2)
    {
    // block executed if the condition1 is false and condition2 is True
    }
    else
    {
    // block executed if the condition1 is false and condition2 is False
    }

  • Use the switch to specify many alternative blocks of code to be executed in a program
    switch(expression)
    {
    case x:
    // code block
    break;
    case y:
    // code block
    break;
    default:
    // code block
    break;
    }

Inheritance:

It is a mechanism by which one class is allowed to inherit the features(fields and methods) of another class.

  • Super Class: The class whose features are inherited is known as superclass and is also called a base class or a parent class.
  • Sub Class: The class that inherits the other class is known as a subclass and is also called a derived class, extended class, or child class. The subclass can add its own methods and fields in addition to the superclass fields and methods.
  • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want then we can derive our new class from the existing class. By doing this, we are reusing the methods and fields of the existing class.

Final:

In c# There is no keyword like “final” but the same thing is achieved by the keyword “sealed“.
A sealed class cannot be inherited.

Static:

Static is a modifier in C# which can be used with the following:

  • Classes: A static class can only contain static data members, static methods, and a static constructor. We cannot create objects of the static class. Static classes are sealed i.e. one cannot inherit a static class from another class
  • Variables: When a variable is declared as static, then a single copy of the variable is created and shared among all objects at the class level.
  • Methods: Static methods are accessed with the name of the class. A static method can access static and non-static fields, static fields are directly accessed by the static method without class name whereas non-static fields require objects.
  • Constructor: Static Constructor is invoked only once in the class and it is invoked during the creation of the first reference to a static member in the class. A static constructor is used to initialize the static fields or data of the class and to be executed only once.
    1. It can’t be called directly.
    2. When it is executing, then the user has no control.
    3. It does not take access modifiers or any parameters.
    4. It is called automatically when we initialize the class before the first instance is created.

Being the most extensively used JavaScript library, a jQuery Certification will add enormous value to your skill-set. jQuery provides various functionalities to the developer in order to develop complex applications with ease and efficiency.

Leave a Reply

Your email address will not be published. Required fields are marked *