Posts

Showing posts with the label Computer Programming

C#: System.Collections.Generic.Dictionary

Namespace: System.Collections.Generic Declaration: Dictionary<string, string> account = new Dictionary<string, string>(); Adding Items: account.Add("id","1"); account.Add("username", "jackd"); Use in ArrayList: Namespace: System.Collections Add Dictionary to ArrayList: Dictionary<string, string> account = new Dictionary<string, string>(); ArrayList aList = new ArrayList(); account.Add("id","1"); account.Add("username", "jackd"); aList.Add(account); account = new Dictionary<string, string>(); account.Add("id","2"); account.Add("username", "janed"); aList.Add(account); account = new Dictionary<string, string>(); account.Add("id","3"); account.Add("username", "d"); Loop Through ArrayList of Dictionary Type: foreach (Dictionary<string, string> dic in aList) { Response.Write("id: ...

C# : Automatically Implemented Properties

There are times when you see a class property without it's private member counterpart and all you see is the {set; get;} accessors. What you are looking at are auto properties that you can create  in C#, the only requirements is that the set; and get; accessors contains no logic.  Usually there are private members that the properties expose to other classes using the get/set accessors, like the code below: public class Product { private int productId; private string name; private string description; private decimal price; public int ProductId { get { return productId; } set { productId = value; } } public string Name { get { return name; } set { name = value; } } public string Description { get { retur...

C# : Arrays

Arrays are fixed size elements of a type, arrays are stored next to each other in the memory. Making them very fast and efficient.  However you must know the exact size of an array when you declare an array. Declaring an array: string[] names = new string[5]; There are two ways you can assign values to an array. The first is to assign the values individually by specify the index of the array inside a square bracket. The index is the position of element in the array. Index starts with 0. names[0] = "George"; names[1] = "James"; names[2] = "Arthur"; names[3] = "Eric"; names[4] = "Jennifer"; Or you can initialize and populate the array at the same time, like the example below. string[] names = new string[]{"George","James","Arthur","Eric","Jennifer"}; You don't have to specify the size of the array if you initialize during the declaration, C# is smart enough to figure out the array siz...