Day 25

Difference between Convert.ToString() and ToString()
-(Important for interview Question)


Convert.ToString() handles null,while ToString() doesn't and throws a NULL Reference Exception

Day 24-Partial classes

Partial classes allow us to split a class into  2 or more files.All these part are then combined into single class,when the application is compiled.The partial keyword can also be used to split a struct or an interface over two or more files

  • Example-Visual studio uses partial classes to  separate automatically generated system code from the developers code.When you add a webform,two .cs file is generated
  1. Webform.aspx.cs-Contain the developer code
  2. Webform.aspx.designer.cs-Contain the system generated code


  •     All the partial class definitions must be in the same assembly and          namespace.
  •     All the parts must have the same accessibility like public or private,        etc.
  •     If any part is declared abstract then the whole class is declared of         the same type.
  •     If any part is declared  sealed  then the whole class is declared of        the same type.
  •     Different parts can have different base types and so the final class will   inherit all the base types
  •     The Partial modifier can only appear immediately before the                keywords class, struct, or interface.
  •     Nested partial types are allowed.



Day 23-Access Modifier

  • Private-Only within the containing classes
  • Public-Any where,NO restrictions
  • Protected-Within the containing class and the class derived from the the containing class
  • Internal-Anywhere with in the containing assembly
  • Protected Internal-Anywhere with in the containing assembly,and from within a derived class in any another assembly



Click To Zoom Image

Click To Zoom Image

Click To Zoom Image
 

Day 22-Collection in c#

Click Here to View Practical Example

Types of Collections:

1. ArrayList
2. Stack
3. Queue
4. Hashtable
5. SortedList



  • ArrayList can store elements of any data type. Like array, we don't need to specify the size of ArrayList at the time of it declaration. ArrayList collection class provide you to dynamically add or remove items from array. You can use ArrayList instead of array when you don't know the how many items will inserted. ArrayList is more used when all items are different data type.

  • Stack is a kind of collection that can useful when you need to store data in a specific order for sequential processing. A Stack works on principle of LIFO - Last In First Out, which means that last item inserted into the stack will be the first item to be removed from stack.The adding of an item into stack is known as Push operation, removing an item from stack is known as Pop operation and if an item only read from top of stack is known as Peek operation.

  • Similar to Stack, Queue can useful when you need to store data in a specific order for sequential processing.A Queue works on the principle of FIFO - First In First Out, which means that the first item inserted into Queue will be first remove form Queue.The adding of an item into queue is known as Enqueue operation, removing an item from queue is known as Dequeue operation and if an item only read from top of queue is known as Peek operation.


  • HashTable stores any data type of items as key-value pairs. The data is stored in Hashtable basis of key and can be accessed by the key rather than index of items.Each items in the HashTable in uniquely identified by it's key.


  • A SortedList is a collection that contains key-value pairs. The data is stored in SortedList basis of key and it can be accessed by the key or the index and because of it is sorted.

Day 21-This Keyword in C#

This keyword refers to current instance of the class. This keyword use to declare indexer and also in extension method.


  • You can call same class property using this keyword.
  • You can call page on code behind page.
  • You can create extension method using this keyword.

Day 20-Garbage Collection in C#

Unused object automatically released by automatic garbage collection in Asp.Net

Garbage collector executes automatically when system has low physical memory.

Advantage of Garbage Collector
1. No worry about memory management

2. Allocate object memory on managed heap efficiently.

3. Reclaims objects that no longer being used, clears their memory and keeps the memory for future allocations.

4. Provides memory safety by making sure that object cannot use the content of other object.

Day 19-Difference between Types and Type Member

Classes,structs,enum,interfaces,delegates are called as types &
fields,properties,constructors,methods etc that normally reside in a type are called as type member


In C# there are 5 different access modifiers
  • Private
  • Protected
  • Public
  • Internal
  • Protected Internal


Type Member can have all the access modifiers,where as the types can have only 2 (internal,public) of the  5 access modifiers

Day 18-Enum

Enum are strongly typed constants

If a Program uses set of integral number,consider replacing them with enum ,Otherwise the program become less Readable ,Maintainable

  • The default underlying  type of an enum is int
  • The default value  for first element is zero and get increment by 1
  • Enum are value type 

Click To Zoom Image-Part1

Click To Zoom Image-Part2

Day 17-Exception Handling

When program is executing then at runtime error may be occur due to wrong data type conversion, passing invalid connection string, permission failed while opening a file and there are lots of condition. Error which is occurred at runtime is known as exception. Exception will cause abnormal termination of program execution, if no exception handler present for a given exception, then the program terminate abnormally and stops execution with an error details. This is very bad impression on user so in that condition we use exception handling mechanism in .Net application.

Exceptions are type that all derive from System.Exception class.

 C# exception handling is built upon four keywords: try, catch, finally, and throw.

    try − A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more catch blocks.

    catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

    finally − The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.

    throw − A program throws an exception when a problem shows up. This is done using a throw keyword.
 


  •     Use the try, catch and finally blocks to handle exceptions in C#.
  •     The try block must be followed by a catch or finally block or both.
  •     A multiple catch block is allowed with different exception filters.
  •     The finally block must come after the try or catch block.
  •     The finally block will always execute irrespective of
  •     The finally block is appropriate place for disposing objects.
  •     The finally block cannot have a return or break
  •     Nested try-catch blocks are allowed in C#.

Click To Zoom Image-Exception Read And Write File
 

 

Day 16-Delegates

Delegates can be used to hold the reference of method. A delegate is a type that safety encapsulates a method and it is similar to function pointer in C and C++. Delegates can be used to define call back methods. Delegate class hold only those methods which have same signature. It allow methods to be passed as parameters. A delegate can be seen as a placeholder for method.

Delegates can be chained multiple methods together and can be called on a single event.

Suppose if you have multiple methods with same signature which have same return type & number of parameters and want to call all the methods with single object then we can go for delegates.

Creating and using delegates involve the four steps:

1. Declaring a delegates

2. Defining a delegate method

3. Creating delegate object

4. Invoking delegate object


Types of Delegates

1. Single cast delegate

Delegate that represent only a single method is known as Single Cast Delegate.
 

2. Multi Cast Delegate

Delegate that represent more than one method is known as Multi Cast Delegate.

Use += operator to add reference of more method to delegate

Use -= operator to remove reference of a method
 


Click To Zoom Image

Click To Zoom Image

Day 15-Abstract classes vs Interface

  • Abstract class can have implementation for some of its member but interface can't have implementation for any of its member
  • Abstract class can have field while Interface cannot have field
  • Abstract class can have access modifiers while interface can not have access modifiers

Day 14-Abstract class

The class which cannot be instantiated known as abstract class. Abstract class has no object. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. Abstract class can contain abstract and non-abstract members. Abstract member has only declaration and non-abstract member has declaration and implementation in abstract class.


Properties of abstract class
  • Abstract class can contain only abstract members or only non-abstract members or both.
  • Abstract class cannot be instantiated.
  • Abstract method cannot be static.
  • Abstract method cannot be virtual.
  • Abstract method cannot be private.
  • A class cannot be inherit more than one abstract class. 
  • Abstract class can contain properties. 
  • Abstract method can be overloaded. 

Day 13-Struct

Just Like classes struct can have
  • Private Fields
  • Public Properties
  • Constructors
  • Methods
Difference Between Classes and Struct

  • Struct is a value type where class is a reference type
  • Struct are stored in stack where as classes are stored in heap
  • Value type hold there value in memory where they are declared but reference type holds a reference to object in a memory
  • Value type are destroyed immediately after the scope is lost,where as reference type only the reference variable is destroyed after the scope is lost,The object is later destroyed by garbage collector
  • Struct can't have destructors while class can have 
  • Struct are sealed type

Day 12-Difference Between method overriding and method hiding

  • In Method Overriding a base class reference variable pointing to a child class object ,will invoke the overridden method . In Method hiding base class reference variable pointing to a child class object ,will invoke the hidden method
  • For hiding the base class method from derived class simply declare the derived class method with the new keyword.
    Whereas in C#, for overriding the base class method in a derived class, you need to declare the base class method as virtual and the derived class method as overridden .

Day 11-Polymorphism

Click Here to View Practical Example

There are two types of polymorphism


  1. Compile Time(Early binding or Overloading or static binding)
  2. Run Time (Late binding or Overriding or dynamic binding)

Compile time polymorphism means we will declare methods with same name but different signatures (Parameter)because of this we will perform different tasks with same method name. This compile time polymorphism also called as early binding or method overloading.

Syntax


public void NumbersAdd(int a, int b)
{
Console.WriteLine(a + b);
}
public void NumbersAdd(int a, int b, int c)
{
Console.WriteLine(a + b + c);
}

