Saturday, May 19, 2012

.NET - Strings, Encodings

Notes:

1. string.Compare(stringA, stringB, boolIgnoreCase, CultureInfo)



 //the infamous Turkish I problem
            string a = "file";
            string b = "file";
            bool equalInvariant = string.Compare(a, b, true, CultureInfo.InvariantCulture) == 0;
            bool equalTurkish = string.Compare(a, b, true, CultureInfo.CreateSpecificCulture("tr-TR")) == 0;



2. stringA.ToUpper() ---Uses current culture
    stringA.ToUpperInvariant()
    stringA.ToUpper(CultureInfo.CreateSpecificCulture("tr-TR"))


3. stringA.Split(delimitersCharArray);


4.

void Main()
{
string name = "Manikandan";

var bytes = Encoding.ASCII.GetBytes(name);

Console.WriteLine ("ASCII: Bytes: {0} Length: {1}", BitConverter.ToString(bytes), bytes.Length);


bytes = Encoding.Unicode.GetBytes(name);

Console.WriteLine ("Unicode Bytes: {0} Length: {1}", BitConverter.ToString(bytes), bytes.Length);

bytes = Encoding.UTF7.GetBytes(name);

Console.WriteLine ("UTF7 Bytes: {0} Length: {1}", BitConverter.ToString(bytes), bytes.Length);

bytes = Encoding.UTF8.GetBytes(name);

Console.WriteLine ("UTF8 Bytes: {0} Length: {1}", BitConverter.ToString(bytes), bytes.Length);

bytes = Encoding.UTF32.GetBytes(name);

Console.WriteLine ("UTF8 Bytes: {0} Length: {1}", BitConverter.ToString(bytes), bytes.Length);

}

output:


ASCII: Bytes: 4D-61-6E-69-6B-61-6E-64-61-6E
Length: 10

Unicode Bytes: 4D-00-61-00-6E-00-69-00-6B-00-61-00-6E-00-64-00-61-00-6E-00
Length: 20

UTF7 Bytes: 4D-61-6E-69-6B-61-6E-64-61-6E
Length: 10

UTF8 Bytes: 4D-61-6E-69-6B-61-6E-64-61-6E
Length: 10

UTF8 Bytes: 4D-00-00-00-61-00-00-00-6E-00-00-00-69-00-00-00-6B-00-00-00-61-00-00-00-6E-00-00-00-64-00-00-00-61-00-00-00-6E-00-00-00
Length: 40

Tuesday, May 15, 2012

C# - Enum

References:




Notes:




  • System.Enum - Derived from ValueType
  • Flag
  • enumerations must have values explicitly assigned that are unique powers of two: 1 2 4 8 16 32 to work correctly.
  • Important methods:
  • Enum.TryParse()
  • Enum.Parse()
  • Enum.IsDefined()
  • HasFlag()
  • Enum.GetValues(typeof(MyEnumInstance))
  • Enum.GetName(typeof(MyEnumInstance), myEnumVariable)
  • You can't override ToString()  for an Enum
  • You can attach meta data to Enum types with Attribute mechanism and Extension Methods.  
  • (MyAttribute[]) MyEnumVariable.GetType().GetField(
  • MyEnumVariable.ToString()).GetCustomAttributes(typeof(MyAttributes), false);