Monday 18 February 2013

Inheritance in CSharp

Children inheriting the property of there parents is known as inheritance which gives reusability we can implement the same rule in our object oriented languages and established parent child relationship between classes so that parent class members can be consumed by child classes i.e child classes are reusing the parent class members without rewriting them again.

A parent class can have any number of child classes and members of parent class can be consumed under all the child classes.

Private members of a class cannot be inherited and acquired by the child classes.


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

namespace CSharpConsole
{
    class Class1
    {
        public Class1()
        {
            Console.WriteLine("Class1 constructor called");
        }
        public void test1()
        {
            Console.WriteLine("First Method");
        }
        public void test2()
        {
            Console.WriteLine("Second Method");
        }

    }
}// Now add another class Class2.cs and write the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class Class2:Class1
    {
        public Class2()
        {
            Console.WriteLine("Class2 constructor called");
        }
        public void Test3()
        {
            Console.WriteLine("Third method called");
        }

        static void Main()
        {
            Class2 c = new Class2();
            c.test1(); c.test2(); c.Test3();
            Console.ReadLine();
         }
    }
}


Each day learning something New and Interesting

Today, we spent more time upon writing SOL queries. Created Sub queries under that I did :
  • Single Row Sub Queries.
  • Multi-Row Sub Querries.
  • Nested Sub querries And
  • Co-related sub queries. I also created Indexes . Indexes is a relational database are mainly use to make the data retrival faster similiar to index available at the end of the book. There are two types of Indexes:
  • Clustered Index : Cluster index is the index that will arrange the rows physically in the memory in sorted order.
  • Non-Clustered Index : Cluster index is the index that will not arrange the rows physically in the memory in sorted order.
So I created the Index by name DnoIdx on Emp table that I created before and tried both clustered/non clustered creation of Indexes.
In the secod half I tried to analyze and learn C# basics like:
  • Static Methods vs Non Static Methods
  • Static Constructors vs Non Static Constructors
  • Static Classes
It has become the everyday Routine to learn Something new related to SQL Server and C#.Net and implement it practically.Looking forward for more such new topics everyday, so that I can at the end, able to know almost everything about the .Net language concepts(C# and ASP),AJAX and SQL Server far better.

 

Static Constructor Vs Non-Static Constructor

  • A Constructor that is defined using static keyword is a static constructor. Rest of the others are Non-Static only. 
  • Static Constructor is the first block of code which executes under a class where as Non-Static Constructor gets executed only after creating the object of class as well as each and every time the object of class is created.
  • In the life cycle of a class a Static Constructor gets called one and only one time whereas a Non-Static Constructor gets called either for zero(no objects are created) or n times (n objects are created).
  • A Static Constructor can never be parametrized because it is the first block of code that executes in a class and more over it is implicitly called and Non-Static constructors can be parametrized because they are explicitly called and we have a chance of sending values to the parameters.

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

namespace ConsoleApps
{
    class StatCon
    {
        static StatCon()
        {
            Console.WriteLine("Static Constructor Called");
        }
        public StatCon()
        {
            Console.WriteLine("Non-Static Constructor Called");
        }
        static void Main()
        {
            Console.WriteLine("Main Method Called");
            StatCon s1 = new StatCon();
            StatCon s2 = new StatCon();
            StatCon s3 = new StatCon();
            Console.ReadLine();
        }
    }
}