Static Members - Guess the Output

public class ABC
{
    public static string Description
    {
        get { return "Class ABC implemented"; }
    }
    public string GetDescription()
    {
        return Description;
    }
}

public class BCD : ABC
{
    public new static string Description
    {
        get { return "Class BCD implemented"; }
    }
}

class program
{
    static void Main(string[] args)
    {
        Console.WriteLine(new BCD().GetDescription());
        Console.ReadKey();
    }
}


Option 1: Class ABC implemented
Option 2: Class BCD implemented
Option 3: Error

Answer: Option 1: Class ABC implemented

Explanation: Static members are relevant to types and not to instances. 

In this case, return Description; always returns the value of Description property of class ABC type.

Enumeration - Guess the Output

public enum eFormDetailType
{
    eInitialize = 1, eClear, eDelete, eFill
}

public static void Main()
{
    try
    {
        eFormDetailType FormDetailTypeItem = (eFormDetailType) 5;
        Console.WriteLine(FormDetailTypeItem);
    }
    catch
    {
        Console.WriteLine("It's an EXCEPTION");
    }
}

What is the output?

Answer: 5

Explanation: 5 does not have an enumeration to cast against, therefore the output will be 5. If another enum like "eSave" was added in, then the output would be "eSave".

Guess the correct output - OOPS Inheritance (Base Class, Sub-Class)

class A
{
    B objBA = new B();
    public A()
    {
        Console.Write("A");
    }
}

class B
{
    public B()
    {
        Console.Write("B");
    }
}

class C : A
{
    B objB = new B();
    public C()
    {
    Console.Write("C");
    }   
}

class program
{
    static void Main(string [] args)
    {
        new C();
    }
}

Option 1: C
Option 2: BC
Option 3: BCBA
Option 4: BABC
Option 5: BBAC

Answer: Option 5: BBAC

Explanation: Base class constructor will be invoked before derived class constructor.
First constructor of class B will be called, then the execution transfers to class A, where constructor of class B will be called again, then constructor of class A will be called. At Last constructor of class C will be invoked.
So, the output comes to BBAC.

Inheritance (Base Class, Sub-Class) - What is the output?

public class A
{
    public A()
    {
        Console.WriteLine("Class A");
    }
}

public class B : A
{
    public B()
    {
        Console.WriteLine("Class B");
    }
}

public class C : B
{
    public C()
    {
        Console.WriteLine("Class C");            
    }
}

C objC = new C();

Option 1: Class A
Option 2: Class B
Option 3: Class C
Option 4: Class C Class B Class A
Option 5: Class A Class B Class C

Answer: Option 5: Class A Class B Class C


Explanation: Constructors that are declared within a base class, are not inherited by sub-classes. As with any other class, a sub-class with no constructors defined is provided with a default constructor, that provides no explicit functionality.
The constructors from the base class are not made available when creating new sub-class object instances. When a sub-class is instantiated, the standard behavior is for the default constructor of the base class to be called automatically.
In this way the base constructor can initialize the base class before the sub-class' constructor is executed.

What are the different types of literals in C#?

  • Boolean literals: True and False are literals of the Boolean type that map to the true and false state, respectively.
  • Integer literals: Used to write values of types int, uint, long, and ulong.
  • Real literals: Used to write values of types float, double, and decimal.
  • Character literals: Represents a single character and usually consists of a character in quotes, such as 'z'.
  • String literals: C# supports two types of string literals, regular string literal and verbatim string literals. A regular string literal consists of zero or more characters enclosed in double quotes, such as "224568". A verbatim string literal consists of an @ character followed by a double–quote character, such as ©"hello".
  • Null literal: Represents the null–type.

SQL - How can you display monthly information starting from current month to last month in the period?

Recently I was working on a project, in which I need to display cumulative monthly information for all the years in the period starting from the current month to the month for end of the period (if less that 12 months) or all the months (is more than 12 months).

I searched Internet, but I was not able to get any substantial help. So, I finally decided to write it down myself:

--------------------------Variable Declaration Start-----------------------------
DECLARE @in_website_id int, @in_surgeon_id int,  @in_customer_type_id int, @in_selection_criteria_id int
DECLARE @vc_marketing_channel varchar(100), @vc_campaign_id_sf varchar(40)
DECLARE @dt_start varchar(20), @dt_end varchar(20), @dt_start_compare varchar(20), @dt_end_compare varchar(20)
--------------------------Variable Declaration End-------------------------------
SET @in_website_id = 6
SET @in_surgeon_id = 0
SET @in_customer_type_id = 0
SET @in_selection_criteria_id = 0
SET @vc_marketing_channel = 'Website-Affiliate-PY'
SET @vc_campaign_id_sf = ''
SET @dt_start = '11/01/2011'
SET @dt_end = '04/30/2012'
SET @dt_start_compare = '11/01/2011'
SET @dt_end_compare = '04/30/2012'
--------------------------Stored Procedure Start---------------------------------
SELECT in_month_id AS in_month, ISNULL(SUM(dc_estimated_net_roi), 0) AS dc_estimated_net_roi
INTO #temp_current
FROM dbo.cd_process_data
WHERE in_website_id = @in_website_id AND in_surgeon_id = @in_surgeon_id
AND (((@vc_marketing_channel <> 'Email Marketing') AND (vc_marketing_channel = @vc_marketing_channel))
OR ((@vc_marketing_channel = 'Email Marketing') AND (vc_marketing_channel = @vc_marketing_channel AND vc_campaign_id_sf = @vc_campaign_id_sf)))
AND in_customer_type_id = @in_customer_type_id AND in_selection_criteria_id = @in_selection_criteria_id
AND ((in_year_id = YEAR(@dt_start) AND in_month_id >= MONTH(@dt_start))
OR ((in_year_id > YEAR(@dt_start) AND in_year_id < YEAR(@dt_end)) AND (in_month_id >= 1 AND in_month_id <= 12))
OR ((in_year_id > YEAR(@dt_start)) AND (in_year_id = YEAR(@dt_end)) AND (in_month_id >= 1 AND in_month_id <= MONTH(@dt_end)))
OR ((in_year_id = YEAR(@dt_start)) AND (in_year_id = YEAR(@dt_end)) AND (in_month_id >= MONTH(@dt_start) AND in_month_id <= MONTH(@dt_end))))
GROUP BY in_month_id

SELECT in_month_id AS in_month, ISNULL(SUM(dc_estimated_net_roi), 0) AS dc_estimated_net_roi
INTO #temp_previous
FROM dbo.cd_process_data
WHERE in_website_id = @in_website_id AND in_surgeon_id = @in_surgeon_id
AND (((@vc_marketing_channel <> 'Email Marketing') AND (vc_marketing_channel = @vc_marketing_channel))
OR ((@vc_marketing_channel = 'Email Marketing') AND (vc_marketing_channel = @vc_marketing_channel AND vc_campaign_id_sf = @vc_campaign_id_sf)))
AND in_customer_type_id = @in_customer_type_id AND in_selection_criteria_id = @in_selection_criteria_id
AND ((in_year_id = YEAR(@dt_start_compare) AND in_month_id >= MONTH(@dt_start_compare))
OR ((in_year_id > YEAR(@dt_start_compare) AND in_year_id < YEAR(@dt_end_compare)) AND (in_month_id >= 1 AND in_month_id <= 12))
OR ((in_year_id > YEAR(@dt_start_compare)) AND (in_year_id = YEAR(@dt_end_compare)) AND (in_month_id >= 1 AND in_month_id <= MONTH(@dt_end_compare)))
OR ((in_year_id = YEAR(@dt_start_compare)) AND (in_year_id = YEAR(@dt_end_compare)) AND (in_month_id >= MONTH(@dt_start_compare) AND in_month_id <= MONTH(@dt_end_compare))))
GROUP BY in_month_id

CREATE TABLE #temp_main (in_month int, dc_current_estimated_net_roi decimal(12, 2), dc_previous_estimated_net_roi decimal(12, 2))

INSERT INTO #temp_main (in_month)
SELECT in_month FROM #temp_current

INSERT INTO #temp_main (in_month)
SELECT in_month FROM #temp_previous WHERE in_month NOT IN (SELECT in_month FROM #temp_main)

UPDATE a SET a.dc_current_estimated_net_roi = ISNULL((SELECT dc_estimated_net_roi FROM #temp_current WHERE in_month = a.in_month), 0)
FROM #temp_main a

UPDATE a SET a.dc_previous_estimated_net_roi = ISNULL((SELECT dc_estimated_net_roi FROM #temp_previous WHERE in_month = a.in_month), 0)
FROM #temp_main a

SELECT DATENAME(MONTH, in_month * 28) AS vc_month, FLOOR(ROUND(dc_previous_estimated_net_roi, 0)) AS [Last Period],
FLOOR(ROUND(dc_current_estimated_net_roi, 0)) AS [Current Period]
FROM #temp_main
ORDER BY (MONTH(GETDATE()) - in_month) % 12  -- starting from Current Month to the Last Month from Current

DROP TABLE #temp_current
DROP TABLE #temp_previous
DROP TABLE #temp_main
--------------------------Stored Procedure End-----------------------------------

For report from Current Month to the Last Month from Current in the given period in SQL Query, we use ORDER BY ((MONTH(GETDATE()) - in_month) + 12) % 12. This is the key which does this trick.

Ex: Let us assume that the current month is March.

Current Date: 03/04/2013
@dt_start = '08/01/2011'
@dt_end = '04/30/2012'

Output Months Order:

March
February
January
December
November
October
September
August
April

Note: Notice that the month starts from March and decreases gradually.


If we require report from One Month before Current Month to the Last Month from Current, we can use the following text in the SQL Query:

ORDER BY ((MONTH(GETDATE()) - (in_month + 1)) + 12) % 12

Ex: Let us assume that the current month is March.

Current Date: 03/04/2013
@dt_start = '08/01/2011'
@dt_end = '04/30/2012'

Output Months Order:
February
January
December
November
October
September
August
April
March

Note: Notice that the month starts from February and decreases gradually.


If we require report from End Month of the period to Last Month from End Month of the period, we can use the following text in the SQL Query:

ORDER BY ((MONTH(@dt_end) - in_month) + 12) % 12

Ex: Let us assume that the current month is March.

Current Date: 03/04/2013
@dt_start = '08/01/2011'
@dt_end = '04/30/2012'

Output Months Order:
April
March
February
January
December
November
October
September
August

Note: Notice that the month starts from April and decreases gradually.