Method Overloading or compile time polymorphism means same method names with different signatures (different parameters)

Run time polymorphism or method overriding means same method names with same signatures.
In this run time polymorphism or method overriding we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance principle and using “virtual & override” keywords.
In Base Class - Method is declared virtual and in derived class we override the same method

Syntax

//Base Class
public class Bclass
{
public virtual void Sample1()
{
Console.WriteLine("Base Class");
}
}
// Derived Class
public class DClass : Bclass
{
public override void Sample1()
{
Console.WriteLine("Derived Class");
}



Day 10-Method Hiding

1-Use a new Keyword to hide a base class method
Click To Zoom Image

Click To Zoom Image

Day 9-Interface

Click Here to View Practical Example


1. Interface is simply defined by interface keyword.
2. Interface has no implementation it has only the signature. Interface have only abstract method.
3. Interface cannot have access modifier private, protected, protected internal.
4. Interface cannot have data field.
5. The class must implements all features of inherited interface.
6. A class implement more than one interface but only one abstract class inherit.


Day 8-Inheritance



Inheritance Click Here to View Practical Example
 Advantage
=>Code Reuse
=> Reduce Time & errors

All the code Which is common is in base class
$Syntax
Public class baseclass
{

}
Public class derived class:baseclass
{
}

All  the field in base class is available or invoked in derived class

c# support only single class inheritance


Day 7-Static and Instance Method

1-When  a method include static,that method is said to be a static method
    Else
    The Method is said to be an Instance Method

2-Static Method is invoked using a class name,where instance method is invoked
   using a instance of  a class name

3-In Instance Method,Multiple instance of a class is created and When Method is
   Static There is no instance of that method and you can invoke only that one
   definition of the static method


Click To Zoom Image

Day 6 -Constructor

Constructor -Same as Class Name Click Here to View Practical Example

Key Points

  • A class can have number of constructors
  • A constructor doesn't have any return type, not even void
  • A static constructor cannot be a parameterized constructor.
  • Within a class you can create only one static constructor

    Types of constructor

    1. Default Constructor
    2. Static Constructor
    3. Private Constructor
    4. Parameterized Constructor
    Default Constructor
    A constructor without having any parameters is Known as default constructor
    Parameterized Constructor
    A constructor with at least one parameter is Known as parameterized constructor
    Private Constructor
    A constructor which has private access modifier is known as Private constructor. When a constructor is created with a private specifier, it is not possible for other classes to derive from this class, neither is it possible to create an instance of this class.You can define private constructor in your class when all members inside class are only static and you don't want to create the object of that class.Using private constructor neither you create a object of that class nor it can inherit by other class. If you want to create object of class that contain private constructor then you need to write a public constructor along with
    private constructor.
    Static Constructor 
    There is only one static constructor in the class
    Key Points
    1. A static constructor does not take access modifiers or have parameters
    2. A static constructor is called automatically to initialize the class before the first instance is created or any static member are referenced
    3. A static constructor cannot be called directly

      

    Day 5 - Selection Statement


    Click to zoom-If Statement
    Click to zoom-If else Statement
    Click to zoom-If  elseIf Statement
    Click to zoom-Nested If Statement

    Click to zoom-Switch Statement



    Day-4-Nullable Type and Null Coalescing Operator




    Click to zoom for theory with example


     

    Day-3 (Data Type Conversion)

    Difference Between Parse() and TryParse()  Shown In Image With Explanation
    Click To Zoom Image
     

    Day-2 Data Types



    Click To Zoom Image


    Data-Type conversion-Implicit
    & Explicit Conversion



    Implicit Conversion -Done By The Compiler
    When There is No Loss Of Information and
    No Possibility Of Throwing Exception During 
    The Conversion


    Example-Converting int into Float
    (Small into large Datatype)


    While Converting Float into int data type ,
    We lose the Fractional 
    in this case Explicit Conversion is required


    Click To Zoom Image
    Explicit Conversion is done by 2 Ways
    1-Cast Operator
    2-Convert






    Day-1


    Click To Zoom Image


    Click To Zoom Image
    Click To Zoom Image










    Console.WriteLine();-Used To Print And
    Write in the Console
    Console.ReadLine();-Used to Read the Line
    that is type on Console 

    Main Method Is  Your entry point of Window
    Forms Application
    Main1() Method is call in Main Method