Monday, 25 September 2017

Abstract Class


The abstract modifier can be used with classes, methods, properties, indexers, and events.

Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes.

Abstract classes have the following features:
  • An abstract class cannot be instantiated.
  • An abstract class may contain abstract methods and accessors.
  • It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited.
  • A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain implementation.

Abstract methods have the following features:
  • An abstract method is implicitly a virtual method.
  • Abstract method declarations are only permitted in abstract classes.
  • Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no braces ({ }) following the signature. For example: 
An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.

Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method. For example:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractCLass
{

    class AbstractClassDemo
    {
        public abstract void Animal(string strAnimal)
        {
            Console.WriteLine("Animal Name is :-" +strAnimal);
        }
    }
}
when u compile this u ll get error as
Can not declare a body because it marked as Abstract
Instead u use following code to make a abstract Function.
Second thing is class is not abstract so we have to make it abstract

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractCLass
{

    abstract class AbstractClassDemo
    {
        public abstract void Animal(string strAnimal);
    }
}

it will compile 
Now u Run below Code.

1)  Class 1
namespace AbstractCLass
{

    abstract class AbstractClassDemo
    {
        public abstract void Animal(string strAnimal);
    }

    class Derived_abstract : AbstractClassDemo
    {
        public override void Animal(string strAnimal)
        {
            Console.WriteLine("This animal is :=  "+strAnimal);
        }
    }
}
2)  Class 2
namespace AbstractCLass
    class Class_Main
    {
        static void Main(string[] args)
        {
            Derived_abstract objDerived_abstract = new Derived_abstract();
            objDerived_abstract.Animal("C# .Net");
            Console.ReadLine();
        }
    }
}

This is a simple Demo for abstract Class 
I also Refer Some concepts of Microsoft MSDN
Its helps me Lot to clear My Concepts

Inheritance In C#

When you derive a class from a base class, the derived class will inherit all members of the base class except constructors, though whether the derived class would be able to access those members would depend upon the accessibility of those members in the base class. C# gives us polymorphism through inheritance. Inheritance-based polymorphism allows us to define methods in a base class and override them with derived class implementations. Thus if you have a base class object that might be holding one of several derived class objects, polymorphism when properly used allows you to call a method that will work differently according to the type of derived class the object belongs to.
The Following are the classes


class Animal
{
    public Animal()
    {
        Console.WriteLine("Animal constructor");
    }
    public void Greet()
    {
        Console.WriteLine("Animal says Hello");
    }
    public void Talk()
    {
        Console.WriteLine("Animal talk");
    }
    public virtual void Sing()
    {
        Console.WriteLine("Animal song");
    }
}; 
Now Try this one
 
class Dog : Animal
{
    public Dog()
    {
        Console.WriteLine("Dog constructor");
    }
    public new void Talk()
    {
        Console.WriteLine("Dog talk");
    }
    public override void Sing()
    {
        Console.WriteLine("Dog song");
    }
}; 
Now call above methods
Animal a1 = new Animal();
a1.Talk();
a1.Sing();
a1.Greet();

//Output

Animal constructor
Animal talk
Animal song
Animal says Hello
Okay Now call this one
Animal a2 = new Dog();
a2.Talk();
a2.Sing();
a2.Greet();

//Output

Animal constructor
Dog constructor
Animal talk
Dog song
Animal says Hello

Friday, 22 September 2017

Pass C# class as a parameter of another class. parameterized class or generic class.



C# we can pass parameter to a class and the class which takes parameter is called parameterized class (generic class). 


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

namespace ParameterizedClassDemo
{
    public class Class1
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int age { get; set; }
    }

    public class Class2<T> where T : Class1
    {
        public void PrintData(T ObjT)
        {
            Console.WriteLine("Id is {0}", ObjT.Id);
            Console.WriteLine("Name is {0}", ObjT.Name);
            Console.WriteLine("age is {0}", ObjT.age);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Class1 ObjClass1 = new Class1();
            ObjClass1.Id = 1;
            ObjClass1.Name = "XYZ";
            ObjClass1.age = 25;
            Class2<Class1> ObjClass2 = new Class2<Class1>();
            ObjClass2.PrintData(ObjClass1);
            Console.ReadLine();
        }
    }
}



Saturday, 16 September 2017

Views

Views:

A view is nothing more than a SQL statement that is stored in the database with an associated name. A view is actually a composition of a table in the form of a predefined SQL query.
A view can contain all rows of a table or select rows from a table. A view can be created from one or many tables which depends on the written SQL query to create a view.
Views, which are kind of virtual tables, allow users to do the following:
  • Structure data in a way that users or classes of users find natural or intuitive.
  • Restrict access to the data such that a user can see and (sometimes) modify exactly what they need and no more.
  • Summarize data from various tables which can be used to generate reports.

Creating Views:

Database views are created using the CREATE VIEW statement. Views can be created from a single table, multiple tables, or another view.
To create a view, a user must have the appropriate system privilege according to the specific implementation.
The basic CREATE VIEW syntax is as follows:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE [condition];
You can include multiple tables in your SELECT statement in very similar way as you use them in normal SQL SELECT query.

Example:

