Wednesday, July 27, 2011

Wednesday, 7/27/2011


using System;
using System.Collections.Generic;
using System.Text;

namespace ListCollection
{
//a simple class to store in the list
public class Employee
{
public Employee(int empID)
{
this.EmpID = empID;
}
public override string ToString()
{
return EmpID.ToString();
}
public int EmpID { get; set; }
}

public class Tester
{
static void Main()
{
//with an Array class, you define how many objects the array will hold
//with a list, you don't declare hwo many objects the List will hold
List empList = new List();
List intList = new List();

//populate the List
for (int i = 0; i < 5; i++)
{
empList.Add(new Employee(i+20));
intList.Add(i*3);
}

//print all the contents
for (int i = 0; i < intList.Count; i++)
{
Console.Write("{0} ", intList[i].ToString());
}

Console.WriteLine("\n");

//print all the contents of the Employee List
for (int i = 0; i < empList.Count; i++)
{
Console.Write("{0} ", empList[i].ToString());
}

Console.WriteLine("\n");
Console.WriteLine("empList.Capacity: {0}", empList.Capacity);
}
}
}




//the current Employee object must compare itself ot the Employee [assed in as a parameter
//and return -1 if it is smaller than the parameter, 1 if it is greater than the parameter
//and 0 if it is equal to the paramter

using System;
using System.Collections.Generic;
using System.Text;

namespace IComparable
{
//a simple class to store in the array
public class Employee : IComparable
{
private int empID;

public Employee(int empID)
{
this.empID = empID;
}

public override string ToString()
{
string str = "EmpID " + empID.ToString();
return str;
}

public bool Equals(Employee other)
{
if (this.empID == other.empID)
{
return true;
}
else
{
return false;
}
}

//comparer delegates back to Employee
//Employee uses the integer's default
//CompareTo method

public int CompareTo(Employee rhs)
{
return this.empID.CompareTo(rhs.empID);
}
}

public class Tester
{
static void Main()
{
List empArray = new List();
List intArray = new List();
//generate random numbers for
//both the integers and the employee IDs

Random r = new Random();

//populate the array
for (int i = 0; i < 5; i++)
{
//add a random employee id
empArray.Add(new Employee(r.Next(10) + 200));
//add a random integer
intArray.Add(r.Next(10));
}

//display all the contents of the int array
for (int i = 0; i < intArray.Count; i++)
{
Console.Write("{0} ", intArray[i].ToString());
}

Console.WriteLine("\n");

//display all the contents of the Employee array
for (int i = 0; i < empArray.Count; i++)
{
Console.Write("{0} ", empArray[i].ToString());
}

Console.WriteLine("\n");

//sort and display the int array
intArray.Sort();
for (int i = 0; i < intArray.Count; i++)
{
Console.Write("{0} ", intArray[i].ToString());
}
Console.WriteLine("\n");

//sort and display the employee array
empArray.Sort();

//display all the contents of the Employee array
for (int i = 0; i < empArray.Count; i++)
{
Console.Write("{0} ", empArray[i].ToString());
}
Console.WriteLine("\n");
}
}
}




using System;

class MainClass
{
public static void Main()
{
//all implicit
sbyte v = 55;
short v2 = v;
int v3 = v2;
long v4 = v3;

Console.WriteLine(v);
Console.WriteLine(v2);
Console.WriteLine(v3);
Console.WriteLine(v4);


//explicit to "smaller" types
v3 = (int)v4;
v2 = (short)v3;
v = (sbyte)v2;
Console.WriteLine(v);
Console.WriteLine(v2);
Console.WriteLine(v3);
}
}




using System;

class Example{
//void means that the method does not have to have a return type
public static void Main(){
//returns the boolean response
Console.WriteLine("10 > 9 is " + (10 > 9));
}
}




using System;

class Example
{
public static void Main()
{
bool b;

b = false;
Console.WriteLine("b is " + b);
b = true;
Console.WriteLine("b is " + b);

//a bool value can control the if statement
if (b)
{
Console.WriteLine("This is executed.");
}
b = false;
if (b)
{
Console.WriteLine("This is not executed.");
}
}
}




using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo[] cultures = {
CultureInfo.CreateSpecificCulture("en-US"),
CultureInfo.CreateSpecificCulture("fr-FR"),
CultureInfo.CreateSpecificCulture("es-ES")};
sbyte positiveNumber = 1;
sbyte negativeNumber = -5;
string[] specifiers = { "G", "C", "D4", "E2", "F", "N", "P", "X2" };

foreach (string specifier in specifiers)
{
foreach (CultureInfo culture in cultures)
{
Console.WriteLine("{0,2} format using {1} culture:{2,16} {3,16}",
specifier,
culture.Name,
positiveNumber.ToString(specifier, culture),
negativeNumber.ToString(specifier, culture));
}
}
}
}





using System;

class MainClass
{
public static void Main()
{
int[] intArray = new int[10];
int arrayLength = intArray.Length;
//total length of array;
Console.WriteLine("arrayLength = " + arrayLength);
for (int counter = 0; counter < arrayLength; counter++)
{
intArray[counter] = counter + 1;
Console.WriteLine("intArray[" + counter + "] = " + intArray[counter]);
}
}
}




using System;

class MainClass
{
public static void Main()
{
Console.WriteLine(5.ToString());
}
}




using System;

class MainClass
{
public static void Main()
{
int v = 55;
object o = v; //box v into o
Console.WriteLine("Value is {0}", o);
int v2 = (int)o;
Console.WriteLine(v2.ToString());
}
}

No comments: