What is Virtual Function :

A virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature.

Compile-time type and runtime type. With virtual methods, the runtime type is always used to determine the best method implementation. In the main entry point, ref1 has a compile-time and runtime type of A. On the other hand, ref2 has a compile-time type of A but a runtime type of B! With virtual methods, the runtime type is used in both invocations.

Example :

using System;
class A
{
   public virtual void Test()
    {
          Console.WriteLine("A.Test");
    }
}

class B : A
{
    public override void Test()
    {
          Console.WriteLine("B.Test");
    }
}

class Program
{
    static void Main()
    {
          // Compile-time type is A.
          // Runtime type is A as well.
          A ref1 = new A();
          ref1.Test();

          // Compile-time type is A.
          // Runtime type is B.
          A ref2 = new B();
          ref2.Test();
    }
}

Output
A.Test
B.Test

Restriction :

- We cannot use override keyword when it is not marked as virtual or abstract in base class.

Advantage :              
- our project may be designed in such a way that you do not know all the types of objects that will occur when it is executed. You can provide a standard (base) type and design around that type. Then, you can               re-implement important functionality depending on the more specific (derived) types. So when you call a method on the base type, you will invoke the more derived and useful method
- Whenever, there is a virtual function in the class, a v-table is constructed in the memory. The v-table has a list of addresses to the virtual functions of the class and pointers to the functions from each of the objects of the derived class. When a virtual function call is made, the v-table is used to get the addresses of the function and the function is called.


Practical Review

using System;

namespace TestConcepts
{
    public partial class C_Examples_Part1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VirtualBase obj = new VirtualBase();
            string bas = obj.Virtual();

            VirtualDerived objDer = new VirtualDerived();
            string der = objDer.Virtual();
           
            //Response.Write("Simple Virtual: " + bas + " - " + der);

            VirtualSecond objSec = new VirtualSecond();
            string sec = objSec.Virtual();

            VirtualSecondDerived objSecondDer = new VirtualSecondDerived();
            string objsecvir = objSecondDer.Virtual();

            //Response.Write("Simple Virtual: " + sec + " - " + objsecvir);



            //alternations

            VirtualBase objFirst = objSecondDer;
            string basFistAlternate = objFirst.Virtual();
            Response.Write("<br>Base First + Second Derived: " + basFistAlternate);


            VirtualBase VirtualBaseSecondBase = objSec;
            string sVirtualBaseSecondBase = VirtualBaseSecondBase.Virtual();
            Response.Write("<br>Base First + Secong Base: " + sVirtualBaseSecondBase);

            VirtualBase VirtualBaseFirstDer = objDer;
            string sVirtualBaseFirstDer = VirtualBaseFirstDer.Virtual();
            Response.Write("<br>Base First + First Derived: " + sVirtualBaseFirstDer);

           
            //VirtualDerived VirtualDerivedBaseFirst = obj;// Not able to assign - cannot allow implicit conversion
            VirtualDerived VirtualDerivedBaseSecond = objSec;
            string sVirtualDerivedBaseSecond = VirtualDerivedBaseSecond.Virtual();
            Response.Write("<br>First Derived + Second Base: " + sVirtualDerivedBaseSecond);

            VirtualDerived VirtualDerivedBaseSecondDer = objSecondDer;
            string sVirtualDerivedBaseSecondDer = VirtualDerivedBaseSecondDer.Virtual();
            Response.Write("<br>First Derived + Second Derived: " + sVirtualDerivedBaseSecondDer);
           // VirtualDerived VirtualDerivedBaseSecondDer = objSecondDer;
           // string sVirtualDerivedBaseSecondDer = VirtualDerivedBaseSecondDer.Virtual();
          //  Response.Write("<br>First Derived + Second Derived: " + sVirtualDerivedBaseSecondDer);
           



        }
    }

    public class VirtualBase
    {
        public virtual string Virtual()
        {
            return "Virtual Base";
        }
    }

    /*Base Class must contain Virtual Keyword when use same function in derived.
     * otherwise implicitly it will take as 'new' */

    public class VirtualDerived : VirtualBase
    {
       
        public override string Virtual()
        {
           return "Virtual Derived";
        }
        public string GetBaseVirutual
        {
            get { return base.Virtual(); }
        }
    }
    public class VirtualSecond : VirtualDerived
    {

       
        new public virtual string Virtual()
        {
            return "Virtul Second";
        }
    }

    public class VirtualSecondDerived : VirtualSecond
    {
        public override string Virtual()
        {
            return "Virtul Second Derived";
        }
    }
}

Static Class

- Static classes can not be instantiated.
- Static classes are sealed so they can not be inherited.
- Only static members are allowed.
- Static classes can only have static constructor to initialize static members. They cannot contain Instance Constructors or simply constructors as we know that they are associated with objects and operates on data when an object is created.
- Static Class members are Called Like “ClassName.MemberName”
- In normal class, we can create object,  So the compiler cannot know the class member address directly at compile time; runtime only the get a address based on objects and isolate the method/member by using “this” keyword ( you won’t give like this.FunctionName(), it will take automatically at the creation of Object ) at runtime. But the Static Class defined at compile time, so it will be faster.

Example

//App_Code Page

static class PrinterCall
{
      /// <summary>
     /// Copy the items from one dropdown list to another dropdown list
    /// </summary>
    /// <param name="ddlSource">Source DropDown list</param>
    /// <param name="ddlDestination">Destination DropDown list</param>
    public static void CopyDropDownList(DropDownList ddlSource, DropDownList ddlDestination)
    {
        ListItem[] myListItemArray = new ListItem[ddlSource.Items.Count];
        ddlSource.Items.CopyTo(myListItemArray, 0);
  ddlDestination.Items.AddRange(myListItemArray);
}
      }

//Aspx or any other Codebehind
protected void Page_Load(object sender, EventArgs e)
{
CommonBehavior.CopyDropDownList(ddlSource, ddlDestination);
}

Can Struct be inherited?

No, Structure can't be inherited as this is implicitly sealed.

What is Virtual keyword?

This keyword indicates that a member can be overridden in a child class. It can be applied to methods, properties, indexes and events.

What is new Modifier?

The new modifiers hides a member of the base class. C# supports only hide by signature.

Default Modifiers :

An enum has default modifier as public

A class has default modifiers as Internal . It can declare members (methods etc) with following access modifiers:
public
internal
private
protected internal

An interface has default modifier as public

A struct has default modifier as Internal and it can declare its members (methods etc) with following access modifiers:
public
internal
private

A methods, fields, and properties has default access modifier as "Private" if no modifier is specified.


Abstraction  -> Hiding the unwanted informations and giving the relevant information.

Early binding means that our code directly interacts with the object, by directly calling its methods. Since the compiler knows the object's data type ahead of time, it can directly compile code to invoke the methods on the object.
    Ex : string s;
Late binding means that our code interacts with an object dynamically at run-time. This provides a great deal of flexibility since our code literally doesn't care what type of object it is interacting with as long as the object supports the methods we want to call.
    Ex : a = new Object();




First we need to Enable DB Mail Option using query, admin only can do this operation,

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Database Mail XPs', 1;
GO
RECONFIGURE
GO

Example of Manual Config :



Som example for sending DB Mail is in the below link :


Sample 1 :

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE usp_SendMail
AS
BEGIN
      SET NOCOUNT ON;
      DECLARE @sub NVARCHAR(4000)
      DECLARE @body1 NVARCHAR(max)
      DECLARE @ERRMSG VARCHAR(max)
      SET @sub = 'Test Mail -- ' + CONVERT(VARCHAR,GETDATE())

      SET @body1 = 'Hi Team,'


BEGIN TRY

      EXEC msdb.dbo.sp_send_dbmail @profile_name='DBA',      -- Need to create a Profile using Management - > DB Mail - > Config DB Mail
      @recipients='nsuresh@hovservices.in',
           
      @copy_recipients='vijayans@hovservices.in',
      @query='SET NOCOUNT ON;
                  SELECT ''<table border=2 bgcolor=gray font-family=Verdana font-size=medium>
                  <tr>
                  <td><B>Test Mail</B></td></tr>''
                  SELECT ''/<table>''',
                  @query_result_no_padding=1

END TRY

BEGIN CATCH

      SET @ERRMSG = ERROR_MESSAGE()

      SET @ERRMSG=REPLACE(@ERRMSG,'''','')

      SET @sub = 'Log -- ERROR' + CONVERT(VARCHAR,GETDATE())

      EXEC msdb.dbo.sp_send_dbmail @profile_name='DBA',@recipients='nsuresh@hovservices.in',

            @copy_recipients='vijayans@hovservices.in',
            @subject=@sub,@body=@ERRMSG,@body_format='HTML',
            @query_result_no_padding=1

END CATCH

END





Sample 2 :
-- Create a Account
EXECUTE msdb.dbo.sysmail_add_account_sp
    @account_name = 'PFP Account',
    @description = 'Mail account for used by PFP Users',
    @email_address = 'nsuresh@hovservices.in',
    @display_name = 'PFP DEV Mail',
    @replyto_address = 'nsuresh@hovservices.in',
    @mailserver_name = '10.16.5.39';


-- Create a Database Mail profile
EXECUTE msdb.dbo.sysmail_add_profile_sp
    @profile_name = 'PFP Profile',
    @description = 'Mail Profile for use by PFP Process';
   
   
    -- Add the account to the profile
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
    @profile_name = 'PFP Profile',
    @account_name = 'PFP Account',
    @sequence_number =1 ;



EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'PFP Profile',
    @recipients = 'vijayans@hovservices.in',
    @body = 'Profile has been created',
    @subject = 'PFP' ;

Getting Date from DaysOfYear

declare @year int
declare @dayofyear int

set @year = 2011
set @dayofyear = 229

select dateadd(day, @dayofyear-1, dateadd(year, @year-1900, 0))

Getting DayOfYear from Date

select datepart(dayofyear, '20060701')

Get Month Name from Month Number

DECLARE @Mth smallint
SET @Mth = 11
SELECT DateName(mm,DATEADD(mm,@Mth,-1)) as [MonthName]