1. as and is operator
if (o is Employee)
{
Employee e = (Employee) o;
}
In this code, the CLR is actually checking the object's type twice: The is operator first checks to see if o is compatible with the Employee type. If it is, inside the if statement, the CLR again verifies that o refers to an Employee when performing the cast.
C# offers a way to simplify this code and improve its performance by providing an as operator:
Employee e = o as Employee;
if (e != null) {
// Use e within the 'if' statement.
}
2. What should one use string or String ?
Because in C# the string (a keyword) maps exactly to System.String (an FCL type), there is no difference and either can be used.
object and string are also primitive types.
3. Type casting
C# allows implicit casts if the conversion is "safe," that is, no loss of data is possible, such as converting an Int32 to an Int64. But C# requires explicit casts if the conversion is potentially unsafe.
4. Checked and Unchecked Primitive Type Operations
a. CLR offers IL instructions that allow the compiler to choose the desired behavior. The CLR has an instruction called add that adds two values together. The
add instruction performs no overflow checking. The CLR also has an instruction called add.ovf that also adds two values together. However, add.ovf throws a System.OverflowException if an overflow occurs.
b. C# allows the programmer to decide how overflows should be handled. By default, overflow checking is turned off. As a result, the code runs faster—but developers must be assured that overflows won't occur or that their code is designed to anticipate these overflows.
One way to get the C# compiler to control overflows is to use the /checked+ compiler switch. The code executes more slowly because the CLR is checking these operations to determine whether an overflow will occur.
c. There are also checked/unchecked operators/statements.
Byte b = 100;
b = checked((Byte) (b + 200)); // OverflowException is thrown
d. Here's the best way to go about using checked and unchecked:
i. As you write your code, explicitly use checked around blocks where an unwanted overflow might occur due to invalid input data, such as processing a request with data supplied from an end user or a client machine.
ii. As you write your code, explicitly use unchecked around blocks where an overflow is OK, such as calculating a checksum.
iii. For any code that doesn't use checked or unchecked, the assumption is that you do want an exception to occur on overflow.
Now, as you develop your application, turn on the compiler's /checked+ switch for debug builds. Your application will run more slowly because the system will be checking for overflows on any code that you didn't explicitly mark as checked or unchecked. If an exception occurs, you'll easily detect it and be able to fix the bug in your code. For the release build of your application, use the compiler's /checked- switch so that the code runs faster and exceptions won't be generated.
5. Value types and reference types
a. Value type instances are usually allocated on a thread's stack (although they can also be embedded in a reference type object).
b. Reference types are always allocated from the managed heap.
c. Value type are sealed.
d. Value type can implement interfaces.
e. Value types have two representations - boxed and unboxed.
f. Value types can't be assigned null.
g. When you assign a value type variable to another value type variable, a field-by-field copy is made.
h. C# compiler selects LayoutKind.Auto for reference types (classes) and LayoutKind.Sequential for value types (structures). However, if you're creating a value type that has nothing to do with interoperability with unmanaged code, you probably want to override the C# compiler's default.
i. The StructLayoutAttribute also allows you to explicitly indicate the offset of each field by passing LayoutKind.Explicit to its constructor. Then you apply an instance of the System.Runtime.InteropServices.FieldOffsetAttribute. This allows you to create unions in C#.
Saturday, November 1, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment