Fundamentals of C#

Variables:

Variables are the storage area which holds the data for further use in the  program. Each variable is of some specific data type about which we will talk later in my other article.

We can declare variables as <datatype> < variable> & we assign constants or data to the variable by writing ‘=’ between the data to be stored & the variable like this: <datatype> < variable> = constant

We can initialize variables in two ways:

  1. Indirect initialization

    Example:

    int a;
    float c;
    a=2;
    c=4.77;

  2. Direct initialization

    Example:

    int a=2;
    float c = 4.77;

Constants:

Constants are the variables which have fixed value. Their value can’t be changed. It can be of any data type. We use constant only when we don’t want to change the value of a data. We can define constant using const keyword and we declare constant like this: const <data_type> <constant_name> = value;

Example:

const double pi = 3.14159;

Literals:

The fixed value provided to the constant or the variable are called literals. There are four types of literals:

  1. Integer literal: Any integer, decimal, octal or hexadecimal value is called integer literal.

    Example:

    85 /* decimal */
    0213 /* octal */
    0x4b /* hexadecimal */

  2. Floating point literal: Floating point literal is written in either decimal or exponential form

    Example:

    3.14159
    314159E-5L

  3. Character literal: When the value is enclosed in single quotes then it is referred as character literal.

    Example:

    ‘x’
    ‘\u’

  4. String literal: When the value is enclosed in double quotes or written as @”value” then it is referred as string literal.

    Example:

    “x”
    @“res”

Statements:

Statements are the single lines of code which end with a semicolon. There is also statement block which contains multiple statements and are contained between the curly braces. There are many kinds of statements. We will see a few frequently used statements with examples.

  1. Declaration statement: When we declare some variable , the whole statement is called declaration statement.

    Example:

    double radius = 2;

  2. Expression statement: When we express some logic or some work to be done by the statement, it is called expression statement.

    Example:

    area = 3.14 * (radius * radius);

  3. Selection statement: Selection statements enable us to do the branching of code.

    Example:

    If, else, else if, switch

  4. Iteration statement: Iteration statement enable us to loop through a block of code.

    Example:

    Do, while, for

  5. Jump statement: Jump statement helps us in transferring the control to some other part of code.

    Example:

    Continue, goto

  6. Exception handling statement: These statement help in no abrupt stopping of execution and let the program run by handling the exception.

    Example:

    Throw, try-catch

  7. Empty statement: When there is just a semicolon in a line of code it is referred as empty statement.

    Example:

    ; //Empty statement

Expressions:

When a line of code is intended to do some work or result in some value, it is termed an expression. Expressions use different operators to yield some result or give some output value or do some work. There are three types of expressions:

  1. Invoking Expression: The expressions which are intended to invoke some method or function is called invoking expression.

    Example:

    DoSomething();

  2. Query Expression: These expressions are written as a query i.e. first class language construct to write some code to yield some output. It is used to derive data from a database or some storage where data is kept.

    Example:

    IEnumerable<int> scoreQuery = from score in scores
    where score > 80
    select score;

  3. Lambda Expressions:

    We can create expression trees or delegates by using anonymous function i.e. lambda expression. For creating a lambda expression we need to specify input parameters (if any) on the left side of the lambda operator, and put the statement or expression on the other side. It is denoted by ‘=>’ symbol.

    Example:

    del myDelegate = x => x * x;
    Here, the lambda expression x => x * x specifies a parameter that’s named x and returns the value of x squared.

I hope this article has helped you.