Monday, December 22, 2008

Strong names and Signing

What is a strong name?

A strong name is a .NET assembly name combined with its version number and other information to uniquely identify the assembly. This allows multiple versions of the same assembly to peacefully co-exist in the global assembly cache, where shared assemblies are typically stored.

A strong name consists of five parts:

  1. Simple Name - Usually the name of the file (without the extension) that contains the assembly
  2. Public Key - RSA cryptographic public key that helps verify the assembly’s authenticity
  3. Version - Four-part version number, in the form of Major.Minor.Build.Revision
  4. Culture - Target audience for the assembly, such as “neutral” (default audience), “en-us” (English - United States) or “fr” (France) etc.
  5. Processor Architecture - Defines the assembly’s format, such as MSIL (intermediate language) or x86 (binary for Intel x86 processors)

An example strong name is “Mini-Launcher, Version=0.3.612.24542, Culture=neutral, PublicKeyToken=ffa52ed9739048b4, ProcessorArchitecture=MSIL”.

Why use strong names?

Strong names are required to store shared assemblies in the global assembly cache (GAC). This is because the GAC allows multiple versions of the same assembly to reside on your system simultaneously, so that each application can find and use its own version of your assembly. This helps avoid DLL Hell, where applications that may be compiled to different versions of your assembly could potentially break because they are all forced to use the same version of your assembly.

Another reason to use strong names is to make it difficult for hackers to spoof your assembly, in other words, replace or inject your assembly with a virus or malicious code.

What is a strong name key file?

A strong name key file has a .snk extension and contains a unique public-private key pair. You use the strong name key file to digitally sign your assembly (see below). Note that this type of file is not secure, as the private key in a .snk file can be easily compromised.

For added protection, Visual Studio can encrypt a strong name key file, which produces a file with the .pfx (Personal Information eXchange) extension. The .pfx file is more secure because whenever someone attempts to use the encrypted key, she will be prompted for the password.

How do I create a strong name key file for a .NET assembly?


Reference : http://www.csharp411.com/net-assembly-faq-part-3-strong-names-and-signing/

Design Guidelines in .NET Framework

Follow all .NET Framework Design Guidelines for both internal and external members. Highlights of these include:

* Do not use Hungarian notation
* Do not use a prefix for member variables (_, m_, s_, etc.). If you want to distinguish between local and member variables you should use “this.” in C# and “Me.” in VB.NET.
* Do use camelCasing for member variables
* Do use camelCasing for parameters
* Do use camelCasing for local variables
* Do use PascalCasing for function, property, event, and class names
* Do prefix interfaces names with “I”
* Do not prefix enums, classes, or delegates with any letter


Reference : http://blogs.msdn.com/brada/articles/361363.aspx

C# Array Functions

Array.ConvertAll- Convert integer array to string array

private void TestMethod(int[] intArray)

{

string[] stringArray =

Array.ConvertAll<int,string>

(intArray,new Converter<int,string>

(ConvertIntToString));

string result = string.Join(",", stringArray);

}

private string ConvertIntToString(int intParameter)

{

return intParameter.ToString();

}


Array.Resize

int[] zArray = { 1, 2, 3, 4 };

Array.Resize<int>(ref zArray, 8);


Array.ConstrainedCopy Method

Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely.


Array.AsReadOnly Method

Returns a read-only wrapper for the specified array.

Wednesday, December 17, 2008

Concept Differences

Difference between Events & Delegates

Events are the actions of the system on user manipulations (e.g. mouse clicks, key press, timer etc.) or any event triggered by the program.

Delegate is type which holds the method(s) reference in an object. It is also refered as a type safe function pointers.

What is the difference between a Thread and Process?

A process is a collection of virtual memory space, code, data, and system resources. A thread is code that is to be serially executed within a process. A processor executes threads, not processes, so each application has at least one process, and a process always has at least one thread of execution, known as the primary thread. A process can have multiple threads in addition to the primary thread. Prior to the introduction of multiple threads of execution, applications were all designed to run on a single thread of execution.

When a thread begins to execute, it continues until it is killed or until it is interrupted by a thread with higher priority (by a user action or the kernel’s thread scheduler). Each thread can run separate sections of code, or multiple threads can execute the same section of code. Threads executing the same block of code maintain separate stacks. Each thread in a process shares that process’s global variables and resources.

Difference between Struct and Class
Struct are Value type and are stored on stack, while Class are Reference type and are stored on heap.
Struct “do not support” inheritance, while class supports inheritance. However struct can implements interface.
Struct should be used when you want to use a small data structure, while Class is better choice for complex data structure.

What is the difference between the destructor and the Finalize() method? When does the Finalize() method get called?

Finalize() corresponds to the .Net Framework and is part of the System.Object class. Destructors are C#'s implementation of the Finalize() method. The functionality of both Finalize() and the destructor is the same, i.e., they contain code for freeing the resources when the object is about to be garbage collected. In C#, destructors are converted to the Finalize() method when the program is compiled. The Finalize() method is called by the .Net Runtime and we can not predict when it will be called. It is guaranteed to be called when there is no reference pointing to the object and the object is about to be garbage collected.

Value and Reference Type
Value Type
As name suggest Value Type stores “value” directly.
Stored in a Stack

For each instance of value type separate memory is allocated.

It Provides Quick Access, because of value located on stack.

Eg: int, float, char, decimal, bool, decimal, struct, etc are value types, while object type such as class, String, Array, etc are reference type.

Reference Type
As name suggest Reference Type stores “reference” to the value.

Reference type are stored on Heap.
It provides comparatively slower access, as value located on heap.

Out and Ref.

ref keyword
Passing variables by value is the default. However, we can force the value parameter to be passed by reference. Note: variable “must” be initialized before it is passed into a method.

out keyword
out keyword is used for passing a variable for output purpose. It has same concept as ref keyword, but passing a ref parameter needs variable to be initialized while out parameter is passed without initialized.

It is useful when we want to return more than one value from the method.

Note: You must assigned value to out parameter in method body, otherwise the method won’t compiled.


System.Array.CopyTo() Vs. System.Array.Clone()
The
Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element's object, resulting in a different, yet identacle object.



Jagged Arrays In C#

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays." The following examples show how to declare, initialize, and access jagged arrays.

// Single-dimensional array
int[] numbers = new int[5];

// Multidimensional array
string[,] names = new string[5,4];

// Array-of-arrays (jagged array)
int[][] jaggedArray = new int[3][];

jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];

Reference:
http://msdn.microsoft.com/en-us/library/2s05feca.aspx