Performance Thumb Rules
- Cache variables.
- Move max code out of loops.
- Factoring control statements.
- Use switch statements.
Functions
- Signature: Pass by reference or by value and data type, function name.
- Abstract methods in a class contain no method body, and are implicitly virtual.
- Virtual methods allow subclasses to provide their own implementation of that method using the override keyword.
Threading
- System.Threading.
Thread t = new Thread(fn); //Here in actuality we pass delegate
- A threads IsAlive property returns true, until the point where the thread ends.
- A separate copy of the local variable is created on each thread’s memory stack.
- Threads share data if they have a common reference to the same object instance.
- Static fields offer another way to share data between threads.
- Thread safety where output is deterministic.
- The remedy is to obtain an exclusive lock while reading and writing to the common field.
- When two threads simultaneously contend a lock (in this case, locker), one thread waits, or blocks, until the lock becomes available.
- So critical section is entered only one at a time.
- While waiting on a Sleep (the current thread waits for specific time) or Join (other threads wait), a thread is blocked and so does not consume CPU resources.
- Join returns true if the thread ended or false if it timed out.
- Sleep(0) relinquishes the threads current time slice immediately to CPU and Yield function to processes on same processor.
Parameter Passing
- Pass by value.
- Pass by reference (ref).
- Pass as out (must be assigned before coming out of function).
- Pass as param (any number of similar parameters).
Memory Allocation and Performance
- Separate allocations of memory for the reference and object.
- The object consumes as many bytes as its fields, plus additional administrative overhead (typically 12 bytes).
- Each reference to an object requires an extra 4 or 8 bytes, depending on whether the .NET runtime is running on a 32- or 64-bit platform.
- The 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.
- For reference types, equality, by default, is based on reference (Exception: String), as opposed to the actual value of the underlying object as in case of value types.
- All array indexing is bounds-checked by the runtime.
- Stack for local variables and parameters. An example is a reference to an object which is local.
- Heap for objects.
- If the instance was declared as a field within an object, or as an array element, that instance lives on the heap.
- An object is eligible for deallocation as soon as nothing references it
- The heap is also used to store static fields and constants and will live on until the application domain is torn down.
- An unreferenced object is eventually collected by the garbage collector. It can’t be explicitly be removed.
- Whenever an unboxing or downcast occurs, the runtime checks the type dynamically.
- Generic collections are better than collections if and only if it stores value type and has large entries. For reference type they are same. Generally former is better. Example List<T> is better than Array List for value entries > 500.
- Presently, .NET does not provide a mechanism for constraining a generic type parameter to be serializable.
Namespace
- global::
- using PropertyInfo2 = System.Reflection.PropertyInfo;
- System.Numerics, System.Boolean, System.Convert, System.String.
- System.Collection => For higher-level data structures, such as dynamically sized arrays and dictionaries.
- System.Object => The ultimate base class for all types.
- System.Collections.Generic => The standard .NET interface for mutable collections.
- System.Delegate => For delegates.
- System.Exception.
- System.Attribute => For attributes.
- System.Web.Services => For SOAP.
Reflection
- Enables us to get some information about object in runtime.
- GetField(varName, BindingFlags.XXX | BindingFlags.YYY etc).
- SetValue(null, newInt), GetValue(null).
- Typeof, System.Type, IsAbstract, IsClass, GetConstructors(), GetMethods().
Arrays
- Automatically initialised with 0.
- Arrays can be rectangular [,] and jagged [] [].
- Implicit typing in arrays can let you omit the type qualifier after the new keyword.
Directives
- /target:assembly.
- /define.
- /warnasdoc.
- /doc.
General
- CLR is the compiler and csc *.cs *.exe csc /target:library *.cs *.dll.
- Immutable means non- editable.
- Value types such as int can also be cast to and from object. This in C# is called unification.