ZCubes Experience

Please Wait....
Your Experience is being rendered.
Click the [Live] button if the Experience does not load in few moments.


















C#


-Priyanka
Introduction

 

 

  • Released in 2000 by Microsoft
  • Developed by Anders Hejlsberg and Scott Anderson
  • It is safe and object oriented language for .Net
    development
Features

 

  • Pointers are missing in C#.
  • Automatic memory management is implemented by garbage collector.
  • Supports inheritance, polymorphism, interfaces .
  • Helps for building interoperable, scalable, robust applications
  •  .NET includes a CLR and a FCL.
    • CLR-common language runtime
    • FCL-Framework class library
 
 
Data Types

 

  • Value Types
  • Reference Types


 Value Type  Reference Type
 allocated on stack  allocated on heap
 contains the data itself  contains the address of memory location where data is actually stored.
 integer, float, boolean, double etc are value types  string and object are reference types
Variables

  • Value-Type Declaration:
    • byte bVar = 111;
    • int iVar = 786;
    • float fVar = 110;
    • char cvar =‘a’;

 

  • Reference –Type Declaration:
    • String sVar= “test”;
    • Object myobj= new Object();

 

Arithmetic Operators

  • Addition: +
  •  Subtraction: -
  • Multiplication: *
  • Division: /
  • Modulus: %
  • Increment and Decrement: ++ --
    • ++a - increments a, returns new value
    • a++ - returns current value, then increments a

 

System.Console.WriteLine(2 * 2); # prints 4

System.Console.WriteLine(5 % 2); # prints 1

System.Console.WriteLine(5 / 2); # prints 2

Relational Operators

 

  • <  >  <= >= == !=
    • for ex: int bigvalue =100;
    • bigvalue == 100    # return true
    • bigvalue != 100     # return false
    • bigvalue <= 100    # return true

 

Logical Operators

  • AND: &&
  • OR: ||
  • not: !
    • (Assume x=5; y=7)
    • (x==3) && (y==7)  # returns false
    • (x==3) || (y==7)  # returns true
    • !(x==3)    # returns true

 

  • Ternary Operator
    • System.Console.WriteLine(2>3 ? “true”: “false”)

#prints false

 

Control

  • Statements that control flow:
    • if (expression) stmt [else  stmt]
    • switch (expression)

{
    case constant-expr;
    jump-stmt;
    [default: stmt]
   }

    • While (expression) stmt
    • Do stmt while (expression)
    • For([initializer];[expr];[iterators]) stmt
    •  Foreach stmt
       
Classes

  • class is a blueprint for creating components of similar behavior and attributes

    • using System;

      // helper class

      class OutputClass
      {   
       string myString;
       // Constructor           
       public OutputClass(string inputString)
       {                
       myString = inputString;          
       }   
       // Instance Method         
       public void printString()  
       {              
       Console.WriteLine("{0}", myString);     
       }   
       // Destructor          
       ~OutputClass()   
       {                 
       // Some resource cleanup routines  
       }
      }
      // Program start class class ExampleClass
      {    
      // Main begins program execution.  
       public static void Main()        
       {            
       // Instance of OutputClass       
       OutputClass outCl = new OutputClass("This is printed by the output class.");
       // Call Output class' method         
       outCl.printString();     
       }
      }

      Interfaces

      • Interfaces have no implementation
      • The functions must be implemented in derived class
      • Cannot create an instance of interface.

      using System;
          interface Interdemo
          {
             void Show();
          }
          class Interimp:Interdemo
         {
            public void Show()
           {
               Console.WriteLine("Show() method Implemented");
           }
          public static void Main(string[] args)
          {
               Interimp inter = new Interimp();
               inter.Show();
           }
        }
      Inheritance

      • Involves a base type and derived type.
      • Base type – generalization
      • Derived type – specialty
      • Helps in code reuse

      using System;
      public class ParentClass
      {    
       public ParentClass()   
       {       
        Console.WriteLine("Parent Constructor.");  
       }
       public void print()   
       {      
        Console.WriteLine("I'm a Parent Class.");    
       }
      }
      public class ChildClass : ParentClass
      {    
       public ChildClass()   
       {      
        Console.WriteLine("Child Constructor.");    
       }   
       public static void Main()  
       {        
        ChildClass child = new ChildClass();    
        child.print();    
       }
      }
      Output:
      Parent Constructor.
      Child Constructor.
      I'm a Parent Class.
      Polymorphism 
       

      • Different Objects behave differently at different instances of time

      public class DrawingObject
      {   
       public virtual void Draw()  
       {        
       Console.WriteLine("I'm just a generic drawing object.");   
       }
      }
      public class DrawDemo
      {    
       public static int Main( )  
       {        
        DrawingObject[] dObj = new DrawingObject[2];
        dObj[0] = new Line();  
        dObj[1] = new Circle();      
        foreach (DrawingObject drawObj in dObj)   
        {          
         drawObj.Draw();   
        }     
       }
      }
      public class Line : DrawingObject
      {   
       public override void Draw()
       {        
       Console.WriteLine("I'm a Line.");    
       }
      }
      public class Circle : DrawingObject
      {  
       public override void Draw()  
       {        
       Console.WriteLine("I'm a Circle.");    
       }
      }
      Assemblies

      • Collection of files that appear to be a single DLL or executable
      • DLL are collections of classes and methods that are linked to the program only when needed.
      Summary

      • C# is safe ,modern and object oriented language for .Net  development