Resources

Types and Members in C# applications

Lesson1

Using Data Types

int anInteger = 10000;

short aShort;

aShort = (short) anInteger;

Data Type Functionality

int I = 100;

object O;

O = I;
string S = "1234";

int I = int.Parse(S);
Name Description
String.Insert Insert specified string into instance
String.PadLeft, String.PadRight Add chars to left or right of instance
String.Remove Delete specified number chars from string
String.Replace Replace occurrences of char in string
String.Split Return substrings delimited by specific char
String.Substring Return substring from string
String.ToCharAray Returns arrays of chars making up string
String.ToLower, String.ToUpper Convert string to lower or upper case
String.TrimEnd, String.TrimStart, String.Trim   Remove trailing or leading characters

 

Name Description
String.Compare   Compare two string objects
String.Concat Concatenate two+ strings
String.Format Format a string
String.Join Concatenate array of strings with specified separator strings

 

Lesson 2

Constants

public const double Pi = 3.14159265;

Enums

public enum DaysOfWeek
{
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
Sunday = 7
}
public enum DaysOfWeek : byte
{
...
}
Public enum Numbers
{
zero,
one,
}

MessageBox.Show((int)Numbers.two * 2).ToString()); // Display 4
public void ScheduleDayOff( DaysOfWeek day )
{
switch( day )
{
...
}
}

Arrays

int[] myIntegers;
myIntegers = new int[32];
int[] myIntegers = new int[32];
int[] myIntegers = new int[32];

myIntegers = new int[45];

// Declare 5 by 3 array
int[,] intArray = new int[5,3];

// Decalre 2D array and set initial values
int[,] intArray2 = {{1,2,3},{4,5,6}};

// Declare cubic array
int[,,] cubeArray = {{7,2},{1,4},{3,5}};

string[][] Families = new string[3][];

Families[0] = new string[] {"Smith", "Mum", "Dad", "Uncle Phil"};

Families[1] = new string[] {"Jones", "Mum", "Dad", "Suzie", "Bobby"};

Families[2] = new string[] {"Williams", "Earl", "Bob"};

Collections

Class Description
ArrayList Array of objects
BitArray Compact arrays of bits (0 and 1)
CollectionBase   Base for implementing own collection class
Hashtable Key-value pairs organised by hashed key
Queue FIFO group of objects
SortedList Access objects by index or key
Stack FILO group of objects z

 

// Create ArrayList
System.Collections.ArrayList myList = new System.Collections.ArrayList;

//Add item
Widget myWidget = new Widget;
myList.Add(myWidget);

// Access item using indexer (note ArrayList is zero-based)
object myObject;
myObject = myList[0];

// Indexer always returns objects. To obtain reference to same type as stored must cast
Widget aWidget;
aWidget = (Widget) myList[0];

// Remove uses reference to object
Widget anotherWidget = new Widget();
myList.Add(anotherWidget);
myList.Remove(anotherWidget);

// RemoveAt uses indexer
myList.RemoveAt(0);

// Count property is number of items in collection (as array zero based the value returned in one more than upper bound)

int arraySize = myList.Count;
int[] myArray = new int[] {1,2,3,4,5};

for(int x=0; x <= myArray.GetUpperBound(0), x++)
{
myArray[x]++;

MessageBox(myArray[x].ToString());
}

Lesson 3

Implementing Properties

textBox1.Text = "Text property";

string myString;

myString = textBox1.Text;
private string theText;

public string myText
{
get
{
return theText;
}

set
{
theText = value;
}
}

Read Only Properties

private readonly int theInt;

public int InstanceNumber
{
get
{
return theInt;
}
}

Write Only Properties

Indexer

private int[] IntArray;

public int this [int index]
{
get
{
return IntArray[index];
}

set
{
Intarray[index] = value;
}
}

Collection Properties

private readonly System.Collections.ArrayList myWidgets = new System.Collections.ArrayList();

public System.Collections.ArrayList Widgets
{
get
{
return myWidgets;
}
}
private System.Collections.ArrayList myWidgets = new System.Collections.ArrayList ();

public Widget GetWidget(int I)
{
return (Widget)myWidgets[I];
}

public void SetWidget(int I, Widget Wid)
{
myWidgets[I] = Wid;
}

Lesson 4

Delegates and Events

Delegates

public delegate int myDelegate(double D);

// Target method for delegate

public int ReturnInt(double D)
{

}

// Create instance of myDelegate
public void aMethod()
{
myDelegate aDelegate = new myDelegate(ReturnInt);
}

// Use delegate to invoke method

aDelegate(12345);

Declaring and Raising Events

// Declare delegate and event
public delegate void calculateDelegate(double D);
public event calculationDelegate CalculationComplete;

// Raise event
CalculationComplete(66532);

Event Handlers

// Assume existence of method DisplayResults with signature appropriate for calculationDelegate.

// Create new delegate to create the association.
Account.CalculationComplete += new calculationDelegate(DisplayResults);

// Create association with existing delegate
CalculationDelegate calc = new calculationDelegate(DisplayResults);

Account.CalculationComplete += calc;
// System.EventHandler is delegate for most controls in System.windows.Formds namespace. Designate event handler for Click event of control called button1

button1.Click += new System.EventHandler(clickHandler);
// Remove association between Account.CalculationComplete and DisplayResults method

Account.CalculatioComplete -= new CalculationDelegate(DisplayResults);
Button1.Click += new System.EventHandler(ClickHandler);

Button2.Click += new System.EventHandler(ClickHandler);

Downloads