Consider the CUSTOMERS table having the following records:
+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+
Now, following is the example to create a view from CUSTOMERS table. This view would be used to have customer name and age from CUSTOMERS table:
SQL > CREATE VIEW CUSTOMERS_VIEW AS
SELECT name, age
FROM  CUSTOMERS;
Now, you can query CUSTOMERS_VIEW in similar way as you query an actual table. Following is the example:
SQL > SELECT * FROM CUSTOMERS_VIEW;
This would produce the following result:
+----------+-----+
| name     | age |
+----------+-----+
| Ramesh   |  32 |
| Khilan   |  25 |
| kaushik  |  23 |
| Chaitali |  25 |
| Hardik   |  27 |
| Komal    |  22 |
| Muffy    |  24 |
+----------+-----+

The WITH CHECK OPTION:

The WITH CHECK OPTION is a CREATE VIEW statement option. The purpose of the WITH CHECK OPTION is to ensure that all UPDATE and INSERTs satisfy the condition(s) in the view definition.
If they do not satisfy the condition(s), the UPDATE or INSERT returns an error.
The following is an example of creating same view CUSTOMERS_VIEW with the WITH CHECK OPTION:
CREATE VIEW CUSTOMERS_VIEW AS
SELECT name, age
FROM  CUSTOMERS
WHERE age IS NOT NULL
WITH CHECK OPTION;
The WITH CHECK OPTION in this case should deny the entry of any NULL values in the view's AGE column, because the view is defined by data that does not have a NULL value in the AGE column.

Updating a View:

A view can be updated under certain conditions:
  • The SELECT clause may not contain the keyword DISTINCT.
  • The SELECT clause may not contain summary functions.
  • The SELECT clause may not contain set functions.
  • The SELECT clause may not contain set operators.
  • The SELECT clause may not contain an ORDER BY clause.
  • The FROM clause may not contain multiple tables.
  • The WHERE clause may not contain subqueries.
  • The query may not contain GROUP BY or HAVING.
  • Calculated columns may not be updated.
  • All NOT NULL columns from the base table must be included in the view in order for the INSERT query to function.
So if a view satisfies all the above-mentioned rules then you can update a view. Following is an example to update the age of Ramesh:
SQL > UPDATE CUSTOMERS_VIEW
      SET AGE = 35
      WHERE name='Ramesh';
This would ultimately update the base table CUSTOMERS and same would reflect in the view itself. Now, try to query base table, and SELECT statement would produce the following result:
+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  35 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

Inserting Rows into a View:

Rows of data can be inserted into a view. The same rules that apply to the UPDATE command also apply to the INSERT command.
Here we can not insert rows in CUSTOMERS_VIEW because we have not included all the NOT NULL columns in this view, otherwise you can insert rows in a view in similar way as you insert them in a table.

Deleting Rows into a View:

Rows of data can be deleted from a view. The same rules that apply to the UPDATE and INSERT commands apply to the DELETE command.
Following is an example to delete a record having AGE= 22.
SQL > DELETE FROM CUSTOMERS_VIEW
      WHERE age = 22;
This would ultimately delete a row from the base table CUSTOMERS and same would reflect in the view itself. Now, try to query base table, and SELECT statement would produce the following result:
+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  35 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

Dropping Views:

Obviously, where you have a view, you need a way to drop the view if it is no longer needed. The syntax is very simple as given below:
DROP VIEW view_name;
Following is an example to drop CUSTOMERS_VIEW from CUSTOMERS table:
DROP VIEW CUSTOMERS_VIEW;
Important Note: 
The complete blog is taken from http://www.tutorialspoint.com/sql/sql-using-views.htm

Wednesday, 6 September 2017

Abstract Class and Virtual Function


Difference Between Abstract and Virtual Method in Abstract Class
·         One must declare  abstract method,properties,indexers,events in abstract class
·         We can Declare virtual method in abstract class
·         If u have abstract method in base class then u have to(mandatory) implement this abstract method in derived class
·         In case if u have virtual method in abstract class then it’s not mandatory to implement it in derived class. This main difference between virtual and abstract
·         By default all the methods in abstract class  are virtual
·         If we have virtual method in base class which is abstract class then we override this abstract method in derived class so when we call this method by creating the object of derived class in this case derived class override method will executed and if we want to execute base class virtual method then we use base Keyword. See example
           abstract class AbstractClassDemo
             {
              
          public virtual void Family()
          {
                  Console.WriteLine("Method Family in Base Class ");
                }
             }
          
           class Derived_abstract : AbstractClassDemo
            {
                 public override void Family()
                 {
                    base.Family();
                    Console.WriteLine("Method Family in Derived Class");
                 }
            }

        In Main Just Call this as
        static void Main(string[] args)
        {          
            Derived_abstract objDerived_abstract = new Derived_abstract();
objDerived_abstract.Family();
            Console.ReadLine();
        }
Just because of base.Family(); virtual method in base class executed

Output as
Method Family in Base Class
Method Family in Derived Class

·         Error an abstract class cannot be sealed or static
sealed abstract class AbstractClassDemo
 {
    public abstract void Animal(string strAnimal);
    public virtual void Family()
     {
       Console.WriteLine("Method Family in Base Class ");
     }
}
Complete Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstractCLass
{
    abstract class AbstractClassDemo
    {
        public abstract void Animal(string strAnimal);

        public virtual void Family()
        {
            Console.WriteLine("Method Family in Base Class ");
        }
    }
    class Derived_abstract : AbstractClassDemo
    {
        public override void Family()
        {
            base.Family();
            Console.WriteLine("Method Family in Derived Class");
        }

        public override void Animal(string strAnimal)
        {
            Console.WriteLine("This animal is :=  "+strAnimal);
        }
    }
}