C#.net Fundamentals

C#.net Fundamentals1. If you really want to use an identifier that clashes with a keyword, you can do so by qualifying it with the @ prefix. For instance:class class  {…}      // illegal

class @class {…}      // legal

The @ symbol doesn’t form part of the identifier itself. So @myVariable is the same as myVariable.

2. By convention, arguments, local variables, and private fields should be in camel case (e.g., myVariable ), and all other identifiers should be in Pascal case (e.g., MyMethod ).

3. Implicit conversions are allowed when both of the following are true:

The compiler can guarantee they will always succeed.

No information is lost in conversion.

Conversely, explicit conversions are required when one of the following is true:

The compiler cannot guarantee they will always succeed.

Information may be lost during conversion

4. Value types comprise most built-in types (specifically, all numeric types, the char type, and the bool type) as well as custom struct and enum types.The assignment of a value type instance always copies the instance.A value type cannot have a null value

Reference types comprise all class, array, delegate, and interface types.

Assigning a reference type variable copies the reference, not the object instance.A reference can be assigned the literal null, indicating that the reference points to no object:

5.Reference types require separate allocations of memory for the reference and object. The object consumes as many bytes as its fields, plus additional administrative overhead. The precise overhead is intrinsically private to the implementation of the .NET runtime, but at minimum the overhead is 12 bytes, used to store a key to the object’s type, as well as temporary information such as its lock state for multi threading and a flag to indicate whether it has been fixed from movement by the garbage collector. Each reference to an object requires an extra four or eight bytes, depending on whether the .NET runtime is running on a 32- or 64-bit platform.

6. Of the integral types, int and long are first-class citizens and are favored by both C# and the run time. The other integral types are typically used for interoperability or when space efficiency is paramount.

Of the real number types, float and double are called floating-point types and are typically used for scientific calculations. The decimal type is typically used for financial calculations, where base-10-accurate arithmetic and high precision are required.

7. The F and M suffixes are the most useful and should always be applied when specifying float or decimal literals. Without the F suffix, the following line would not compile, because 4.5 would be inferred to be of type double, which has no implicit conversion to float:

float f = 4.5F;

The same principle is true for a decimal literal:

decimal x = -1.23M;     // Will not compile without the M suffix.

8. At runtime, arithmetic operations on integral types can overflow. By default, this happens silently—no exception is thrown. While the C# specification is agnostic as to the result of an overflow, the CLR always causes wraparound behavior. For example, decrementing the minimum possible int value results in the maximum possible int value:

int a = int.MinValue;

a–;

Console.WriteLine (a == int.MaxValue); // True

9. You can make arithmetic overflow checking the default for all expressions in a program by compiling with the /checked+ command-line switch (in Visual Studio, go to Advanced Build Settings). If you then need to disable overflow checking just for specific expressions or statements, you can do so with the unchecked operator. For example, the following code will not throw exceptions—even if compiled with /checked+:

10. For reference types, equality, by default, is based on reference, as opposed to the actual value of the underlying object:

 

Leave a Reply

Your email address will not be published. Required fields are marked *