h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

Explicit interface member implementations

Explicit interface member implementations
             An explicit interface member implementation is a method, property, event, or indexer declaration that references a fully qualified interface member name.

Example for Explicit interface member implementations
interface IControl
{
   void Paint();
}
interface ITextBox: IControl
{
   void SetText(string text);
}
class TextBox: ITextBox
{
   void IControl.Paint() {...}
   void ITextBox.SetText(string text) {...}
}

Zoom the View in Visual Studio

Zoom the View in Visual Studio

Keyboard Shortcuts

  • To make the font larger, press CTRL+SHIFT+PERIOD
  • To make the font smaller, press CTRL+SHIFT+COMMA


Dictionary initializer in C# 6.0

Dictionary initialize in C# 6.0
            Adds a new ability to use the Key / indexer to map with the Values directly during the initialization itself.

Dictionary<int, string> students = new Dictionary<int, string> {
[1] = "Student 1" ,
[2] = "Student 2",
[3] = "Student 3",
[4] = "Student 4",
};

Dictionary initializer in C#

Dictionary initialize in C# 

  You can easily initialize a Dictionary Object using a Collection Initialize, which is there since C# 3.0.
  Collection initialize adds one of the  parameter as Key and another as  Value corresponding to the assigned Key.

Dictionary<int, string> students = new Dictionary<int, string> {
{1, "Student 1" },
{2, "Student 2" },
{3, "Student 3" },
{4, "Student 4" }
};
Find Public Key Token  for dll in Visual Studio

Use .Net Framework Tool sn.exe
Open the Visual Studio 2008 Command Prompt and then point to the dll’s folder you want to
get the public key,

Use the following command,
sn –T myDLL.dll
This will give you the public key token. Remember one thing this only works if the assembly has to be strongly signed.

New Features of C# 6.0

New Features of C# 6.0

  • Initialization of Dictionary – Dictionary initializer in C# 6.0
  • Null – Conditional Operators in C# 6.0
  • Using nameof Operator in C# 6.0
  • Conditional Exception Handling – Exception Filters in C# 6.0
  • Using await in a catch or finally block – in C# 6.0
  • Expression – Bodied Methods in C# 6.0
  • Simplify Static Member Access – Using Statement With Static Classes in C# 6.0
  • Easily format strings – String interpolation in C# 6.0
  • Initialize Auto-Property in C# 6.0
  • Getter Only Auto-Property in C# 6.0


Tips to improve performance of C# code

Tips to improve performance of C# code

  • Choose your data type before using it
  • Use For loop instead of foreach
  • Choose when to use a class and when to use a structure
  • Always use Stringbuilder for String concatenation operations
  • Choose best way to assign class data member
  • Boxing and UnBoxing
  • ‘as’ versus type casting
  • Control overflow checking
  •  Using readonly versus const
  • Avoid producing a lot of garbage
  • Avoid using finalizers when possible
  • Recycle and/ or cache objects
  • Reduce class hierarchy
  • Avoid explicitly calling the Garbage Collector
  • Avoid synchronization
  • Use Lazy Evaluation
  •  Optimized classes
  •  Explicitly close resources
  • Limit number of threads
  • Ternary Operator (?:)
  •  Null-Coalesce Operator (??)
  • Namespace Alias Qualifier
  • Object Initializers
  • Nullable Types
  • Type Inference
  • Lambda Expressions



Volatile -- C# Keyword

volatile -- C# Keyword


  • The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access.
  • The volatile keyword can be applied to fields of these types:
  • Reference types.
  • Pointer types (in an unsafe context). Note that although the pointer itself can be volatile, the object that it points to cannot. In other words, you cannot declare a "pointer to volatile."
  • Types such as sbyte, byte, short, ushort, int, uint, char, float, and bool.
  • An enum type with one of the following base types: byte, sbyte, short, ushort, int, or uint.
  • Generic type parameters known to be reference types.
  • IntPtr and UIntPtr.

public volatile <data type> <Var Name>;

virtual -- C# Keyword

virtual -- C# Keyword
         The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class.

 public virtual <Data Type> <Method Name>()
{
///Code
  return <Value>
}

unsafe -- C# Keyword

unsafe -- C# Keyword
           The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.

Without  Parameter
unsafe
{
    // Unsafe context: can use pointers here.
}
With Parameter(s)
unsafe static void <Method Name>(<data Type> <var name>)
{
    // Unsafe context: can use pointers here.
}