C# Hello World Program

// A Hello World! program in C#.
using System;
namespace HelloWorldExampleNamespace
{
    class HelloWorldClass
    {
        static void Main()
        {
            Console.WriteLine("Hello World!");

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

Static Class in #

A static class is the same as a non-static class, but a static class cannot be instantiated. In other words, you cannot use the new keyword to create an object of the class type. The static keyword in the C# programming language allows you to define static classes and static members. A static class is similar to a class that is both abstract and sealed. To declare a class as static, you should mark it with the static keyword in the class declaration.



Syntax of Static Class


static class classname
{
    //static data members
   //static methods
}



Example of Static Class

using System;
namespace StaticClassExampleNameSpace
{
    public static class StaticClassExamleClass
    {
        public static double CelsiusToFahrenheit(string temperatureCelsius)
        {
            // Convert argument to double for calculations.
            double celsius = Double.Parse(temperatureCelsius);
            // Convert Celsius to Fahrenheit.
            double fahrenheit = (celsius * 9 / 5) + 32;
            return fahrenheit;
        }

        public static double FahrenheitToCelsius(string temperatureFahrenheit)
        {
            // Convert argument to double for calculations.
            double fahrenheit = Double.Parse(temperatureFahrenheit);
            // Convert Fahrenheit to Celsius.
            double celsius = (fahrenheit - 32) * 5 / 9;
            return celsius;
        }
    }
}


Variable in c#

The variable in C# is nothing but a name given to a data value. 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.

Example

using System;
namespace VariableExampleNameSpace
{
    public class StudentClass
    {
        private int RollNo; ///private variable declaration
        public int EnrolNo; ///public variable declaration
        public void Percentage()
        {
            ///local variable declaration,
            int totalMarks = 360;
        }
    }
}


Array in c#


We can store multiple variables of the same type in an array data structure, so An array stores a fixed-size sequential collection of elements of the same type. A fixed-length array can store a predefined number of items. A dynamic array does not have a predefined size. The size of a dynamic array increases as you add new items to the array. You can declare an array of fixed length or dynamic.

Example of Array in c#:-

int[] array = new int[4];
array[0] = 10;
array[1] = 20;
array[2] = 30;
array[3] = 40;

More Examples of the array in c#

int[] intArray;  // can store int values
bool[] boolArray; // can store boolean values
string[] stringArray; // can store string values

Array Types in c#
1.      Single-dimensional arrays
2.      Multidimensional arrays or rectangular arrays
3.      Jagged arrays
4.      Mixed arrays.

Single Dimension Arrays
int[] intArray;
intArray = new int[3];

Multi-Dimensional Arrays
Array with more than one dimension is called multi- Dimensional. The form of a multi-dimensional array is a matrix.

Example:-
int[,] numbers = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };

Jagged Arrays

Jagged arrays are arrays of arrays.
int[][] intJaggedArray = new int[3][];
intJaggedArray[0] = new int[2]; 
intJaggedArray[1] = new int[4]; 
intJaggedArray[2] = new int[6]; 

Mixed Arrays
Mixed arrays are a combination of multi-dimension arrays and jagged arrays.



Data Types in c#

There are two kinds of types in C#: value types and reference types. Variables of value types directly contain their data whereas variables of reference types store references to their data. There are two kinds of types in C#: value types and reference types. Variables of value types directly contain their data whereas variables of reference types store references to their data.

Value types of data types in c#

1) sbyte
2) short
3) int
4) long
5) byte
6) ushort
7) uint
8) ulong
9) char
10) float
11) double
12) decimal ,bool
13) Enum types
14) Struct types
15) Nullable value types

Reference types of data types in c#
1.     Class types
2.      Interface types
3.      Array types
4.      Delegate types

C# History and Versions with Features


C# is pronounced as "C-Sharp". It is an object-oriented programming language provided by Microsoft that runs on .Net Framework and Anders Hejlsberg is known as the founder of C# language.


C# version with .NET framework and CLR version with Visual Studio.


1.      C# 1.0.NET framework 1.0,1.1 , CLR version 1.0, Visual Studio 2002.

2.      C# 2.0 .NET framework 2.0 , CLR version 2.0, Visual Studio 2005.

3.      C# 3.0 .NET framework 3.0,3.5 , CLR version 2.0, Visual Studio 2008.

4.      C# 4.0 .NET framework 4.0 , CLR version 4.0, Visual Studio 2010.

5.      C# 5.0 .NET framework 4.5 , CLR version 4.0, Visual Studio 2012,2013.

6.      C# 6.0 .NET framework 4.6 , CLR version 4.0 ,Visual Studio 2013,2015.

7.      C# 7.0 .NET framework 4.6,4.6.1,4.6.2 , CLR version 4.0, Visual Studio 2015, 2017 RC.





C# version with Features



C# 1.0(.NET Framework 1.0/1.1Visual Studio .NET 2002)

  • Basic features


C# 2.0.NET Framework 2.0(Visual Studio 2005)

  • Generics
  • Partial types
  • Anonymous methods
  • Iterators
  • Nullable types
  • Private setters (properties)
  • Method group conversions (delegates)
  • Covariance and Contra-variance
  • Static classes

C# 3.0.NET Framework 3.0\3.5 (Visual Studio 2008)

  • Implicitly typed local variables
  • Object and collection initializers
  • Auto-Implemented properties
  • Anonymous types
  • Extension methods
  • Query expressions
  • Lambda expressions
  • Expression trees
  • Partial Methods

C# 4.0.NET Framework 4.0 (Visual Studio 2010)

  • Dynamic binding (late binding)
  • Named and optional arguments
  • Generic co- and contravariance
  • Embedded interop types

C# 5.0.NET Framework 4.5 (Visual Studio 2012/2013)

  • Async features
  • Caller information

C# 6.0.NET Framework 4.6 (Visual Studio 2013/2015)

  • Expression Bodied Methods
  • Auto-property initializer
  • nameof Expression
  • Primary constructor
  • Await in catch block
  • Exception Filter
  • String Interpolation

C# 7.0.NET Core (Visual Studio 2017)

  • out variables
  • Tuples
  • Discards
  • Pattern Matching
  • Local functions
  • Generalized async return types
  • throw Expressions




Switch statement in c#

Switch statement

The switch statement is like a set of if statements. It's a list of possibilities, with an action for each possibility, and an optional the default action, in case nothing else evaluates to true, so we can say that A switch statement allows a variable to be tested for equality against a list of values.

Syntax

switch ( CaseVariable )
{
    case constantExpression_1:
    statements;
    break;
    case constantExpression_2:
    statements;
    break;
    case constantExpression_3:
    statements;
    break;
    default:
    statements;
    break;
}



Example of a switch statement

public class switchStatementExample
{
  static void Main(string[] args)
  {
    char grade = 'B';

    switch (grade)
    {
      case 'A':
        Console.WriteLine("Excellent!");
        break;
      case 'B':
      case 'C':
        Console.WriteLine("Well done");
        break;
      case 'D':
        Console.WriteLine("You passed");
        break;
      case 'F':
        Console.WriteLine("Better try again");
        break;
      default:
        Console.WriteLine("You Failed!");
        break;
    }
  }
}

For Loop in c#

For Loop

The for loop contains three parts: initialization, conditional expression, and steps, which are separated by a semicolon,
1.  variable initialization: Declare & initialize a variable here which will be used in conditional expression and steps part.
2.      condition: The condition is a boolean expression that will return either true or false.
3.      steps: The steps define the incremental or decremental part

Syntax



for ( initialization; termination; increment/decrement)
{
    statements
}



For Loop example

class ForLoopExampleProgram
{
  static void Main(string[] args)
  {
    int i;
    i = 1;
    for (i = 0; i <= 50; i++)
    {
      Console.WriteLine("current Value of the i variable is :" + i.ToString());
      i = i + 5;
    }
    Console.Read();
  }