use alias for generics in c#2.0

Aliased generics:

using ASimpleName = Dictionary<string, Dictionary<string, List<string>>>;

It allows you to use ASimpleName, instead of Dictionary<string, Dictionary<string, List<string>>>.

Use it when you would use the same generic big long complex thing in a lot of places.

Nice extention methods for Enums

I find  very nice Extention for enums  and i want to share it.

[Flags]
public enum ErrorTypes : int {
    None = 0,
    MissingPassword = 1,
    MissingUsername = 2,
    PasswordIncorrect = 4
}
 
public static class EnumExtensions {
 
    public T Append<T>(this System.Enum type, T value) {
        return (T)(object)(((int)(object)type | (int)(object)value));
    }
 
    public static T Remove<T>(this System.Enum type, T value) {
        return (T)(object)(((int)(object)type & ~(int)(object)value));
    }
 
    public static bool Has<T>(this System.Enum type, T value) {
        return (((int)(object)type & (int)(object)value) == (int)(object)value);
    }
 
}
 
...
 
//used like the following...
 
ErrorTypes error = ErrorTypes.None;
error = error.Append(ErrorTypes.MissingUsername);
error = error.Append(ErrorTypes.MissingPassword);
error = error.Remove(ErrorTypes.MissingUsername);
 
//then you can check using other methods
if (error.Has(ErrorTypes.MissingUsername)) {
    ...
}

http://stackoverflow.com/questions/9033/hidden-features-of-c/407325#407325

Yield Keyword C# with IEnumerable

Used in an iterator block to provide a value to the enumerator object or to signal the end of iteration.

expression is evaluated and returned as a value to the enumerator object; expressionhas to be implicitly convertible to the yield type of the iterator.

The yield statement can only appear inside an iterator block, which might be used as a body of a method, operator, or accessor. The body of such methods, operators, or accessors is controlled by the following restrictions:

  • Unsafe blocks are not allowed.
  • Parameters to the method, operator, or accessor cannot be ref or out.

yield statement cannot appear in an anonymous method. For more information, seeAnonymous Methods (C# Programming Guide).

When used with expression, a yield return statement cannot appear in a catch block or in a try block that has one or more catch clauses. For more information, seeException Handling Statements (C# Reference).

using System;
using System.Collections.Generic;
 
public class MyClass
{
    public static  IEnumerable<int> Powers(int from,int to)
    {
        for (int i = from; i <= to; ++i)
        {
            yield return (int)Math.Pow(2, i);
        }
    }
 
    static void Main()
    {
        IEnumerable<int> powers = Powers(0, 16);
        foreach (int result in powers)
        {
            Console.WriteLine(result);
        }
    }
}
Source :MSDN

Define data type for enums

The data type can be defined for an enumeration:

enum EnumName : [byte, char, int16, int32, int64, uint16, uint32, uint64]
{
    A = 1,
    B = 2
}

Enum Inheritance

this is not possible. Enums cannot inherit from other enums. In fact all enums must actually inherit from System.Enum. C# allows syntax to change the underlying representation of the enum values which looks like inheritance, but in actuality they still inherit from System.enum.

See section 8.5.2 of the CLI spec for the full details. Relevant information from the spec

  • All enums must derive from System.Enum
  • Because of the above, all enums are value types and hence sealed

Source:
http://stackoverflow.com/questions/757684/enum-inheritance/757762#757762