Archive for category Microsoft.Net

Collections in C#.NET (SortedList & Hash Table)

Posted by admin on Friday, 28 January, 2011

SortedList

static void Main(string[] args)

{

SortedList studentRoll = new SortedList();

studentRoll.Add(4, “Cheng Boon”);

studentRoll.Add(1, “Desmong Tan”);

studentRoll.Add(3, “Poh Kang”);

studentRoll.Add(2, “Wai Yee”);

foreach (int i in studentRoll.Keys)

Console.WriteLine(“Key={0}, Value={1}”, i, studentRoll[i]);

Console.WriteLine();

Console.ReadKey();

}

HashTable

static void Main(string[] args)

{

Hashtable myHash = new Hashtable();

myHash.Add(1, “One”);

myHash.Add(“two”, 2);

myHash.Add(3, 3);

foreach (object i in myHash.Keys)

Console.WriteLine(“Key={0}, Value={1}”, i, myHash[i]);

Console.ReadKey();

}

By

Team CVK (Microsoft Competency)


Collections in C#.NET ( Queue & ArrayList)

Posted by admin on Friday, 28 January, 2011

Queue

static void Main(string[] args)

{

Queue<string> students = new Queue<string>();

students.Enqueue(“Taylor”);

students.Enqueue(“David”);

students.Enqueue(“Andrew”);

students.Enqueue(“James”);

foreach (string stud in students)

Console.WriteLine(stud);

Console.WriteLine();

Console.WriteLine(“Dequeing {0}”, students.Dequeue());

foreach (string stud in students)

Console.WriteLine(stud);

Console.WriteLine();

Console.WriteLine(“Peeking {0}”, students.Peek());

foreach (string stud in students)

Console.WriteLine(stud);

Console.WriteLine();

Console.ReadKey();

}

ArrayList

static void Main(string[] args)

{

ArrayList myValues = new ArrayList();

myValues.Add(20);

myValues.Add(“Titanic”);

myValues.Add(10.3754);

myValues.Add(false);

foreach (object obj in myValues)

Console.WriteLine(obj);

Console.WriteLine();

Console.ReadKey();

}

By

Team CVK (Microsoft Competency)


Collections in C#.Net : STACK

Posted by admin on Friday, 28 January, 2011

Stack

static void Main(string[] args)

{

Stack<string> students = new Stack<string>();

students.Push(“Elizabeth”);

students.Push(“Renee”);

students.Push(“Philip”);

students.Push(“Andy”);

foreach (string stud in students)

Console.WriteLine(stud);

Console.WriteLine();

Console.WriteLine(“Popping {0}”, students.Pop());

foreach (string stud in students)

Console.WriteLine(stud);

Console.WriteLine();

Console.WriteLine(“Peeking {0}”, students.Peek());

foreach (string stud in students)

Console.WriteLine(stud);

Console.WriteLine();

Console.ReadKey();

}

By

Team CVK (Microsoft Competency)


Collections in C#.Net : DICTIONARY

Posted by admin on Friday, 28 January, 2011

static void Main(string[] args)

{

Dictionary<int, string> studentDict = new Dictionary<int, string>();

studentDict.Add(1, “Smith”);

studentDict.Add(2, “Trevor”);

studentDict.Add(3, “Aaron”);

//Throws an exception if key already exists

try

{

studentDict.Add(2, “Sharon”);

}

catch (ArgumentException)

{

Console.WriteLine(“Key already exists”);

}

//Add values by specifyin a key that doesn’t exist

studentDict[4] = “Brian”;

//See if a key exists and get its value

if (studentDict.ContainsKey(4))

Console.WriteLine(“Key = 4, Value={0}”, studentDict[4]);

//See if a value exists and get its key

if (studentDict.ContainsValue(“Aaron”))

Console.WriteLine(“Value=Aaron exists”);

//Try to get a value, if you are not sure of the key

string value = string.Empty;

if (studentDict.TryGetValue(5, out value))

Console.WriteLine(“Key 5 exists and the value is – {0}”, value);

else

Console.WriteLine(“Key 5 doesn’t exist”);

Console.WriteLine();

//Printing with keys

foreach (int i in studentDict.Keys)

Console.WriteLine(studentDict[i]);

Console.WriteLine();

//Printing with values

foreach (string val in studentDict.Values)

Console.WriteLine(val);

Console.ReadKey();

}

By

Team CVK (Microsoft Competency)


Collections in C#.Net : LIST

Posted by admin on Friday, 28 January, 2011

Like an array, a collection is also a group of some values but the number of values that you store in a collection is not limited. Also, with the weakly typed collections, you can have different types of values stored in a single collection.

List

class ListDemo

{

static void Main(string[] args)

{

List<string> students = new List<string>();

students.Add(“John”);

students.Add(“Emily”);

students.Add(“Aakash”);

Console.WriteLine(“Capacity: {0}”, students.Capacity);

PrintValues(students);

Console.WriteLine();

students.Insert(2, “Mukesh”);

Console.WriteLine(“Capacity after insert: {0}”, students.Capacity);

PrintValues(students);

Console.WriteLine();

students.Remove(“Emily”);

Console.WriteLine(“Capacity after removing Emily: {0}”, students.Capacity);

PrintValues(students);

Console.WriteLine();

Console.WriteLine(“Location of Mukesh: {0}”, students.IndexOf(“Mukesh”));

Console.WriteLine();

students.Clear();

Console.WriteLine(“Capacity after clear: {0}”, students.Capacity);

PrintValues(students);

Console.WriteLine();

Console.ReadKey();

}

static void PrintValues(List<string> stringList)

{

foreach (string stringVal in stringList)

Console.WriteLine(stringVal);

}

}

By

Team CVK ( Microsoft Competency)


Arrays in C#.NET

Posted by admin on Friday, 28 January, 2011

An array is a group of values that belong to same data type. The values in an array are called as ‘elements’. The following code snippet shows how to declare and initialize an array.

int[] intArray = new int[3]; //New array with 3 integer elements

//Array initialization at the time of declaration

int[] intArray1 = new int[3] { 10, 20, 30 };

//Array size is determined by number of values in the initilization

int[] intArray2 = new int[] { 20, 30, 40 };

//Arrady initialization without “new” operator

int[] intArray3 = {30,40,50};

//string array

string[] stringArray = new string[] { “one”, “two”, “three” };

To store a value into the second element of the array:

intArray[3] = 25;

To retrieve value of first element in the array:

Console.WriteLine(“First element: {0}”, stringArray[0]);

The examples shown above deal with single dimensional arrays. You can store large number of values with multi-dimensional arrays. Here are some examples.

int[,] intArray = new int[2,2]; //New array with total 4 integer elements

int[,] intArray1 = new int[2,2] { {10, 20}, {30,40} };

int[,] intArray2 = new int[,] { { 10, 20 }, { 30, 40 } };

int[,] intArray3 = { { 10, 20 }, { 30, 40 } };

string[,] stringArray = new string[,] { {“one”, “two”}, {“three”, “four”} };

Lastly, you can declare an array of arrays. Here is how:

int[][] intArray = new int[2][];

intArray[0] = new int[2];

intArray[1] = new int[3];

(or)

int[][] intArray1 = new int[2][] { new int[]{10,20}, new int[]{30,40,50}};

By

Team CVK (Microsoft Competency)


String Vs. StringBuilder in C #.NET

Posted by admin on Friday, 28 January, 2011

If you have to deal with large number of string concatenations, the StringBuilder class gives much faster performance than the regular “string” data type.

There are multiple ways to concatenate a string, with the “string” data type. They are:

stringVar = stringVar + “some string literal”;

stringVar += “some string literal”;

Experts say that the first method (in the code above) gives better performance than the second one.

Finally, here is some code that explains the use of StringBuilder while comparing its performance with string.

static void Main(string[] args)

{

string normalString = string.Empty;

StringBuilder sbString = new StringBuilder();

int capacity = sbString.Capacity;

Console.WriteLine(“Initial StringBuilder capacity: {0}”, capacity);

DateTime startTime = DateTime.Now;

for (int i = 0; i < 12000; i++)

normalString = normalString + “This is a very very very very very very looooooooooooooooooooooooong string”;

DateTime endTime = DateTime.Now;

TimeSpan runTime = endTime – startTime;

Console.WriteLine(“Time to execute with string: {0}”, runTime);

startTime = DateTime.Now;

for (int i = 0; i < 12000; i++)

{

sbString = sbString.Append(“This is a very very very very very very looooooooooooooooooooooooong string”);

if (capacity != sbString.Capacity)

{

capacity = sbString.Capacity;

Console.WriteLine(“StringBuilder capacity increased to: {0}”, capacity);

}

}

endTime = DateTime.Now;

runTime = endTime – startTime;

Console.WriteLine(“String length: {0}”, sbString.ToString().Length);

Console.WriteLine(“Time to execute with StringBuilder: {0}”, runTime);

Console.ReadKey();

}

By

Team CVK ( Microsoft Competency)


Nullable types in C# .Net

Posted by admin on Friday, 28 January, 2011

The normal data types expect some valid value. However, when you are using these variables to store values from a database table you can’t be sure that you always get a proper value. The database table may have null values. C# provides nullable data types to deal with this situation.

  1. Create a new console application and type the following code into the “main” function.

static void Main(string[] args)

{

int normalInt = 0; //<- normal integer type variable

int? nullableInt = null; //<- nullable integer type

//Tells you whether the variable has a proper value

Console.WriteLine(nullableInt.HasValue);

nullableInt = 10;

Console.WriteLine(nullableInt.HasValue);

//Gives the value

Console.WriteLine(“nullableInt: {0}”, nullableInt.Value);            Console.ReadKey();

}

There is a special operator “??” that can be used with nullable types. The following code demonstrates the use of this operator.

static void Main(string[] args)

{

int? valueOne = null;

int? valueTwo = null;

//The following returs the value of valueOne, if it is not null

//Returns zero otherwise

Console.WriteLine(“non-null value: {0}”, valueOne ?? 0);

//The following returs the value of valueOne, if it is not null

//If valueOne is null, it returns the value of valueTwo, if it is not null

//Returns zero otherwise

Console.WriteLine(“non-null value: {0}”, valueOne ?? valueTwo ?? 0);

valueOne = 10;

Console.WriteLine(“non-null value: {0}”, valueOne ?? valueTwo ?? 0);

valueOne = null;

valueTwo = 20;

Console.WriteLine(“non-null value: {0}”, valueOne ?? valueTwo ?? 0);

Console.ReadKey();

}

By

Team  CVK ( Microsoft Competency)


Formatting DateTime values in C#.Net

Posted by admin on Friday, 28 January, 2011

In the following example you will see how to use standard and custom format strings that can be used with DateTime type of values.

  1. Create a new console application and type the following code into the “main” function.

static void Main(string[] args)

{

DateTime currentTime = DateTime.Now;

Console.WriteLine(“Default: {0}”, currentTime.ToString());

Console.WriteLine(“Short date pattern: {0:d}”, currentTime);

Console.WriteLine(“Long date pattern: {0:D}”, currentTime);

Console.WriteLine(“Month/day pattern: {0:D}”, currentTime);

Console.WriteLine(“Short time format: {0:t}”, currentTime);

Console.WriteLine(“Long time format: {0:T}”, currentTime);

//Custom formatting

Console.WriteLine(currentTime.ToString(“dd-MM-yyyy”));

Console.WriteLine(currentTime.ToString(“ddd, dd-MMM-yyyy”));

Console.ReadKey();

}

By

Mr.Prasad GVL

Microsoft Technology Trainer @ CVK


Formatting ‘Double’ values

Posted by admin on Friday, 28 January, 2011

‘float’ and ‘double’ are the two floating-point types available in C#. You can use the following program with either of these types. All of the format strings shown in the previous example can be used with this data type also.

The following example shows the use custom-format strings with numeric data types.

  1. Create a new console application and type the following code into the “main” function.

static void Main(string[] args)

{

double myDouble = 12345.67;

long myPhone = 1234567890;

Console.WriteLine(“000000.0000: {0:000000.0000}”, myDouble);

Console.WriteLine(“######.####: {0:######.####}”, myDouble);

Console.WriteLine(“(###) ### – ####: {0:(###) ### – ####}”,

myPhone);

Console.ReadKey();

}

2. Run the program. The output should look like: