Monday, 31 July 2017

Boxing , Unboxing , Virtual,Void ,new keywords And Access Specifiers


Lets get introduced to following terms


  • Boxing- The term boxing stands for converting value types to Object type. Boxing is the ultimate way to treat value type as Object type.The type conversions are happened internally from value type to Object type.

  • In below example the int is the value datatype converted into Object type using Object Data type.

    Example
    int i=7
    Object o=i


  • Unboxing- The term Unboxing stands for converting Object types to value type vice versa of Boxing. It is the opposite of the Boxing. These conversions are also happened implicitly.
  • Example

    Object 0=23
    int i=(int)o



  • VIRTUAL and OVERRIDE Keyword 
  •            virtual keyword is used while implementing method overriding. method overriding stands for overriding method of base class with derived class.


    Program.


    using System;
    namespace virtual_override
    {
        class Base
        {
            public virtual void show()
            {
                Console.WriteLine("Base class method");
                Console.ReadKey();
            }
        }
        class Derived:Base
        {
            public override void show()
            {
         
                Console.WriteLine("derived class method");
                Console.ReadKey();
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Base b = new Base();
                Derived d = new Derived();
       //         b.show();
                d.show();               //will show derived class output
            }
        }
    }


    Output.
    derived class method



  • Using new keyword
  • C# supports method hiding. new keyword is used to explicitly hide member it inherited from base class.

     class Base
        {
            public void show()
            {
                Console.WriteLine("Base class method");
                Console.ReadKey();
            }
        }
        class Derived:Base
        {
            public new  void show()
            {
           //     base.show();
                Console.WriteLine("derived class method");
                Console.ReadKey();
            }
        }


In C sharp , every Object and it's members has been set an accessibility. The term Access  stands for the accessibility level of Objects and members. Access Modifiers provides a level of security to Objects and Classes. We can control which class should be accessed by other classes.


There are total five Access Specifiers in C Sharp.

  • public- the most commonly used access modifier in C Sharp. public access modifier states that there is no restriction and limitation to use Object or Class that this Specifier is assigned to. The public class or Object can be accessed from anywhere , inside or outside of program.


  • private-Opposite to public access modifier , private Object or Classes have very limited scope. They can be accessed only inside the class in which they are declared. 


  • protected- The class or Object to which protected has been assigned can be accessed only with the class and it's derived class.


  • internal-These type of classes and objects can be accessed within  the same program containing it's declaration. But cannot accessed from outside of program.


  • protected internal- Combines accessibility of both protected and internal access specifiers. Can be accessed in same program or class as well as inherited classes.
  • Sunday, 30 July 2017

    Loops in C sharp



    There may be times when you want to execute certain code of lines multiple times. In C sharp , there are certain loop structure who's function is to execute lines of code multiple times until the condition specified is true.




    • WHILE loop- Repeatedly executes a group of statements until given condition is true. The condition is tested before execution of statements.

    Syntax

             
               while(Condition)
               {
                        //group of statements to execute;
               }


    Here the group of statements are the statements which needs to be executed if the condition provided in WHILE loop is true. The statements are executed in order. i.e first statement will execute first, second will execute second. If the Condition provided in WHILE loop is not true , then the WHILE loop will be skipped and the statements after the loop will be executed.
    Program.


    using System;
    namespace constant_var_program
    {
        class Program
        {
            static void Main(string[] args)
            {
                int a = 5;
                while (a < 10)
                {
                    Console.WriteLine("Value of a {0}", a);
                    a++;
                 
                }
                Console.ReadKey();

            }
        }
    }


    Output
    value of a 5
    value of a 6
    value of a 7
    value of a 8
    value of a 9



    • DO..WHILE loop-  unlike WHILE and FOR loop, DO..WHILE loop tests the condition at the end of the loop. Means the statements will be executed at least once even if the condition is wrong.
    • After checking the condition , if the condition is true , then the control will go to start of the loop again and will continue to execute until the condition is false.


    Syntax

               do
              {
                        //statements;

              }while(condition);

    Program


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace constant_var_program
    {
        class Program
        {
            static void Main(string[] args)
            {
                int k=1;
                do
                {
                    Console.WriteLine("value of k is {0}", k);
                    k++;
                } while (k < 10);
                Console.ReadKey();
            }
        }
    }



    Output



    value of k 1
    value of k 2
    value of k 3
    value of k 4
    value of k 5
    value of k 6
    value of k 7
    value of k 8
    value of k 9



    • FOR loop- unlike other loops , FOR loop is more efficient in syntax and execution.
    Syntax

             

              for(initialisation;condition;increment)
             {
                   //Statements ;
             }


    In Initialization step , the variables for the loop will be declared and initialized. This step is executed only once.
    Next in Condition step , the condition will be specified. If the condition is true , then the statements in body of the FOR loop will be executed. Otherwise the body of the loop is skipped and the control goes to the next statement after the loop.
    After the Condition evaluates to be true and the body of the loop is executed then the control is transferred to the Increment step. In this step , the loop control variables can be updated by any number.
    Program

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;


    namespace constant_var_program
    {
        class Program
        {
            static void Main(string[] args)
            {
                for (int i = 1; i <= 5; i++)
                {
                    Console.WriteLine("Value of i {0}",i);
                 
                }
                Console.ReadKey();
            }
        }
    }



    Output

    value of i 1
    value of i 2
    value of i 3
    value of i 4
    value of i 5


    We will be working on advanced program based on this basic.

    Operators in C sharp



    Operators are used to perform Mathematical Operations

    • Arithmetic Operators-
    • Operator Usage Example
      + To add two Variables A+B
      - To Substract Two Variables A-B
      * To Multiply Two Variables A*B
      / To Divide Two Variables A/B
      % Called As Modules. Gives Reminder of Division A%B
      ++ Increment Operator-Increments integer variable by 1 A++
      -- Decrement Operator-Decrements integer variable by 1 A--


    • Relational Operators-
    • Operator Usage Example
      == To check if two operands are equal A==B
      != To check if two operands are equal or not A!=B
      > To Check if the left hand operand is grater then right A > B
      < To check whether left hand operand is less than right A < B
      >= Check whether left hand operand is greater than or equal to right hand operand A>=B
      <= Check whether left hand operand is less than or equal to right hand operand A<=B


    • Logical Operators-
    • Operator Usage Example
      && Logical AND Operator. If both values are non-zero then condition is true A && B
      || Logical AND Operator. If any one value is non-zero then condition is true A || B
      ! Logical NOT Operator. Used to reverse the logical state of operand !A


  • Bitwise Operators-

  • Operator Usage Example
    & Compares bits of two integrals respectively and set corresponding bit
    to 1 when both sides have 1 bit
    A & B
    | Compares bits of two integrals respectively and set corresponding bit
    to 1 when either sides have 1 bit
    A | B
    ^ XOR operator.Compares bits of two integrals respectively and set corresponding
    bit to 1 when only one sides have 1 bit
    A ^ B
    ~ Unary operator resulting in one's complement of number ~A
    << Left Shift- left operand value is moved left specified by
    bits specified at right of operator
    A << 5
    >> Right Shift- left operand value is moved right specified by
    bits specified at right of operator
    A >>4



  • Assignment Operators-

  • Operator Usage Example
    = Assignment operator used in assigning value of right side of operator to left side operand A = 2 Or R=P+Q
    += Shorthand Assignment operator
    First Adds the numbers and then assign the result to left hand operand
    A+=B
    -= Shorthand Assignment operator
    First subtracts the numbers and then assign the result to left hand operand
    A -= B
    *= Shorthand Assignment operator
    First multiply the numbers and then assign the result to left hand operand
    A*=B
    /= Shorthand Assignment operator
    First divide the numbers and then assign the result to left hand operand
    A /= B
    %= Shorthand Assignment operator
    First takes modules the numbers and then assign the result to left hand operand
    A %=B

    Friday, 28 July 2017

    Data Types in C Sharp



    Following are the Datatypes in C Sharp



    • int- This is a 32 signed integer type data type. It range from -2,147,483,648 to 2,147,483,647 and it's default value is 0.

    • float- This is 32-bit single-precision floating point data type . It ranges from -3.4 x 1038 to + 3.4 x 1038 and it's default value is 0.0f.

    • string- This data type contains string of Unicode characters. It can range from any number of alphabet.

    • long-This is 64-bit signed integer data type.It ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and it's default value is 0L.

    • short. This is 16-bit signed integer data type. It ranges from -32,768 to 32,767 and default value is 0.

    • bool-This is boolean value data type . It represents only two values True or False. Default value is False.

    • byte-This is 8-bit unsigned integer data type.It ranges from 0 to 255 and default value is 0.

    • char-This is 16-bit Unicode character data type. It ranges from U +0000 to U +ffff and default value is '0'.

    • decimal-This is 128-bit precise decimal values with 28-29 significant digits data type. It ranges from (-7.9 x 1028 to 7.9 x 1028) / 100 to 28 and  default value is 0.0.

    • double-This is 64-bit double-precision floating point data type. It's default value is 0.0D and  it ranges from (+/-)5.0 x 10-324 to (+/-)1.7 x 10308  .

    • sbyte-This is 8-bit signed integer data type. It ranges from -128 to 127 and default value is 0.

    • short-This is 16-bit signed integer data type. It ranges from -32,768 to 32,767 and 0 is it;s default value.

    • uint- This is 32-bit unsigned integer data type. It's range is 0 to 4,294,967,295 and having 0 default value.

    • ulong-This is 64-bit unsigned integer data type. It ranges from 0 to 18,446,744,073,709,551,615. and 0 is default value

    • ushort- This is  16 bit signed integer data type. Its range is 0 to 65,535. and default value is 0.

    Decision Making In C Sharp


    In C Sharp , using Conditional Statements we can check and evaluate expressions . Some times we need to check whether the expression turns out to be true or false. Depending on that , we can make decision what to do with that expression.

    Syntax



              if(expression)        
             {
                          // statements to execute if expression is true.
              }
              else
             {
                            // statements to execute if expression is false.
             }


    In above syntax , we pass the expression to the IF() statement. The IF() statement checks the condition/expression passed to it and determines if the expression is true or false. If the expression is true , the statements immediately following the IF() in the brackets are executed. And if the given expression turns out to be false, then the control flows to else and the statements in the else are executed.

    Below program defines use of if...else structure



    using System;
    namespace conditional_program
    {
        class Program
        {      
             static void Main(string[] args)
            {

                int a = 10;
                int b = 20;
                if (a < b)
                {
                    Console.WriteLine("a is less than b");
                }
                else
                {
                    Console.WriteLine("a is greater than b");
                }
             
            }
        }
    }

    Output
    a is less than b

    • if..else..if
    When there are multiple conditional statements that need to be checked simultaneously, then we can use if..else..if structure.This is called as else..if ladder.

    using System;

    namespace conditional_program
    {
        class Program
        {
            static void Main(string[] args)
            {

                int a = 10;
                int b = 20;
                if (a > b)
                {
                    Console.WriteLine("a is less than b");
                }
                else if (a<b)
                {
                    Console.WriteLine("a is less than b");
                    Console.ReadKey();
                }
                else
                {
                    Console.WriteLine("both are same");
                    Console.ReadKey();
                }
             
            }
        }
    }

    Output
    "a is less than b
    • nested if..else
    In some cases , you need to check more than one condition one after another then we can use nested if..else. Using nested if..else we can check two or more conditions simultaneously. The treatment given in nested if else is if one condition is true then go to next condition and check if it is also true or control transfers to the else condition of the first if statement.


    using System;


    namespace constant_var_program
    {
        class Programv  
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter two values");
                int a = int.Parse(Console.ReadLine());
                int b = int.Parse(Console.ReadLine());

                if (a < b)
                {
                    if (b > a)
                    {
                        Console.WriteLine("a is less than b and b is greater than a ");
                        Console.ReadKey();
                    }
                    else
                    {
                        Console.WriteLine("b is less then a ");
                        Console.ReadKey();
                    }

                }
                else
                {
                    Console.WriteLine("a is greater than b ");
                    Console.ReadKey();
                }


            }
        }
    }


    Output
    condition 1

    Enter two values
    10
    20
    a is less than b and b is greater than a

    condition 2

    Enter two values
    20
    10
    a is greater than b


    Constants , Variables And Type Conversions


    In this Post , I am going to introduce you with the C Sharp Constants, Variables And Type conversions.


    • Constants - are those variables whose values do not change. Once a value is assigned to constant variable it will be same throughout the program. If tried to assign value to constant Variable in a program after it is already assigned , then it will generate an error. 
    • Constants can be specified in Octal , Hexadecimal or an integer type. Also they can be of any Data Type like character constant, integer constant , String constant or floating point constant. These fixed values are also called as literals.
      For Example. -

      Integer Literal- Contains octal, hexadecimal and decimal constants. There are prefix defined for each type of constants. 0x or 0X is for Hexadecimal constant, 0 for Octal constant and no prefix means Decimal constant.
      In other cases, there are also suffix defined with integer constant. There are two types of suffix for integer constants. U for unsigned integer and L for Long integer.

      Valid Integer constants-

      452-   decimal constant
      454u-   unsigned integer constant
      0x3ABu-   Hexadecimal Constant
      076-   octal constant

      Floating point Literal - Floating literals is a combination of integer , decimal as well as well as fractional part and exponent part. It also contains suffix as f, F, l L.

      Rules
      1) You can exclude Decimal part or Fraction part but cannot exclude both while writing Floating point literal.
      2) You can exclude Decimal part or Exponent part with signed integer exponent but cannot exclude both.
      3) There should be one digit present to the left and right of the Decimal part when writing Floating literal in Fractional Form.
      Examples

      1.84
      3.54e
      3.543-4

      String literal C# strings are nothing but a set of characters.string literal is a string constant. Basically String literals are divided into two types. Regular String literal and verbatim string literal. Regular strings are similar to other programming language strings like Java ,CPP etc. They are contained in "". Opposite to that , verbatim string literals starts with @ symbol at the beginning of the string.

      Example
      "Regular"
      @"verbatim"
      "world"
      @"Back"

      Character literal- Character literal is a single character enclosed in single quotation mark. For Example 'f'. Character literals also contains escape sequence and universal characters. For example \t , \n , \r.

      Syntax

      const DATA_TYPE
      VARIABLE_NAME =value

      Example

      const int i=1
      const float f=3.4

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text; using System.Threading.Tasks;


      namespace constant_program
      {
          class Program
          {
              static void Main(string[] args)
              {
                  const int i = 20;
                  Console.WriteLine(i);
                  Console.ReadKey();
               
              }
          }
      }


      Output
      20

    • Variables- Variables are nothing but a temporary storage area gets created in memory.Variables are used to store values. The value in Variable may change in a program. Each variable has a specific data type which determines how much size it will occupy in memory.

    • There are certain rules to be followed while defining the variable.
      1)Variable name must start with Alphabet, letter and underscore.
      2)C# defined keywords cannot be used as variable name.
      3)Variable names must not contain spaces and special characters.
      4)It is good practice not to use all uppercase letters in variables. Upper case letters are primarily used to define constant variables.

      Syntax
      DATA_TYPE variable_name

      Example

      int i;
      float j
      string s;


      Program
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;

      namespace var_program
      {
          class Program
          {
              static void Main(string[] args)
              {
                  int i;
                  i=20;
                  string s;
                  s="Technology";
                  Console.WriteLine(i);
                  Console.WriteLine(s);
                  Console.ReadKey();
               
              }
          }
      }
      Output
      20
      Technology
    • Type Conversion
    • Type Conversions stands for Converting one data type to another data types. It is also known as Type Casting. Basically having two types.

    • Implicit Type Conversion
    • This type of conversion is done by the C Sharp itself.being a strongly typed language, this type of conversion is performed in type safe manner by C Sharp.



    • Explicit Type Conversion.
    • This type of conversion is done by users on built in data type. Explicit Type Conversion.are done with the help of cast operator.

      using System;

      namespace var_program
      {
          class Program
          {
              static void Main(string[] args)
              {
                  float p=553.4;
                  int i=(int) p;
                  Console.WriteLine(i);
                  Console.ReadKey();
               
              }
          }


      Output
      553. (As it is casted in integer data type, so the remaining part is truncated.)

    C Sharp Introduction


    C Sharp is (C#) very powerful programming language developed by Microsoft. Using C Sharp , developers can create efficient Web as well as Windows Application Programs. C sharp is purely Object Oriented  Programming Language. The features of C Sharp gives developers the power to write and develop highly efficient Applications.

    C Sharp uses the Microsoft .Net Framework to develop Applications. .Net Framework is Software infrastructure created by Microsoft for creation and development of Applications that use .Net technology. It contains many features that C Sharp can use to develop Applications.

    In this section , You will be getting all types of C Sharp Programs.