NET Tutorial _ Learn C# and .NET in 4 days

Embed Size (px)

DESCRIPTION

.NET and C# tutorial for beginners.

Citation preview

  • .NET Story

    May2014

    1 .NET Tutorial (shibasissengupta.blogspot.com)

  • .NET Tutorial (shibasissengupta.blogspot.com)2

    ASP.NET Asp.NET is successor of Microsoft's Active server pages (ASP) technology. ASP.NET is a server-side Web application framework designed for

    Web development to produce dynamic Web pages. It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services.

    ASP.NET is built on the Common Language Runtime (CLR), allowing programmers to write ASP.NET code using any supported .NET language

    Date Version Comments

    Jan, 2002 1.0

    April, 2003 1.1

    Nov, 2005 2.0

    Nov, 2006 3.0

    Nov, 2007 3.5

    Aug, 2008 3.5 sp1

    April, 2010 4.0 Parallel extensions and other .NET 4 features introduced

    Aug, 2012 4.5 Released with VS 2012

  • .NET Tutorial (shibasissengupta.blogspot.com)3

    Common Language Runtime - CLR The Common Language Runtime (CLR) is the virtual machine

    component of Microsoft's .NET framework and is responsible for managing the execution of .NET programs.

    In a process known as just-in-time compilation, the compiled code is converted into machine instructions that, in turn, are executed by the computer's CPU

    The CLR provides additional services including memory management, type safety, exception handling,garbage collection and thread management

    CLR is common to all versions of the .NET framework

    CLR Version .NET Version

    1.0 1.0

    1.1 1.1

    2.0 2.0, 3.0, 3.5

    4 4, 4.5

  • .NET Tutorial (shibasissengupta.blogspot.com)4

    Common Language Infrastructure (CLI) & Common Intermediate Language (CIL) The Common Language Infrastructure (CLI) is an open specification

    developed by Microsoft

    In August 2000, Microsoft, Hewlett-Packard, Intel, and others worked to standardize CLI.

    To implement the CLI standard requires conformance to one of the supported and defined profiles of the standard

    The .NET Framework is Microsoft's original commercial implementation of the CLI

    .NET Compact Framework is Microsoft's commercial implementation of the CLI for portable devices and Xbox 360.

    CLI Languages are computer programming languages that are used to produce libraries and programs that conform to the Common Language Infrastructure (CLI) specifications

    Most CLI languages compile entirely to the Common Intermediate Language (CIL), an intermediate language that can be executed using an implementation of CLI such as the Common Language Runtime (CLR, a part of the Microsoft .NET Framework), Mono, or Portable.NET

    Common CLI compliant languages are - C# A# (CLI implementation of Ada) Cobra F# IronRuby IronPython

  • .NET Tutorial (shibasissengupta.blogspot.com)5

    Getting Started // Namespace Declaration

    using System; // Program start classclass WelcomeCSS { // Main begins program execution. static void Main() { // Write to console Console.WriteLine("Welcome to the C# Tutorial!"); }}

    Namespaces are C# equivalent of Java packages.

    Collection of namespace/s are distributed generally as dll. In Java world, it is similar to jar files.

  • .NET Tutorial (shibasissengupta.blogspot.com)6

    Value Types, Reference Types, aliases and difference from Java In particular, C# provides two typesclass and struct, which are almost the same except that one is a reference type while

    the other is a value type Put simply, structs are cut-down classes.Imagine classes that dont support inheritance or finalizers, and you have the cut-

    down version: the struct Structs arevalue types, while classes arereference types, and the runtime deals with the two in different ways When a value-type instance is created, a single space in memory is allocated to store the value.Primitive types such

    asint,float,boolandcharare also value types, and work in the same way (Not String/string).When the runtime deals with a value type, it's dealing directly with its underlying data and this can be very efficient, particularly with primitive types.

    With reference types, however, an object is created in memory, and then handled through a separate referencerather like a pointer

    How to check which type it is Console.WriteLine(typeof(int).IsValueType); // displays "True We will discuss shortly why string isnt a value type like integer

  • .NET Tutorial (shibasissengupta.blogspot.com)7

    Value Types, Reference Types, aliases and difference from Java(contd..) With reference types, however, an object is created in memory, and then handled through a separate referencerather

    like a pointer.SupposePointis a struct, andFormis a class.We can instantiate each as follows: Point p1 =newPoint();// Point is a *struct*

    Form f1 =newForm();// Form is a *class*

    In the first case, one space in memory is allocated forp1, wheras in the second case, two spaces are allocated: one for aFormobject and another for its reference (f1).It's clearer when we go about it the long way: Form f1;// Allocate the reference

    f1 =newForm();// Allocate the object

    If we copy the objects to new variables: Point p2 = p1;

    Form f2 = f1;

    p2, being a struct, becomes an independentcopyofp1, with its own separate fields.But in the case off2, all weve copied is areference, with the result that bothf1andf2point to the same object.

  • .NET Tutorial (shibasissengupta.blogspot.com)8

    Value Types, Reference Types, aliases and difference from Java(contd..) The Common Language Runtime allocates memory for objects in two places: thestackand theheap.The stack is a

    simple first-in last-out memory structure, and is highly efficient.When a method is invoked, the CLR bookmarks the top of the stack.The method thenpushesdata onto the stack as it executes.When the method completes, the CLR just resets the stack to its previous bookmarkpopping all the methods memory allocations is one simple operation!

    In contrast, theheapcan be pictured as a random jumble of objects.Its advantage is that it allows objects to be allocated or deallocated in a random order.The heap requires the overhead of amemory managerandgarbage collectorto keep things in order.

    Why string /String isnt a value type? Mainly reason is Performance: Strings can be at times large and stack memory allocation being less (many times at 1MB

    only) it can create space and performance issue. So its handled via heap memory like reference types. Even after being a reference type, C# allows string to be used like a value type. But in Java, string comparison like the

    below one will compare the references and will return false. string s = "hello"; string t = "hello"; bool b = (s == t); will return true

  • .NET Tutorial (shibasissengupta.blogspot.com)9

    Value Types, Reference Types, aliases and difference from Java(contd..)Note: apart from object and string, all aliases are value types.

    Commonly Asked Question What to use string/String?

    Both being same its a matter of personal choice. In fact, CLR always converts string to System.String internally.

    Alias Pointing To

    object System.Object

    string System.String

    bool System.Boolean

    byte System.Byte

    sbyte System.SByte

    short System.Short

    ushort System.Ushort

    int System.Int32

    uint System.UInt32

    long System.Int64

    ulong System.UInt64

    float System.Single

    double System.Double

    decimal System.Decimal

    char System.Char

  • .NET Tutorial (shibasissengupta.blogspot.com)10

    Types

    Type Size (In bits)

    decimal 128

    8

    byte 8

    short 16

    ushort 16

    int 32

    uint 32

    long 64

    ulong 64

    char 16

    float 32

    double 64

    decimal 128

    Escape Sequence Meaning

    \'

    Single quote

    \ Double quote

    \\ Backslash

    \o Null, not the same as C# null value

    \a Bell

    \b Backspace

    \f Form feed

    \n New Line

    \r Carriage Return

    \t Horizontal Tab

    \v Vertical Tab

  • .NET Tutorial (shibasissengupta.blogspot.com)11

    Unary Operatorsusing System;

    class Unary{ public static void Main() { int unary = 0; int preIncrement; int preDecrement; int postIncrement; int postDecrement; int positive; int negative; sbyte bitNot; bool logNot; preIncrement = ++unary; Console.WriteLine("pre-Increment: {0}", preIncrement); preDecrement = --unary; Console.WriteLine("pre-Decrement: {0}", preDecrement); postDecrement = unary--; Console.WriteLine("Post-Decrement: {0}", postDecrement);

    postIncrement = unary++; Console.WriteLine("Post-Increment: {0}", postIncrement); Console.WriteLine("Final Value of Unary: {0}", unary);}}

    Output:

    pre-Increment: 1 pre-Decrement 0 Post-Decrement: 0 Post-Increment: -1 Final Value of Unary: 0

  • .NET Tutorial (shibasissengupta.blogspot.com)12

    Control Statements using System;

    class SwitchSelect

    {

    public static void Main()

    {

    string myInput;

    int myInt;

    begin:

    Console.Write("Please enter a number between 1 and 3: ");

    myInput = Console.ReadLine();

    myInt = Int32.Parse(myInput);

    // switch with integer type

    switch (myInt)

    {

    case 1:

    Console.WriteLine("Your number is {0}.", myInt);

    break;

    case 2:

    Console.WriteLine("Your number is {0}.", myInt);

    break;

    case 3:

    Console.WriteLine("Your number is {0}.", myInt);

    break;

    default:

    Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);

    break;

    }

    decide:

    Console.Write("Type \"continue\" to go on or \"quit\" to stop: ");

    myInput = Console.ReadLine();

    // switch with string type

    switch (myInput)

    {

    case "continue":

    goto begin;

    case "quit":

    Console.WriteLine("Bye.");

    break;

    default:

    Console.WriteLine("Your input {0} is incorrect.", myInput);

    goto decide;

    }

    }

    }

  • .NET Tutorial (shibasissengupta.blogspot.com)13

    Nested Namespace // Namespace Declaration

    using System;

    namespace csharp_tutorial { // Program start class class NamespaceCSS { // Main begins program execution. public static void Main() { // Write to console Console.WriteLine("This is the new C# Namespace."); } }}

    // Namespace Declarationusing System;

    namespace csharp_tutorial { namespace tutorial { // Program start class class NamespaceCSS { // Main begins program execution. public static void Main() { // Write to console Console.WriteLine("This is the new C# Tutorial Namespace."); } } }}

  • .NET Tutorial (shibasissengupta.blogspot.com)14

    Three pillars of Object Oriented Programming Encapsulation: Encapsulation involves a single object that

    contains both its data and behaviors and can hide what it wants from other objects. In real programming world it is achieved by classes, access modifiers etc.

    Inheritance : Inheritance allows the concept of creating a basic functionality which can be inherited in many places as well as enhanced as required.

    Polymorphism: Is probably the hardest of the three concepts and will be discussed in detail in later sections.

  • .NET Tutorial (shibasissengupta.blogspot.com)15

    Classes using System;// helper classclass OutputClass {public string myString;

    // Constructorpublic OutputClass(string inputString) {myString = inputString;}

    // Instance Methodpublic void printString() {Console.WriteLine("{0}", myString);

    }

    public static String testStatic()

    {

    Console.WriteLine(This is a static calls);

    //The following line will not complie this refers to an instance at runtime which might not be available

    this.myString=Static Update;

    New OutputClass.myString=Another static update; // This will work

    }

    // Destructor~OutputClass() {// Some resource cleanup routines}}

    class ExampleClass {// Main begins program execution.public static void Main() {// Instance of OutputClassOutputClass outCl = new OutputClass("This is printed by the output class.");

    // Call Output class' methodoutCl.printString();

    //Static Call

    OutputClass.testStatic();}}

  • .NET Tutorial (shibasissengupta.blogspot.com)16

    Classes (Contd..) Desigining classes reflects the requirements In a bottom-up design approach many DB layer objects can be

    mapped to single classes or clubbed together as a class as per the requirements.

    e.g. Department Table and Employee Table is linked as each employee is part of a department. One way to effectively map this relationship to class level is design a Department Class an Employee Class and have a property in the Department class which can hold a collection of employee class objects.

    public Class Department

    {

    String deptId;

    Sring deptName;

    String deptHeadEmpId;

    Dictionary empList;

    }

    public Class Employee

    {

    Public String empId;

    Public String empName;

    Public String deptId;

    Public String reportsToId;

    }

  • .NET Tutorial (shibasissengupta.blogspot.com)17

    Access Modifiers Access mofiders support a major pillar of object oriented

    programming- encapsulation.

    Modifier Description

    public There are no restrictions on accessing public members.

    private Access is limited to within the class definition. This is the default access modifier type if none is formally specified

    protected Access is limited to within the class definition and any class that inherits from the class

    internal Access is limited exclusively to classes defined within the current project assembly

    protected internal Access is limited to the current assembly and types derived from the containing class. All members in current project and all members in derived class can access the variables

    The default access for everything in C# is "the most restricted access you could declare for that member".

    So for example:

    namespace MyCompany{

    class Outer

    {

    void Foo() {}

    class Inner {}

    }}

    is equivalent to

    namespace MyCompany{

    internal class Outer

    {

    private void Foo() {}

    private class Inner {}

    }}

  • .NET Tutorial (shibasissengupta.blogspot.com)18

    Access Modifiers private Class Default access level of a class in C# is internal You can not declare a root level class as private in a na mespace A root level class in a namespace can only be public or internal

    (default)

    The following is allowed

  • .NET Tutorial (shibasissengupta.blogspot.com)19

    Classes Properties (another example of encapsulation)Traditional Getter/Setters:

    using System;

    public class Customer

    { private int m_id = -1;

    public int GetID() { return m_id; }

    public void SetID(int id) { m_id = id; }

    private string m_name = string.Empty;

    public string GetName() { return m_name; }

    public void SetName(string name) { m_name = name; } }

    public class CustomerManagerWithAccessorMethods{

    public static void Main()

    {

    Customer cust = new Customer();

    cust.SetID(1);

    cust.SetName("New Cust");

    Console.WriteLine(

    "ID: {0}, Name: {1}",

    cust.GetID(), cust.GetName()); }}

    New Way:

    using System;

    public class Customer {

    private int m_id = -1;

    public int ID { get { return m_id; } set { m_id = value; } }

    private string m_name = string.Empty;

    public string Name { get { return m_name; } set { m_name = value; } } }

    public class CustomerManagerWithProperties{

    public static void Main() {

    Customer cust = new Customer();

    cust.ID = 1;

    cust.Name = "New Cust";

    Console.WriteLine(

    "ID: {0}, Name: {1}",

    cust.ID,

    cust.Name); }}

    Properties can be made read-only. This is accomplished by having only a get accessor in the property implementation

  • .NET Tutorial (shibasissengupta.blogspot.com)20

    Classes - Inheritanceusing System;

    public class Parent

    {

    string parentString;

    public Parent()

    {

    Console.WriteLine("Parent Constructor.");

    }

    public Parent(string myString)

    {

    parentString = myString;

    Console.WriteLine(parentString);

    }

    public void print()

    {

    Console.WriteLine("I'm a Parent Class.");

    }

    }

    public class Child : Parent

    {

    public Child() : base("From Derived")

    {

    Console.WriteLine("Child Constructor.");

    }

    public new void print()

    {

    base.print();

    Console.WriteLine("I'm a Child Class.");

    }

    public static void Main()

    {

    Child child = new Child();

    child.print();

    ((Parent)child).print();

    }

    }

  • .NET Tutorial (shibasissengupta.blogspot.com)21

    Classes Inheritance (Contd..)Output:

    From Derived

    Child Constructor.

    I'm a Parent Class.

    I'm a Child Class.

    I'm a Parent Class.

    Derived classes can communicate with base classes during instantiation. The colon, ":", and keyword base call the base class constructor with the matching parameter list. If the code had not appended base("From Derived") to the Derived constructor, the code would have automatically called Parent(). The first line of output shows the base class constructor being called with the string "From Derived".

    Sometimes you may want to create your own implementation of a method that exists in a base class. The Child class does this by declaring its own print() method. The Child print() method hides the Parent print() method. The effect is the Parent print() method will not be called, unless we do something special to make sure it is called.

    Inside the Child print() method, we explicitly call the Parent print() method. This is done by prefixing the method name with "base.". Using the base keyword, you can access any of a base class public or protected class members. The output from the Child print() method is on output lines 3 and 4.

    Another way to access base class members is through an explicit cast. This is done in the last statement of the Child class Main() method. Remember that a derived class is a specialization of its base class. This fact allows us to perform a cast on the derived class, making it an instance of its base class. The last line of output shows the Parent print() method was indeed executed.

  • .NET Tutorial (shibasissengupta.blogspot.com)22

    Classes Inheritance (Contd..) - Multiple Vs Multi-Level In traditional concepts of OOP two forms of inheritance are talked

    about- Multiple inheritence: Where one class can inherit from

    multiple base classes. Multi- Level inheritence: Where once class can inherit

    from only one base class. A clear need of implementing multiple inheritence remains

    debatable subject over the years. C# only allows a weak form of multiple inheritence. All classes

    without an explicit base class is still the base class of the Object class. Otherwise, multiple inheritence can be implemented using interfaces.

    Microsoft's blog quotes on why multiple inheritence is generally not allowed - http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/85562.aspx

    There are new ways of implementing multiple- inheritence in C# 4.0 onwards - http://www.canerten.com/multiple-inheritance-in-c-using-dynamic-features/

  • .NET Tutorial (shibasissengupta.blogspot.com)23

    Classes - PolymorphismPolymorphism is often referred to as the third pillar of object-oriented

    programming, after encapsulation and inheritance.Polymorphism is a Greek word that means "many-shaped".

    using System;

    public class DrawingObject{public virtual void Draw(){Console.WriteLine("I'm just a generic drawing object.");}}

    using System;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.");}}

    public class Square : DrawingObject{public override void Draw(){Console.WriteLine("I'm a Square.");}}

    using System;public class DrawDemo{public static int Main( ){DrawingObject[] dObj = new DrawingObject[4];

    dObj[0] = new Line();dObj[1] = new Circle();dObj[2] = new Square();dObj[3] = new DrawingObject();

    foreach (DrawingObject drawObj in dObj){drawObj.Draw();}return 0;}}

  • .NET Tutorial (shibasissengupta.blogspot.com)24

    New Vs OverrideCase 1 : Base Class

    public void DoIt();

    Case 1 : Inherited class

    public new void DoIt();

    Case 2 : Base Class

    public virtual void DoIt();

    Case 2 : Inherited class

    public override void DoIt();

    virtual: indicates that a function may be overriden by an inheritor

    override: overrides the functionality of a virtual function in a base class, providing different functionality.

    new: hides the original function (which doesn't have to be virtual), providing different functionality. This should only be used where it is absolutely necessary

    If you're using real polymorphism you should always use override. The only time we can use new on methods is when the inherited implementation has nothing to do with the base implementation.

  • .NET Tutorial (shibasissengupta.blogspot.com)25

    IndexersIndexers allow your class to be used just like an array. On the inside of a class, you manage a

    collection of values any way you want. These objects could be a finite set of class members, another array, or some complex data structure. Regardless of the internal implementation of the class, its data can be obtained consistently through the use of indexers.

    using System;

    /// /// A simple indexer example./// class IntIndexer{private string[] myData;

    public IntIndexer(int size){myData = new string[size];

    for (int i=0; i < size; i++){myData[i] = "empty";}}

    public string this[int pos]{get{return myData[pos];}set{myData[pos] = value;}}

    static void Main(string[] args){int size = 10;

    IntIndexer myInd = new IntIndexer(size);

    myInd[9] = "Some Value";myInd[3] = "Another Value";myInd[5] = "Any Value";

    Console.WriteLine("\nIndexer Output\n");

    for (int i=0; i < size; i++){Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]);}}}

  • .NET Tutorial (shibasissengupta.blogspot.com)26

    ArraysInitializing Arrays:

    int[] numbers = new int[5] {1, 2, 3, 4, 5};

    string[] names = new string[3] {"Matt", "Joanne", "Robert"};

    You can omit the size of the array, like this:

    int[] numbers = new int[] {1, 2, 3, 4, 5};

    string[] names = new string[] {"Matt", "Joanne", "Robert"};

    You can also omit the new operator if an initializer is provided, like this:

    int[] numbers = {1, 2, 3, 4, 5};

    string[] names = {"Matt", "Joanne", "Robert"};

    Arrays Are Objects:

    In C#, arrays are actually objects. System.Array is the abstract base type of all array types. You can use the properties, and other class members, that System.Array has. An example of this would be using the Length property to get the length of an array.

    int[] numbers = {1, 2, 3, 4, 5};

    int LengthOfNumbers = numbers.Length;

    The System.Array class provides many other useful methods/properties, such as methods for sorting, searching, and copying arrays.

    Using foreach on Arrays:

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

    foreach (int i in numbers)

    {

    System.Console.WriteLine(i);

    }

  • .NET Tutorial (shibasissengupta.blogspot.com)27

    Collections For many applications, you want to create and manage groups of related

    objects. There are two ways to group objects: by creating arrays of objects, and by creating collections of objects

    Arrays are most useful for creating and working with a fixed number of strongly-typed objects

    Collections provide a more flexible way to work with groups of objects. Unlike arrays, the group of objects you work with can grow and shrink dynamically as the needs of the application change.

    For some collections, you can assign a key to any object that you put into the collection so that you can quickly retrieve the object by using the key

    If your collection contains elements of only one data type, you can use one of the classes in the System.Collections.Generic namespace.

    System.Collection Classes The classes in the System.Collections namespace do not store elements as specifically

    typed objects, but as objects of type Object.

    Whenever possible, you should use the generic collections in the System.Collections.Generic namespace or the System.Collections.Concurrent namespace instead of the legacy types in the System.Collections namespace.

    The following table lists some of the frequently used classes in the System.Collections namespace:

    Class Description

    ArrayList Represents an array of objects whose size is dynamically increased as required

    Hashtable Represents a collection of key/value pairs that are organized based on the hash code of the key

    Queue Represents a first in, first out (FIFO) collection of objects

    Stack Represents a last in, first out (LIFO) collection of objects

  • .NET Tutorial (shibasissengupta.blogspot.com)28

    Boxing and UnboxingBoxing is the process of converting a value type to the type object or to any

    interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. The concept of boxing and unboxing underlies the C# unified view of the type system in which a value of any type can be treated as an object.

    In the following example, the integer variable i is boxed and assigned to object o.

    int i = 123;

    // The following line boxes i.

    object o = i;

    The object o can then be unboxed and assigned to integer variable i:

    o = 123;

    i = (int)o; // unboxing

    In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally

    Boxing & Unboxing in Collections:

    int i = 10;

    ArrayList arrlst = new ArrayList();

    arrlst.Add(i); // Boxing occurs automatically

    int j = (int)arrlst[0]; // Unboxing occurs

  • .NET Tutorial (shibasissengupta.blogspot.com)29

    GenericsThe following block of Java code illustrates a problem that exists when not

    using generics. First, it declares an ArrayList of type Object. Then, it adds a String to the ArrayList. Finally, it attempts to retrieve the added String and cast it to an Integer.

    List v = new List();

    v.add("test"); //boxing happens here

    Integer i = (Integer)v.get(0); // Run time error

    Although the code compiles without error, it throws a runtime exception (InvalidCastException in C# and java.lang.ClassCastException in Java) when executing the third line of code. This type of problem can be avoided by using generics and is the primary motivation for using generics.

    Using generics, the above code fragment can be rewritten as follows:

    List v = new List();

    v.add("test");

    Integer i = v.get(0); // (type error) Compile time error

    System.Collections.Generic Classes

    You can create a generic collection by using one of the classes in the System.Collections.Generic namespace. A generic collection is useful when every item in the collection has the same data type.

    The following table lists some of the frequently used classes of the System.Collections.Generic namespace:

    Class Description

    Dictionary Represents a collection of key/value pairs that are organized based on the key.

    List Represents a list of objects that can be accessed by index. Provides methods to search, sort, and modify lists.

    Queue Represents a first in, first out (FIFO) collection of objects

    SortedList Represents a collection of key/value pairs that are sorted by key based on the associated implementation

    Stack Represents a last in, first out (LIFO) collection of objects.

  • .NET Tutorial (shibasissengupta.blogspot.com)30

    Generics (Contd..)No boxing /Unboxing Required in Generics:

    List list1 = new List();

    list1.Add(3); // No boxing

    Interger i=list1.get(0); // No unboxing

    Boxing doesn't happen here because the array that backs the List is T[], not object[]. Therefore, the runtime knows your items are integers and doesn't need to box them.

  • .NET Tutorial (shibasissengupta.blogspot.com)31

    Interface An interface looks like a Class but has no implementation

    The only thing it contains are declarations

    No fields can be defined in an interface

    Inherited by Class/Structs which must provide an implementation for each interface method

    Interface defines a contract

    If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

    Interface naming convention is to start with 'I'

    public interface IEmployee {

    String ID { get; set; }

    String FirstName { get; set; }

    String LastName { get; set; }

    String Add();

    String Delete(); }

    public class Emp_fulltime2 : IEmployee {

    protected String id;

    protected String lname;

    protected String fname;

    public Emp_fulltime2() { // // TODO: Add constructor logic here // }

    public String ID { get { return id; } set { id = value; } }

    public String FirstName { get { return fname; } set { fname = value; } }

    public String LastName { get { return lname; } set { lname = value; } }

    public String Add() { return "Fulltime Employee " + fname + " added."; }

    public String Delete() { return "Fulltime Employee " + fname + " deleted."; } }

    private void InterfaceExample_Click(object sender, System.EventArgs e) {

    try { IEmployee emp;

    Emp_fulltime2 emp1 = new Emp_fulltime2();

    emp = emp1; emp.ID = "2234";

    emp.FirstName= "Rahman" ; emp.LastName = "Mahmoodi" ; //call add method od the object MessageBox.Show(emp.Add().ToString()); //call the CalculateWage method MessageBox.Show(emp.CalculateWage().ToString()); }

    catch(Exception ex) { MessageBox.Show(ex.Message); } }

  • .NET Tutorial (shibasissengupta.blogspot.com)32

    Abstract Class An Abstract class without any implementation just looks like an Interface

    An abstract class is a special kind of class that cannot be instantiated

    It only allows other classes to inherit from it but cannot be instantiated

    When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract

    public abstract class Employee { //we can have fields and properties //in the Abstract class protected String id;

    protected String lname;

    protected String fname;

    public abstract String ID { get; set; }

    public abstract String FirstName { get; set; }

    public abstract String LastName { get; set; }

    public String Add() { return "Employee " + id + " " + lname + " " + fname + " added"; }

    public String Update() { return "Employee " + id + " " + lname + " " + fname + " updated"; }

    public abstract String CalculateWage();}

    public class Emp_Fulltime : Employee

    { //uses all the properties of the //Abstract class therefore no //properties or fields here!

    public Emp_Fulltime() { }

    public override String ID { get { return id; } set { id = value; } }

    public override String FirstName { get { return fname; } set { fname = value; } }

    public override String LastName { get { return lname; } set { lname = value; } }

    //common methods that are //implemented in the abstract class

    public new String Update() { return base.Update(); }

    public override String CalculateWage() { return "Full time employee " + base.fname + " is calculated " + "using the Abstract class..."; }

    private void cmdAbstractExample_Click(object sender, System.EventArgs e)

    { Employee emp;

    emp = new Emp_Fulltime();

    emp.ID = "2244"; emp.FirstName= "Maria" ; emp.LastName = "Robinlius" ; MessageBox.Show(emp.Add().ToString()); //call the CalculateWage method MessageBox.Show(emp.CalculateWage().ToString()); }

  • .NET Tutorial (shibasissengupta.blogspot.com)33

    Interface Vs Abstract Class As always there is a trade-off, an interface gives you freedom with regard

    to the base class, an abstract class gives you the freedom to add new methods later. Erich Gamma

    You cant go and change an Interface without having to change a lot of other things in your code, so the only way to avoid this would be to create a whole new Interface, which might not always be a good thing.

    Abstract classes should primarily be used for objects that are closely related, whereas interfaces are better at providing common functionality for unrelated classes.

    An abstract class, in contrast to interface, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. The catch is, code using it must use your class as the base.In Jav/C#a, a class can inherit from only one base class.

    Feature Interface Abstract Class

    Multiple inheritance A class may inherit several interfaces.

    A class may inherit only one abstract class

    Default implementation An interface cannot provide any code, just the signature

    An abstract class can provide complete, default code and/or just the details that have to be overridden

    Access Modfiers An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public

    An abstract class can contain access modifiers for the subs, functions, properties

    Core VS Peripheral Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.

    An abstract class defines the core identity of a class and there it is used for objects of the same type.

    Speed Requires more time to find the actual method in the corresponding classes

    Fast

  • .NET Tutorial (shibasissengupta.blogspot.com)34

    Delegates In Common Language Infrastructure (CLI), a delegate is a form of type-

    safe function pointer usually used in an observer pattern as a means telling which method to call when an event is triggered

    Delegate is a type which holds the method(s) reference in an object

    Used to call a method asynchronously

    Declaration:

    public delegate type_of_delegate delegate_name()

    public delegate int mydelegate(int delvar1,int delvar2)

    public delegate double Delegate_Prod(int a,int b);

    class Class1 {

    static double fn_Prodvalues(int val1,int val2) { return val1*val2; }

    static void Main(string[] args) {

    //Creating the Delegate Instance

    Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues); Console.Write("Please Enter Values");

    int v1 = Int32.Parse(Console.ReadLine());

    int v2 = Int32.Parse(Console.ReadLine()); //use a delegate for processing double res = delObj(v1,v2);

    Console.WriteLine ("Result :"+res);

    Console.ReadLine(); } }

  • .NET Tutorial (shibasissengupta.blogspot.com)35

    Events Today's graphical user interface (GUI) programming model uses event-

    driven programming. A modern program presents the user interface and waits for the user to take an action. The user might take many different actions, such as choosing among menu selections, pushing buttons, updating text fields, clicking icons, and so forth. Each action causes an event to be raised. Other events can be raised without direct user action, such as events that correspond to timer ticks of the internal clock, email being received, file-copy operations completing, and so forth.

    An event is the encapsulation of the idea that "something happened" to which the program must respond. Events and delegates are tightly coupled concepts because flexible event handling requires that the response to the event be dispatched to the appropriate event handler. An event handler is typically implemented in C# via a delegate

    Publishing and Subscribing (Observer Design Pattern)

    In C#, any object can publish a set of events to which other classes can subscribe. For example, a button might notify any number of interested observers when it is clicked. The button is called the publisher because the button publishes the Click event and the other classes are the subscribers because they subscribe to the Click event.

    Note that the publishing class does not know or care who (if anyone) subscribes; it just raises the event. Who responds to that event, and how they respond, is not the concern of the publishing class.

    The publisher and the subscribers are decoupled by the delegate. The publisher can change the reason of raising the event and the subscribers can change the way they respond.

    Events and Delegates (Working together)

    Events in C# are implemented with delegates

    The publishing class defines a delegate. The subscribing class does two things: first, it creates a method that matches the signature of the delegate, and then it creates an instance of that delegate type

    When the event is raised, the subscribing class's methods are invoked through the delegate.

    A method that handles an event is called an event handler. You can declare your event handlers

    By convention, event handlers in the .NET Framework always return void and take two parameters. The first parameter is the "source" of the event (that is, the publishing object). The second parameter is an object derived from EventArgs. Your event handlers will need to follow this design pattern

  • .NET Tutorial (shibasissengupta.blogspot.com)36

    Events (Contd..)using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading;

    namespace Example_17_3_ _ _ _Delegates_and_Events{

    public class TimeInfoEventArgs : EventArgs

    {

    public int hour;

    public int minute;

    public int second;

    public TimeInfoEventArgs(int hour, int minute, int second)

    {

    this.hour = hour;

    this.minute = minute;

    this.second = second;

    }

    }

    // The publisher: the class that other classes

    // will observe. This class publishes one delegate:

    // SecondChanged.

    public class Clock

    {

    private int hour;

    private int minute;

    private int second;

    // the delegate the subscribers must implement

    public delegate void SecondChangeHandler(object clock, TimeInfoEventArgs timeInformation);

    // an instance of the delegate with event keyword added

    public event SecondChangeHandler SecondChanged;

  • Events (Contd..) // set the clock running

    // it will raise an event for each new second

    public void Run( )

    {

    for (; ; )

    { // sleep 10 milliseconds

    Thread.Sleep(100);

    // get the current time

    System.DateTime dt = System.DateTime.Now;

    // if the second has changed

    // notify the subscribers

    if (dt.Second != second)

    {

    // create the TimeInfoEventArgs object

    // to pass to the subscriber

    TimeInfoEventArgs timeInformation =

    new TimeInfoEventArgs(dt.Hour, dt.Minute, dt.Second);

    // if anyone has subscribed, notify them

    if (SecondChanged != null)

    {

    SecondChanged(this, timeInformation);

    }

    }

    // update the state

    this.second = dt.Second;

    this.minute = dt.Minute;

    this.hour = dt.Hour;

    }

    }

    }

    .NET Tutorial (shibasissengupta.blogspot.com)37

  • Events (Contd..)// A subscriber: DisplayClock subscribes to the

    // clock's events. The job of DisplayClock is

    // to display the current time

    public class DisplayClock

    { // given a clock, subscribe to

    // its SecondChangeHandler event

    public void Subscribe(Clock theClock)

    { theClock.SecondChanged +=

    new Clock.SecondChangeHandler(TimeHasChanged);

    }

    // the method that implements the

    // delegated functionality

    public void TimeHasChanged(object Clock, TimeInfotheEventArgs ti)

    {

    Console.WriteLine("Current Time: {0}:{1}:{2}",

    ti.hour.ToString( ), ti.minute.ToString( ), ti.second.ToString( ));

    }

    }

    // a second subscriber whose job is to write to a file

    public class LogCurrentTime

    {

    public void Subscribe(Clock theClock)

    { theClock.SecondChanged +=

    new Clock.SecondChangeHandler(WriteLogEntry);

    }

    // this method should write to a file

    // we write to the console to see the effect

    // this object keeps no state

    public void WriteLogEntry(object theClock, TimeInfoEventArgs ti)

    {

    Console.WriteLine("Logging to file: {0}:{1}:{2}",

    ti.hour.ToString( ), ti.minute.ToString( ), ti.second.ToString( ));

    }

    }

    .NET Tutorial (shibasissengupta.blogspot.com)38

  • public class Tester

    {

    public void Run( )

    {

    // create a new clock

    Clock theClock = new Clock( );

    // create the display and tell it to

    // subscribe to the clock just created

    DisplayClock dc = new DisplayClock( );

    dc.Subscribe(theClock);

    // create a Log object and tell it

    // to subscribe to the clock

    LogCurrentTime lct = new LogCurrentTime( );

    lct.Subscribe(theClock);

    // Get the clock started

    theClock.Run( );

    }

    }

    What is the event keyword is not used?

    The event keyword indicates to the compiler that the delegate can be invoked only by the defining class, and that other classes can subscribe to and unsubscribe from the delegate using only the appropriate += and -= operators, respectively

    The following code will be possible if the event keyword is not used

    public void Subscribe(Clock theClock)

    {

    theClock.SecondChanged =

    new Clock.SecondChangeHandler(WriteLogEntry);

    }

    This assignment will replace the multicast delegate. Also, other classes can directly invoke the event which is not intended.

    .NET Tutorial (shibasissengupta.blogspot.com)39

  • Exception Handlingusing System;

    using System.IO;

    class FinallyDemo{static void Main(string[] args){FileStream outStream = null;FileStream inStream = null;

    try{outStream = File.OpenWrite("DestinationFile.txt");inStream = File.OpenRead("BogusInputFile.txt");}catch(Exception ex){Console.WriteLine(ex.ToString());}finally{if (outStream != null){outStream.Close();Console.WriteLine("outStream closed.");}if (inStream != null){inStream.Close();Console.WriteLine("inStream closed.");}}}}

    Exception Handling Best Practices

    Try to write code to avoid exception as much possible. e.g.

    Instead of writing -

    int testInt=Int32.ParseInt(testString);

    int testInt=Int32.TryParse(testString,out testInt);

    Use try/catch blocks around code that can potentially generate an exception, and use a finally block to clean up resources, if necessary

    In catch blocks, always order exceptions from the most specific to the least specific

    Throw exceptions instead of returning an error code

    In most cases, use the predefined .NET Framework exception types. Introduce a new exception class only when a predefined one doesn't apply

    Throw an InvalidOperationException exception if a property set or method call is not appropriate given the object's current state

    Throw an ArgumentException exception or a class derived from ArgumentException if invalid parameters are passed

    For most apps, derive custom exceptions from the Exception class. Deriving from the ApplicationException class doesn't add significant value

    End exception class names with the word "Exception". For example:public class MyFileNotFoundException : Exception

    {

    } .NET Tutorial40 .NET Tutorial (shibasissengupta.blogspot.com)

  • Design Patterns In software engineering, a design pattern is a general reusable solution to a

    commonly occurring problem within a given context

    A design pattern is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations

    Design patterns gained popularity in computer science after the book Design Patterns: Elements of Reusable Object-Oriented Software was published in 1994 by the so-called "Gang of Four" (Gamma et al.), which is frequently abbreviated as "GOF

    Design patterns were mainly grouped into the following categories -

    Creation Patterns: Design patters that deal with object creation mechanisms.Creational design patterns are further categorized into Object-creational patterns and Class-creational patterns.In greater details, Object-creational patterns defer part of its object creation to another object, while Class-creational patterns defer its object creation to subclasses. Five well known creation patterns are -

    Abstract Factory pattern

    Builder pattern

    Factory Method Pattern

    Prototype Pattern

    Singleton Pattern

    Structural Patterns: Structrual patterns are meant to ease the design by identifying a simple way to realize relationships between entities. Examples -

    Adapter Pattern

    Aggregate Pattern

    Bridge Pattern

    Behavioural Design Patterns: These patterns identify common communication patterns between objects. By doing so, these patterns increase flexibility in carrying out this communication. Examples -

    Chain of Responsibility

    Command Pattern

    Observer Pattern

    Schedules Task Pattern

    Iterator Pattern

    Protocol Stack

    .NET Tutorial41 .NET Tutorial (shibasissengupta.blogspot.com)

  • Factory PatternThe factory pattern method is a popularly used design pattern and it is very useful to restrict clients from knowing the actual business logic methods, it is useful to create a decoupled system, and it is useful to eliminate object creation in a client environment.

    For example we have a scenario where based on user input we need to call particular class methods. I have a customer input screen. He enters his choice whether they want to buy a bike or a car. Normally we get input from the user and based on that will create an object in the client class and call those methods like below.

    If (choice == Car){

    // call car class and its methodsCar c = new car();c.buy();

    }If (choice == bike){

    // call bike class and its methodsBike b = new Bike();b.buy()

    }

    In case in future if there is any other vehicle added then we need to change the client functionality. Worse, if there are many client classes using these classes all of them might neeed to be updated.

    public interface IChoice { string Buy(); }

    A new class will be added and this class we will be called factory class. This class sits between the client class and the business class and based on user choice it will return the respective class object through the interface.

    // // Factory Class // public class FactoryChoice { static public IChoice getChoiceObj(string cChoice) { IChoice objChoice=null; if (cChoice.ToLower() == "car") { objChoice = new clsCar(); } else if (cChoice.ToLower() == "bike") { objChoice = new clsBike(); } else { objChoice = new InvalidChoice(); } return objChoice; } }

    //Business classes Bike//public class clsBike:IChoice { #region IChoice Members public string Buy() { return ("You choose Bike"); } #endregion } //Business Class Carpublic class clsCar:IChoice { IChoice Members public string Buy() { return ("You choose Car"); } }

    From the client class call the factory class object and it will return the interface object. Through the interface object we will call the respective method//Client classIChoice objInvoice;objInvoice = FactoryClass.FactoryChoice.getChoiceObj(txtChoice.Text.Trim());MessageBox.Show(objInvoice.Buy());

    In future if we need to add a new vehicle then there is no need to change the client class, simply return that object using the factory class..NET Tutorial42 .NET Tutorial (shibasissengupta.blogspot.com)

  • Singleton PatternSingleton Patterns are implemented when there is a need for only instance of a class throughout the application.

    Commonly used in Logging, shareable services etc. Multiple logger instances can slow down the system performance using singleton pattern in these case is a perfect example.

    public sealed class BusinessRulesManager{private BusinessRulesManager(){}

    public static BusinessRulesManager GetInstance{get{return BusinessRulesManagerImpl.instance;}}

    public void DisplayRules(){Console.WriteLine("Single instance object");}

    private class BusinessRulesManagerImpl{static BusinessRulesManagerImpl(){}

    internal static readonly BusinessRulesManager instance = new BusinessRulesManager();}}

    private static void Singleton(){BusinessRulesManager businessRulesManager = BusinessRulesManager.GetInstance;businessRulesManager.DisplayRules();

    Console.ReadKey();}

    The class is marked as sealed which means that inheritance is not allowed for it

    a private nested class is used to provide access to the instance that must only exist once in the application.

    This nested class has a static default constructor and an internal static read-only instance of the object. Only the container class has access to the instance

    .NET Tutorial43 .NET Tutorial (shibasissengupta.blogspot.com)

  • Adapter PatternThe Adapter Design Pattern allows you to make an existing class work with other existing class libraries without changing the code of the existing class.

    Say, there is an Employee Class which implements the Iemployee interface. The organization tree is constructed using this Employee class. You are then given the Consultant class and you need to plug this Consultant class into the organization tree. The Consultant class is the Adaptee. The way to do this is by creating the adapter class named the EmployeeAdapter.

    using System.Collections.Generic; class Program {

    static void Main(string[] args) { List list = new List(); list.Add(new Employee("Tom")); list.Add(new Employee("Jerry")); list.Add(new EmployeeAdapter(new Consultant("Bruno"))); //*** Code below from the existing library does not need to be changed *** ShowHappiness(list); } //*** Code below from the existing library does not need to be changed *** static void ShowHappiness(List list) { foreach (IEmployee i in list) i.ShowHappiness(); } } //from the existing library, does not need to be changed public interface IEmployee { void ShowHappiness(); }

    public class Employee : IEmployee { private string name; public Employee(string name) { this.name = name; } void IEmployee.ShowHappiness() { Console.WriteLine("Employee " + this.name + " showed happiness"); } }

    //existing class does not need to be changed public class Consultant { private string name; public Consultant(string name) { this.name = name; } protected void ShowSmile() { Console.WriteLine("Consultant " + this.name + " showed smile"); } }

    public class EmployeeAdapter : IEmployee { Consultant _consultant; public EmployeeAdapter(Consultant consultant) { _consultant = consultant; } void IEmployee.ShowHappiness() { consultant.ShowSmile(); //call the adaptee Consultant class } }

    .NET Tutorial44 .NET Tutorial (shibasissengupta.blogspot.com)

  • Observer PatternMany a times, we need one part of our application updated with the status of some other part of the application. One way to do this is to have the receiver part repeatedly check the sender for updates, but this approach has two main problems. First, it takes up a lot of CPU time to check the new status and second, depending on the interval we are checking for change we might not get the updates "immediately".

    This problem has one easy solution i.e. Observer Pattern.

    Events and delegates uses the oberserver pattern in C#

    According to MSDN MVC is a fundamental design pattern for the separation of user interface logic from business logic.

    .NET Tutorial45 .NET Tutorial (shibasissengupta.blogspot.com)

  • Iterator Pattern Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation

    "Enumerated" means to count off the members of a set/collection/category one by one (usually in order, usually by name).An enumerable then is an object that can step through a series of other objects one by one.

    You use the iterator pattern most likely in your every day work maybe without being aware of:

    List all = new List() { "you", "me", "everyone" };...foreach(string who in all){ Console.WriteLine("who: {0}!", who);}

    Behind the scenes, the foreach loop translates into -using (var it = all.GetEnumerator())while (it.MoveNext()){ string who = it.Current; ...}

    A class that can be used in a foreach loop must provide a IEnumerator GetEnumerator() { ... } method. The method name is reserved for that purpose.

    Some classes may also provide the non-generic IEnumerator GetEnumerator() { ... } method. This is from the older days where there were no generics yet.

    So, the core of the C# implementation of the Iterator Pattern is the GetEnumerator() method

    What is an Iterator?An iterator provides a means to iterate (i.e. loop) over some items. The sequence of elements is given by the implementations of the IEnumerator/IEnumerator interfaces:

    namespace System.Collections{ public interface IEnumerator { object Current { get; } bool MoveNext(); void Reset(); }}

    .NET Tutorial46 .NET Tutorial (shibasissengupta.blogspot.com)

  • Iterator Pattern (Contd..)namespace System.Collections.Generic{ public interface IEnumerator : IDisposable, IEnumerator { T Current { get; } }}

    The pattern is basically given by MoveNext() and Current. The semantics is that one has to first call MoveNext() to get to the first element. If MoveNext() returns false, then there is no more element. Current returns the current element. You are not supposed to call Current if the preceeding MoveNext() returned false.

    The MoveNext() gives the next element in the sequence of elements - what ever that sequence is, e.g. from first to last, or sorted by some criteria, or random, etc.

    We know now how to apply the iterator pattern (e.g. in a foreach loop) and that this is possible for all classes that provide the above mentioned GetEnumerator() method (the iterator).

    .NET Tutorial47 .NET Tutorial (shibasissengupta.blogspot.com)

    .NET StoryASP.NETCommon Language Runtime - CLRCommon Language Infrastructure (CLI) & Common Intermediate Language (CIL)Getting StartedValue Types, Reference Types, aliases and difference from JavaValue Types, Reference Types, aliases and difference from Java(contd..)Slide 8Slide 9TypesUnary OperatorsControl StatementsNested NamespaceThree pillars of Object Oriented ProgrammingClassesClasses (Contd..)Access ModifiersAccess Modifiers private ClassClasses Properties (another example of encapsulation)Classes - InheritanceClasses Inheritance (Contd..)Classes Inheritance (Contd..) - Multiple Vs Multi-LevelClasses - PolymorphismNew Vs OverrideIndexersArraysCollectionsBoxing and UnboxingGenericsGenerics (Contd..)InterfaceAbstract ClassInterface Vs Abstract ClassDelegatesEventsEvents (Contd..)Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47