Tuesday, July 26, 2011

Tuesday 7/26/11


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

namespace Enumberable
{
public class ListBoxTest : IEnumerable
{
private string[] strings;
private int ctr = 0;
//enumberable classes can return an enumerator
public IEnumerator GetEnumerator()
{
foreach (string s in strings)
{
yield return s;
}
}

//explicit interface implementation.
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

//initialize the listbox with strings
public ListBoxTest(params string[] initialStrings)
{
//allocate space for the strings
strings = new String[10];
//copy the string passed in to the constructor
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}

//add a single string to the end of the listbox
public void Add(string theString)
{
strings[ctr] = theString;
ctr++;
}

//allow array-like access
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
//handle bad index
}
return strings[index];
}
set
{
strings[index] = value;
}
}

//publish how many strings you hold
public int GetNumEntries()
{
return ctr;
}
}

public class Tester
{
static void Main()
{
//create a new listbox and initialize
ListBoxTest lbt = new ListBoxTest("Howdy", "World");

//add a few strings
lbt.Add("Who");
lbt.Add("Is");
lbt.Add("Douglas");
lbt.Add("Adams");

//test the access
string subst = "Universe";
lbt[1] = subst;
;
//accesss all the strings
foreach (string s in lbt)
{
if (s != null)
{
Console.WriteLine("Value: {0}", s);
}
else
{
Console.WriteLine("Value: Null");
}
}
}
}
}




using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
var worldCup2006Finalists = new[]{new
{
TeamName = "France",
Players = new string[]
{
"A", "B","C", "D",
}
},
new
{
TeamName = "Italy",
Players = new string[]
{
"Q", "W","E", "R",
}
}
};
Print(worldCup2006Finalists);
}

private static void Print(IEnumerable items)
{
foreach (T item in items)
{
Console.WriteLine(item);
}
}
}




using System;

class RefTest
{
public void sqr(ref int i)
{
i = i * i;
}
}

class MainClass
{
public static void Main()
{
RefTest ob = new RefTest();
int a = 10;
Console.WriteLine("a before call: " + a);
ob.sqr(ref a);
Console.WriteLine("a after call: " + a);
}
}

No comments: