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

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.
}

static -- C# Keyword

static  -- C# Keyword


  • Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object.
  •  The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.
static class <Class Name>
    {
        public static void <Method Name>() { /*...*/ }
        public static void <Method Name>() { /*...*/  }
    }

sealed -- C# Keyword

sealed  -- C# Keyword

  •  When applied to a class, the sealed modifier prevents other classes from inheriting from it.
  •   In the following example, class B inherits from class A, but no class can inherit from class B.


class A {}  
sealed class B : A {}

readonly -- C# Keyword

readonly -- C# Keyword
  •  The readonly keyword is a modifier that you can use on fields.
  •   When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.
readonly <data Type> <var Name>;