How is a C# program compiled to a processor specific native code?

When C# programs are compiled, they are physically packaged into assemblies. Assemblies are self-describing unit of functionality containing both code and metadata. Assemblies typically have the file extension .exe or .dll, depending on whether they implement applications or libraries.

Assemblies contain executable code in the form of Intermediate Language (IL) instructions, and symbolic information in the form of metadata. Before it is executed, the IL code in an assembly is automatically converted to processor-specific code by the Just-In-Time (JIT) compiler of .NET Common Language Runtime.

BETWEEN logical operator (SQL) is Inclusive

SQL logical operator BETWEEN is inclusive. It uses a closed interval ([a,b]).

Syntax:

 <#test_expression#> [ NOT ] BETWEEN <#begin_expression#> AND <#end_expression#>

Result Type: Boolean

BETWEEN returns TRUE if the value of the <#test_expression#> is greater than or equal to the value of <#begin_expression#> and less than or equal to the value of <#end_expression#>.

NOT BETWEEN returns TRUE if the value of <#test_expression#> is less than the value of <#begin_expression#> or greater than the value of <#end_expression#>.

Note: You can use the greater than (>) and less than operators (<) to specify an exclusive range. If any input to the BETWEEN or NOT BETWEEN predicate is NULL, the result is UNKNOWN.

Example: The example below retrieves a list of Opportunities which has been implemented last 60 days:

SELECT a.Id, a.Name, a.StageName
FROM dbo.sf_opportunity a
WHERE a.Consultation_Date_Time_test__c BETWEEN DATEADD(DAY, -60, GETDATE()) AND GETDATE() AND a.IsDeleted = 0

operator keyword (C# Conversion Keywords)

The operator keyword is used to overload a built-in operator or to provide a user-defined conversion in a class or struct declaration.

Example: The following example overloads the + and * operators to perform fractional addition and multiplication, and also provides a conversion operator that converts a Fraction type to a double type.

class Fraction
{
int iNum, iDen;
public Fraction(int iNum, int iDen)
{
this.iNum = iNum;
this.iDen = iDen;
}

// overload operator +
public static Fraction operator +(Fraction x, Fraction y)
{
return new Fraction(x.iNum * y.iDen + y.iNum * x.iDen, x.iDen * y.iDen);
}

// overload operator *
public static Fraction operator *(Fraction x, Fraction y)
{
return new Fraction(x.iNum * y.iNum, x.iDen * y.iDen);
}

// user-defined conversion from Fraction to double
public static implicit operator double(Fraction oFraction)
{
return (double)oFraction.iNum / oFraction.iDen;
}
}

class Test
{
static void Main()
{
Fraction x = new Fraction(1, 2);
Fraction y = new Fraction(3, 7);
Fraction z = new Fraction(2, 3);
Console.WriteLine((double)(x * y + z));
}
}

/*Output: 0.880952380952381*/

explicit keyword (C# Conversion Keywords)

The explicit keyword declares a user-defined type conversion operator that must be invoked with a cast. The conversion operator converts from a source type to a target type. The source type provides the conversion operator. Unlike implicit conversion, explicit conversion operators must be invoked by means of a cast.

Omitting the cast results in compile-time error CS0266.

Example: The following example provides a Fahrenheit and a Celsius class, each of which provides an explicit conversion operator to the other class.

class Celsius
{
public Celsius(float fTemp)
{
fDegrees = fTemp;
}
// Must be defined inside a class called Celsius:
public static explicit operator Fahrenheit(Celsius c)
{
return new Fahrenheit((9.0f / 5.0f) * c.fDegrees + 32);
}
public float Degrees
{
get { return fDegrees; }
}
private float fDegrees;
}

class Fahrenheit
{
public Fahrenheit(float fTemp)
{
fDegrees = fTemp;
}
// Must be defined inside a class called Fahrenheit:
public static explicit operator Celsius(Fahrenheit fahr)
{
return new Celsius((5.0f / 9.0f) * (fahr.fDegrees - 32));
}
public float Degrees
{
get { return fDegrees; }
}
private float fDegrees;
}

class MainClass
{
static void Main()
{
Fahrenheit oFahrenheit = new Fahrenheit(100.0f);
Console.Write("{0} Fahrenheit", oFahrenheit.Degrees);
Celsius oCelsius = (Celsius)oFahrenheit; // Calls explicit conversion operator in Fahrenheit class

Console.Write(" = {0} Celsius", oCelsius.Degrees);
Fahrenheit oFahrenheit2 = (Fahrenheit)oCelsius; // Calls explicit conversion operator in Celsius class
Console.WriteLine(" = {0} Fahrenheit", oFahrenheit2.Degrees);
}
}

implicit keyword (C# Conversion Keywords)

An implicit keyword is used to declare an implicit user-defined type conversion operator. It is used to enable implicit (automatic) conversions between a user-defined type and another type, if the conversion is guaranteed not to cause a loss of data.

Implicit conversions can improve source code readability by eliminating unnecessary casts. Care must be taken to prevent unexpected results because implicit conversions do not require programmers to explicitly cast from one type to the other. In general, implicit conversion operators should never throw exceptions and never lose information so that they can be used safely without the programmer's awareness. If a conversion operator cannot meet those criteria, it should be marked explicit.

Example:

class Digit
{
public Digit(double dValue) { dVal = dValue; }
public double dVal;

// User-defined conversion from Digit to double
public static implicit operator double(Digit oDigit)
{
return oDigit.dVal;
}

// User-defined conversion from double to Digit
public static implicit operator Digit(double dDoubleVal)
{
return new Digit(dDoubleVal);
}
}

class Program
{
static void Main(string[] args)
{
Digit oDigit = new Digit(7);

double dNum = oDigit; //This call invokes the implicit "double" operator

Digit oDigit2 = 12; //This call invokes the implicit "Digit" operator

Console.WriteLine("dNum = {0} oDigit2 = {1}", dNum, oDigit2.dVal);
Console.ReadLine();
}
}

Recursive Common Table Expressions

Recursion (in SQL) is a process in which a query executes itself repeatedly for a given number of time to obtain a result set.

Recursive Query: When a query references a recursive CTE, it is referred to as Recursive Query. A common use of recursive queries is to return hierarchical data.

Recursive Common Table Expression: When an initial CTE is repeatedly executed to return subsets of data until the complete result set is obtained, it becomes a recursive Common Table Expression.

The code required to run a recursive query within a SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement can be greatly simplified by the use of recursive CTE.

Structure of a Recursive CTE

The structure of a recursive CTE is similar to recursive routines in other programming languages. A recursive CTE can return multiple rows whereas a recursive routine in other languages returns a scalar value.

A recursive CTE consists of three elements:
  • Invocation – The invocation of the recursive CTE consists of one or more CTE_query_definitions joined by UNION ALL, UNION, EXCEPT, or INTERSECT operators.
    These query definitions are referred to as anchor members because they form the base result set of the CTE structure.
    CTE_query_definitions are considered anchor members unless they reference the CTE itself. All anchor-member query definitions must be positioned before the first recursive member definition, and a UNION ALL operator must be used to join the last anchor member with the first recursive member.
  • Recursive Invocation – Recursive invocation includes one or more CTE_query_definitions joined by UNION ALL operators that reference the CTE itself. These query definitions are referred to as recursive members.
  • Termination check – The termination check is not explicit, it is implicit. When no rows are returned from the previous invocation, recursion stops.