Monday 17 December 2012

c# for dummies: c sharp programming

c# for dummies: c sharp programming: THE SORTEDLIST CLASS As we mentioned in the Introduction section of this chapter, a SortedList is a data structure that stores key–valu...

c sharp programming


THE SORTEDLIST CLASS

As we mentioned in the Introduction section of this chapter, a SortedList is a
data structure that stores key–value pairs in sorted order based on the key.We
can use this data structure when it is important for the keys to be sorted, such
as in a standard word dictionary, where we expect the words in the dictionary
to be sorted alphabetically. Later in the chapter, we’ll also see how the class
can be used to store a list of single, sorted values.

Using the SortedList Class
We can use the SortedList class in much the same way we used the classes
in the previous sections, since the SortedList class is a specialization of the
DictionaryBase class.
To demonstrate this, the following code creates a SortedList object that
contains three names and IP addresses:
SortedList myips = New SortedList();
myips.Add("Mike", "192.155.12.1");
myips.Add("David", "192.155.12.2");
myips.Add("Bernica", "192.155.12.3");

The name is the key and the IP address is the stored value.

The generic version of the SortedList class allows you to decide the data
type of both the key and the value:
SortedList<Tkey, TValue>
For this example, we could instantiate myips like this:
SortedList<string, string> myips =
new SortedList<string, string>();

A grade book sorted list might be instantiated as follows:
SortedList<string, int> gradeBook =
new SortedList<string, int>();
We can retrieve the values by using the Item method with a key as the
argument:

Foreach(Object key In myips.Keys)
Console.WriteLine("Name:"&key+"\n" +
"IP: " & myips.Item(key))
This fragment produces the following output:


Alternatively, we can also access this list by referencing the index num-
bers where these values (and keys) are stored internally in the arrays, which
actually store the data. Here’s how:
for(inti=0;i< myips.Count; i++)
Console.WriteLine("Name:"+ myips.GetKey(i) + "\n" +
"IP: " & myips.GetByIndex(i));


This code fragment produces the exact same sorted list of names and IP
addresses:



A key–value pair can be removed from a SortedList by either specifying a
key or specifying an index number, as in the following code fragment, which
demonstrates both removal methods:
myips.Remove("David");
myips.RemoveAt(1);

If you want to use index-based access into a SortedList but don’t know the
indexes where a particular key or value is stored, you can use the following
methods to determine those values:
int indexDavid = myips.GetIndexOfKey("David");
int indexIPDavid = _
myips.GetIndexOfValue(myips.Item("David"));
The SortedList class contains many other methods and you are encouraged
to explore them via VS.NET’s online documentation.

c# for dummies: c sharp programming

c# for dummies: c sharp programming: THE GENERIC KEYVALUEPAIR CLASS C# provides a small class that allows you to create dictionary-like objects that store data based on a key. T...

c sharp programming

THE GENERIC KEYVALUEPAIR CLASS
C# provides a small class that allows you to create dictionary-like objects that
store data based on a key. This class is called the KeyValuePair class. Each
object can only hold one key and one value, so its use is limited.
A KeyValuePair object is instantiated like this:
KeyValuePair<string, int> mcmillan =
new KeyValuePair<string, int>("McMillan", 99);
The key and the value are retrieved individually:
Console.Write(mcmillan.Key);
Console.Write(""+ mcmillan.Value);


The KeyValuePair class is better used if you put the objects in an array. The
following program demonstrates how a simple grade book might be imple-
mented:

using System;
using System.Collections.Generic;
using System.Text;
namespace Generics
{
class Program
{
static void Main(string[] args)

{
KeyValuePair<string, int>[] gradeBook = new
KeyValuePair<string, int>[10];
gradeBook[0] = new KeyValuePair<string,
int>("McMillan", 99);
gradeBook[1] = new KeyValuePair<string,
int>("Ruff", 64);
for (inti=0;i<= gradeBook.GetUpperBound(0); i++)
if (gradeBook[i].Value != 0)
Console.WriteLine(gradeBook[i].Key + ": " +
gradeBook[i].Value);
Console.Read();
}
}

}

c# for dummies: c sharp programming

c# for dummies: c sharp programming: Other DictionaryBase Methods There are two other methods that are members of the DictionaryBase class: CopyTo and GetEnumerator. We dis...

c sharp programming


Other DictionaryBase Methods
There are two other methods that are members of the DictionaryBase class:
CopyTo and GetEnumerator. We discuss these methods in this section.
The CopyTo method copies the contents of a dictionary to a one-
dimensional array. The array should be declared as a DictionaryEntry array,
though you can declare it asObject and then use theCType function to convert
the objects to DictionaryEntry.
The following code fragment demonstrates howto use the CopyTomethod:
IPAddresses myIPs = new IPAddresses("c:\ips.txt");
DictionaryEntry[] ips = _
new DictionaryEntry[myIPs.Count-1];
myIPs.CopyTo(ips, 0);

The formula used to size the array takes the number of elements in the dic-
tionary and then subtracts one to account for a zero-based array. The CopyTo
method takes two arguments: the array to copy to and the index position to
start copying from. If you want to place the contents of a dictionary at the
end of an existing array, for example, you would specify the upper bound of
the array plus one as the second argument.
Once we get the data from the dictionary into an array, we want to work
with the contents of the array, or at least display the values. Here’s some code
to do that:
for(inti=0;i<= ips.GetUpperBound(0); i++)
Console.WriteLine(ips[i]);



Unfortunately, this is not what we want. The problem is that we’re storing
he data in the array as DictionaryEntry objects, and that’s exactly what we
see. If we use the ToString method:
Console.WriteLine(ips[ndex]ToString())
we get the same thing. In order to actually view the data in a DictionaryEntry
object,we have to use either the Key property or the Value property, depending
on if the object we’re querying holds key data or value data. So how do we
know which is which? When the contents of the dictionary are copied to the
array, the data is copied in key–value order. So the ?rst object is a key, the
second object is a value, the third object is a key, and so on.
Now we can write a code fragment that allows us to actually see the data:

for(inti=0;i<= ips.GetUpperBound(0); i++) {
Console.WriteLine(ips[index].Key);
Console.WriteLine(ips[index].Value);
}

c# for dummies: c sharp programming

c# for dummies: c sharp programming: Other DictionaryBase Methods There are two other methods that are members of the DictionaryBase class: CopyTo and GetEnumerator. We discuss ...

c sharp programming

Other DictionaryBase Methods
There are two other methods that are members of the DictionaryBase class:
CopyTo and GetEnumerator. We discuss these methods in this section.
The CopyTo method copies the contents of a dictionary to a one-
dimensional array. The array should be declared as a DictionaryEntry array,
though you can declare it asObject and then use theCType function to convert
the objects to DictionaryEntry.
The following code fragment demonstrates howto use the CopyTomethod:
IPAddresses myIPs = new IPAddresses("c:\ips.txt");
DictionaryEntry[] ips = _
new DictionaryEntry[myIPs.Count-1];
myIPs.CopyTo(ips, 0);

The formula used to size the array takes the number of elements in the dic-
tionary and then subtracts one to account for a zero-based array. The CopyTo
method takes two arguments: the array to copy to and the index position to
start copying from. If you want to place the contents of a dictionary at the
end of an existing array, for example, you would specify the upper bound of
the array plus one as the second argument.
Once we get the data from the dictionary into an array, we want to work
with the contents of the array, or at least display the values. Here’s some code
to do that:
for(inti=0;i<= ips.GetUpperBound(0); i++)
Console.WriteLine(ips[i]);



Unfortunately, this is not what we want. The problem is that we’re storing
he data in the array as DictionaryEntry objects, and that’s exactly what we
see. If we use the ToString method:
Console.WriteLine(ips[ndex]ToString())
we get the same thing. In order to actually view the data in a DictionaryEntry
object,we have to use either the Key property or the Value property, depending
on if the object we’re querying holds key data or value data. So how do we
know which is which? When the contents of the dictionary are copied to the
array, the data is copied in key–value order. So the ?rst object is a key, the
second object is a value, the third object is a key, and so on.
Now we can write a code fragment that allows us to actually see the data:

for(inti=0;i<= ips.GetUpperBound(0); i++) {
Console.WriteLine(ips[index].Key);
Console.WriteLine(ips[index].Value);
}

c# for dummies: c sharp programming

c# for dummies: c sharp programming: Fundamental DictionaryBase Class Methods and Properties When working with a dictionary object, there are several operations you want to...

c sharp programming


Fundamental DictionaryBase Class
Methods and Properties
When working with a dictionary object, there are several operations you want
to perform. At aminimum, you need an Addmethod to add new data, an Item
method to retrieve a value, a Remove method to remove a key–value pair, and
a Clear method to clear the data structure of all data.
Let’s begin the discussion of implementing a dictionary by looking at a
simple example class. The following code shows the implementation of a
class that stores names and IP addresses:


public class IPAddresses : DictionaryBase {
public IPAddresses() {
}
public void Add(string name, string ip) {
base.InnerHashtable.Add(name, ip);

}
public string Item(string name) {
return base.InnerHashtable[name].ToString();
}
public void Remove(string name) {
base.InnerHashtable.Remove(name);
}
}

As you can see, these methods were very easy to build. The ?rst method
implemented is the constructor. This is a simple method that does nothing
but call the default constructor for the base class. The Add method takes a
name/IP address pair as arguments and passes them to the Add method of the
InnerHashTable object, which is instantiated in the base class.
The Item method is used to retrieve a value given a speci?c key. The key is
passed to the corresponding Item method of the InnerHashTable object. The
value that is stored with the associated key in the inner hash table is returned.
Finally, the Remove method receives a key as an argument and passes
the argument to the associated Remove method of the inner hash table. The
method then removes both the key and its associated value from the hash
table.

There are two methods we can use without implementing them: Count
and Clear. The Count method returns the number of DictionaryEntry objects
stored in the inner hash table, whereas Clear removes all the DictionaryEntry
objects from the inner hash table.
Let’s look at a program that utilizes these methods:

class chapter9 {
static void Main() {
IPAddresses myIPs = new IPAddresses();
myIPs.Add("Mike", "192.155.12.1");
myIPs.Add("David", "192.155.12.2");
myIPs.Add("Bernica", "192.155.12.3");
Console.WriteLine("There are"+ myIPs.Count +
"IP addresses");
Console.WriteLine("David's ip address: " +
myIPs.Item("David"));
myIPs.Clear();

Console.WriteLine("There are"+ myIPs.Count +
"IP addresses");
}
}

The output from this program is in the image below


One modi?cation we might want to make to the class is to overload the
constructor so that we can load data into a dictionary from a ?le. Here’s the
code for the new constructor, which you can just add into the IPAddresses
class de?nition:
public IPAddresses(string txtFile) {
string line;
string[] words;
StreamReader inFile;
inFile = File.OpenText(txtFile);
while(inFile.Peek() != -1) {
line = inFile.ReadLine();
words = line.Split(',');
this.InnerHashtable.Add(words[0], words[1]);
}
inFile.Close();
}

Now here’s a new program to test the constructor:
class chapter9 {
static void Main() {
for(inti=0;i<4; i++)
Console.WriteLine();

IPAddresses myIPs = _
new IPAddresses("c:\\data\\ips.txt");
Console.WriteLine("There are {0} IP addresses",
myIPs.Count);
Console.WriteLine("David's IP address: " +
myIPs.Item("David"));
Console.WriteLine("Bernica's IP address: " +
myIPs.Item("Bernica"));
Console.WriteLine("Mike's IP address: " +
myIPs.Item("Mike"));
}
}


c# for dummies: c sharp programming

c# for dummies: c sharp programming: Building Dictionaries: The DictionaryBase Class and the SortedList Class A dictionary is a data structure that stores data as a key–value pa...

c sharp programming

Building Dictionaries:
The DictionaryBase Class
and the SortedList Class


A dictionary is a data structure that stores data as a key–value pair. The
DictionaryBase class is used as an abstract class to implement different data
structures that all store data as key–value pairs. These data structures can be
hash tables, linked lists, or some other data structure type. In this chapter,
we examine how to create basic dictionaries and how to use the inherited
methods of the DictionaryBase class.We will use these techniques later when
we explore more specialized data structures.
One example of a dictionary-based data structure is the SortedList. This
class stores key–value pairs in sorted order based on the key. It is an interesting
data structure because you can also access the values stored in the structure
by referring to the value’s index position in the data structure, which makes
the structure behave somewhat like an array.We examine the behavior of the
SortedList class at the end of the chapter.


THE DICTIONARYBASE CLASS
You can think of a dictionary data structure as a computerizedword dictionary.
The word you are looking up is the key, and the de?nition of the word is the
value. The DictionaryBase class is an abstract (MustInherit) class that is used
as a basis for specialized dictionary implementations.
The key–value pairs stored in a dictionary are actually stored as Dictio-
naryEntry objects. The DictionaryEntry structure provides two ?elds, one for
the key and one for the value. The only two properties (or methods) we’re
interested in with this structure are the Key and Value properties. Thesemeth-
ods return the values stored when a key–value pair is entered into a dictionary.

Internally, key–value pairs are stored in a hash table object called Inner-
HashTable.
just view it as an ef?cient data structure for storing key–value pairs.
The DictionaryBase class actually implements an interface from the Sys-
tem.Collections namespace, IDictionary. This interface is actually the basis
for many of the classes we’ll study later in this book, including the ListDic-
tionary class and the Hashtable class.


c# for dummies: c sharp programming

c# for dummies: c sharp programming: IStructuralEquatable and IStructuralComparable As we said in the previous chapter, structs implement structural comparison by de- fault: two...

c sharp programming

IStructuralEquatable and IStructuralComparable
As we said in the previous chapter, structs implement structural comparison by de-
fault: two structs are equal if all of their fields are equal. Sometimes, however, struc-
tural equality and order comparison are useful as plug-in options on other types as
well—such as arrays and tuples. Framework 4.0 introduces two new interfaces to
help with this:
public interface IStructuralEquatable
{
  bool Equals (object other, IEqualityComparer comparer);
  int GetHashCode (IEqualityComparer comparer);
}
public interface IStructuralComparable
{
  int CompareTo (object other, IComparer comparer);
}
The IEqualityComparer/IComparer  that you pass  in are applied  to each  individual
element in the composite object. We can demonstrate this using arrays and tuples,
which implement these interfaces: in the following example, we compare two arrays
for equality: first using the default Equals method, and then using IStructuralEquat
able’s version:
int[] a1 = { 1, 2, 3 };
int[] a2 = { 1, 2, 3 };
Console.Write (a1.Equals (a2));                                 // False
Console.Write (a1.Equals (a2, EqualityComparer<int>.Default));  // True

Here’s another example:
string[] a1 = "the quick brown fox".Split();
string[] a2 = "THE QUICK BROWN FOX".Split();
bool isTrue = a1.Equals (a2, StringComparer.InvariantCultureIgnoreCase);
Tuples work in the same way:
var t1 = Tuple.Create (1, "foo");
var t2 = Tuple.Create (1, "FOO");
bool isTrue = t1.Equals (t2, StringComparer.InvariantCultureIgnoreCase);
int zero = t1.CompareTo (t2, StringComparer.InvariantCultureIgnoreCase);
The difference with tuples, though, is that their default equality and order compar-
ison implementations also apply structural comparisons:
var t1 = Tuple.Create (1, "FOO");
var t2 = Tuple.Create (1, "FOO");
Console.WriteLine (t1.Equals (t2));   // True

c# for dummies: c sharp programming

c# for dummies: c sharp programming: IComparer and Comparer Comparers are used to switch in custom ordering logic for sorted dictionaries and collections. Note that a comparer i...

c sharp programming

IComparer and Comparer
Comparers are used to switch in custom ordering logic for sorted dictionaries and
collections.
Note that a comparer is useless to the unsorted dictionaries such as Dictionary and
Hashtable—these  require  an  IEqualityComparer  to  get  hash  codes.  Similarly,  an
equality comparer is useless for sorted dictionaries and collections.
Here are the IComparer interface definitions:
public interface IComparer
{
  int Compare(object x, object y);
}
public interface IComparer <in T>
{
  int Compare(T x, T y);
}
As with equality comparers,  there’s an abstract class you can  subtype  instead of
implementing the interfaces:
public abstract class Comparer<T> : IComparer, IComparer<T>
{
   public static Comparer<T> Default { get; }
   public abstract int Compare (T x, T y);       // Implemented by you
   int IComparer.Compare (object x, object y);   // Implemented for you
}
The following example illustrates a class that describes a wish, and a comparer that
sorts wishes by priority:

class Wish
{
  public string Name;
  public int Priority;
  public Wish (string name, int priority)
  {
    Name = name;
    Priority = priority;
  }
}
class PriorityComparer : Comparer <Wish>
{
  public override int Compare (Wish x, Wish y)
  {
    if (object.Equals (x, y)) return 0;          // Fail-safe check
    return x.Priority.CompareTo (y.Priority);
  }
}

The object.Equals check ensures that we can never contradict the Equals method.
Calling the static object.Equals method in this case is better than calling x.Equals
because it still works if x is null!
Here’s how our PriorityComparer is used to sort a List:
var wishList = new List<Wish>();
wishList.Add (new Wish ("Peace", 2));
wishList.Add (new Wish ("Wealth", 3));
wishList.Add (new Wish ("Love", 2));
wishList.Add (new Wish ("3 more wishes", 1));
wishList.Sort (new PriorityComparer());
foreach (Wish w in wishList) Console.Write (w.Name + " | ");
// OUTPUT: 3 more wishes | Love | Peace | Wealth |
In the next example, SurnameComparer allows you to sort surname strings in an order
suitable for a phonebook listing:
class SurnameComparer : Comparer <string>
{
  string Normalize (string s)
  {
    s = s.Trim().ToUpper();
    if (s.StartsWith ("MC")) s = "MAC" + s.Substring (2);
    return s;
  }

  public override int Compare (string x, string y)
  {
    return Normalize (x).CompareTo (Normalize (y));
  }
}
Here’s SurnameComparer in use in a sorted dictionary:
var dic = new SortedDictionary<string,string> (new SurnameComparer());
dic.Add ("MacPhail", "second!");
dic.Add ("MacWilliam", "third!");
dic.Add ("McDonald", "first!");
foreach (string s in dic.Values)
  Console.Write (s + " ");              // first! second! third!

c# for dummies: c sharp programming

c# for dummies: c sharp programming: Plugging in Equality and Order A type’s default equating or comparison implementation typically reflects what is most “natural” for that typ...

c sharp programming

Plugging in Equality and Order


A type’s default equating or comparison implementation typically reflects what is
most “natural” for that type. Sometimes, however, the default behavior is not what
you want.  You might  need  a  dictionary whose  string-type  key  is  treated  case-
insensitively. Or you might want a sorted list of customers, sorted by each customer’s
postcode. For this reason, the .NET Framework also defines a matching set of “plug-
in” protocols. The plug-in protocols achieve two things:

• They allow you to switch in alternative equating or comparison behavior.
• They allow you to use a dictionary or sorted collection with a key type that’s
not intrinsically equatable or comparable.

The plug-in protocols consist of the following interfaces:
IEqualityComparer and IEqualityComparer<T>
• Performs plug-in equality comparison and hashing
• Recognized by Hashtable and Dictionary
IComparer and IComparer<T>
• Performs plug-in order comparison
• Recognized by the sorted dictionaries and collections; also, Array.Sort
Each interface comes in generic and nongeneric forms. The IEqualityComparer in-
terfaces also have a default implementation in a class called EqualityComparer.
In addition, Framework 4.0 adds two new interfaces called IStructuralEquatable
and IStructuralComparable, which allow the option of structural comparisons on
classes and arrays.


IEqualityComparer and EqualityComparer
An equality comparer switches  in nondefault equality and hashing behavior, pri-
marily for the Dictionary and Hashtable classes.

Recall  the requirements of a hashtable-based dictionary.  It needs answers  to  two
questions for any given key:
• Is it the same as another?
• What is its integer hash code?
An  equality  comparer  answers  these  questions  by  implementing  the
IEqualityComparer interfaces:
public interface IEqualityComparer<T>
{
   bool Equals (T x, T y);
   int GetHashCode (T obj);
}
public interface IEqualityComparer     // Nongeneric version
{
   bool Equals (object x, object y);
   int GetHashCode (object obj);
}
To write a custom comparer, you implement one or both of these interfaces (im-
plementing both gives maximum interoperability). As this is somewhat tedious, an
alternative is to subclass the abstract EqualityComparer class, defined as follows:

public abstract class EqualityComparer<T> : IEqualityComparer,
                                            IEqualityComparer<T>
{
  public abstract bool Equals (T x, T y);
  public abstract int GetHashCode (T obj);
  bool IEqualityComparer.Equals (object x, object y);
  int IEqualityComparer.GetHashCode (object obj);
  public static EqualityComparer<T> Default { get; }
}
EqualityComparer implements both interfaces; your job is simply to override the two
abstract methods.

The semantics for Equals and GetHashCode follow the same rules for object.Equals
and object.GetHashCode, In the following example, we define
a Customer class with two fields, and then write an equality comparer that matches
both the first and last names:
public class Customer
{
  public string LastName;
  public string FirstName;
  public Customer (string last, string first)
  {
    LastName = last;
    FirstName = first;
  }
}

public class LastFirstEqComparer : EqualityComparer <Customer>
{
  public override bool Equals (Customer x, Customer y)
  {
    return x.LastName == y.LastName && x.FirstName == y.FirstName;
  }
  public override int GetHashCode (Customer obj)
  {
    return (obj.LastName + ";" + obj.FirstName).GetHashCode();
  }
}
To illustrate how this works, we’ll create two customers:
Customer c1 = new Customer ("Bloggs", "Joe");
Customer c2 = new Customer ("Bloggs", "Joe");
Because we haven’t overridden object.Equals, normal reference  type equality se-
mantics apply:
Console.WriteLine (c1 == c2);               // False
Console.WriteLine (c1.Equals (c2));         // False
The  same  default  equality  semantics  apply  when  using  these  customers  in  a
Dictionary without specifying an equality comparer:
var d = new Dictionary<Customer, string>();
d [c1] = "Joe";
Console.WriteLine (d.ContainsKey (c2));         // False

Now with the custom equality comparer:
var eqComparer = new LastFirstEqComparer();
var d = new Dictionary<Customer, string> (eqComparer);
d [c1] = "Joe";
Console.WriteLine (d.ContainsKey (c2));         // True
In  this  example,  we  would  have  to  be  careful  not  to  change  the  customer’s
FirstName or LastName while it was in use in the dictionary. Otherwise, its hash code
would change and the Dictionary would break.


EqualityComparer<T>.Default
Calling EqualityComparer<T>.Default returns a general-purpose equality comparer
that can be used as an alternative to the static object.Equals method. The advantage
is that first checks if T implements IEquatable<T> and if so, calls that implementation
instead, avoiding the boxing overhead. This is particularly useful in generic methods:
static bool Foo<T> (T x, T y)
 {
   bool same = EqualityComparer<T>.Default.Equals (x, y);
  ...

c# for dummies: c sharp programming

c# for dummies: c sharp programming: ReadOnlyCollection ReadOnlyCollection is a wrapper, or proxy, that provides a read-only view of a collection. This is useful in allowi...

c sharp programming

ReadOnlyCollection<T>

ReadOnlyCollection<T> is a wrapper, or proxy, that provides a read-only view of a
collection. This is useful in allowing a class to publicly expose read-only access to a
collection that the class can still update internally.
A read-only collection accepts  the  input collection  in  its constructor,  to which  it
maintains a permanent reference. It doesn’t take a static copy of the input collection,
so  subsequent  changes  to  the  input  collection  are  visible  through  the  read-only
wrapper.
To illustrate, suppose your class wants to provide read-only public access to a list
of strings called Names:
public class Test
{
  public List<string> Names { get; private set; }
}

This only does half the job. Although other types cannot reassign the Names property,
they can still call Add, Remove, or Clear on the list. The ReadOnlyCollection<T> class
resolves this:
public class Test
{
  List<string> names;
  public ReadOnlyCollection<string> Names { get; private set; }
  public Test()
  {
    names = new List<string>();
    Names = new ReadOnlyCollection<string> (names);
  }
  public void AddInternally() { names.Add ("test"); }
}
Now, only members within the Test class can alter the list of names:
Test t = new Test();
Console.WriteLine (t.Names.Count);       // 0
t.AddInternally();
Console.WriteLine (t.Names.Count);       // 1

t.Names.Add ("test");                    // Compiler error
((IList<string>) t.Names).Add ("test");  // NotSupportedException

c# for dummies: c sharp programming

c# for dummies: c sharp programming: KeyedCollection and DictionaryBase KeyedCollection subclasses Collection. It both adds and sub- tracts functi...

c sharp programming

KeyedCollection<TKey,TItem> and DictionaryBase

KeyedCollection<TKey,TItem> subclasses Collection<TItem>. It both adds and sub-
tracts functionality. What it adds is the ability to access items by key, much like with
a dictionary. What it subtracts is the ability to proxy your own inner list.

A keyed collection has some resemblance to an OrderedDictionary in that it com-
bines a linear list with a hashtable. However, unlike OrderedDictionary, it doesn’t
implement IDictionary and doesn’t support the concept of a key/value pair. Keys
are  obtained  instead  from  the  items  themselves:  via  the  abstract  GetKeyForItem
method. This means  enumerating  a keyed  collection  is  just  like  enumerating  an
ordinary list.
KeyedCollection<TKey,TItem>  is  best  thought  of  as  Collection<TItem>  plus  fast
lookup by key.
Because it subclasses Collection<>, a keyed collection inherits all of Collection<>’s
functionality, except for the ability to specify an existing list in construction. The
additional members it defines are as follows:
public abstract class KeyedCollection <TKey, TItem> : Collection <TItem>
  // ...
  protected abstract TKey GetKeyForItem(TItem item);
  protected void ChangeItemKey(TItem item, TKey newKey);
  // Fast lookup by key - this is in addition to lookup by index.
  public TItem this[TKey key] { get; }
  protected IDictionary<TKey, TItem> Dictionary { get; }
}


GetKeyForItem is what the implementer overrides to obtain an item’s key from the
underlying object. The ChangeItemKey method must be called if the item’s key prop-
erty changes,  in order to update the  internal dictionary. The Dictionary property
returns the internal dictionary used to implement the lookup, which is created when
the  first  item  is  added.  This  behavior  can  be  changed  by  specifying  a  creation
threshold  in  the constructor, delaying  the  internal dictionary  from being created
until the threshold is reached (in the interim, a linear search is performed if an item
is requested by key). A good reason not to specify a creation threshold is that having
a  valid  dictionary  can  be  useful  in  obtaining  an  ICollection<>  of  keys,  via  the
Dictionary’s  Keys  property.  This  collection  can  then  be  passed  on  to  a  public
property.
The most common use for KeyedCollection<,> is in providing a collection of items
accessible both by index and by name. To demonstrate this, we’ll revisit the zoo,
this time implementing AnimalCollection as a KeyedCollection<string,Animal>:
public class Animal
{
  string name;
  public string Name
  {
    get { return name; }
    set {
      if (Zoo != null) Zoo.Animals.NotifyNameChange (this, value);
      name = value;
    }
  }


  public int Popularity;
  public Zoo Zoo { get; internal set; }
  public Animal (string name, int popularity)
  {
    Name = name; Popularity = popularity;
  }
}
public class AnimalCollection : KeyedCollection <string, Animal>
{
  Zoo zoo;
  public AnimalCollection (Zoo zoo) { this.zoo = zoo; }
  internal void NotifyNameChange (Animal a, string newName)
  {
    this.ChangeItemKey (a, newName);
  }
  protected override string GetKeyForItem (Animal item)
  {
    return item.Name;
  }
  // The following methods would be implemented as in the previous example
  protected override void InsertItem (int index, Animal item)...
  protected override void SetItem (int index, Animal item)...
  protected override void RemoveItem (int index)...
  protected override void ClearItems()...
}


public class Zoo
{
  public readonly AnimalCollection Animals;
  public Zoo() { Animals = new AnimalCollection (this); }
}
class Program
{
  static void Main()
  {
    Zoo zoo = new Zoo();
    zoo.Animals.Add (new Animal ("Kangaroo", 10));
    zoo.Animals.Add (new Animal ("Mr Sea Lion", 20));
    Console.WriteLine (zoo.Animals [0].Popularity);               // 10
    Console.WriteLine (zoo.Animals ["Mr Sea Lion"].Popularity);   // 20
    zoo.Animals ["Kangaroo"].Name = "Mr Roo";
    Console.WriteLine (zoo.Animals ["Mr Roo"].Popularity);        // 10
  }
}


DictionaryBase
The nongeneric  version of  KeyedCollection  is  called  DictionaryBase. This  legacy
class  takes  very  different  in  its  approach:  it  implements  IDictionary  and  uses

clumsy  hook  methods  like  CollectionBase:  OnInsert,  OnInsertComplete,  OnSet,
OnSetComplete, OnRemove, OnRemoveComplete, OnClear, and OnClearComplete (and ad-
ditionally, OnGet). The primary advantage of implementing IDictionary over taking
the KeyedCollection approach is that you don’t need to subclass it in order to obtain
keys. But since the very purpose of DictionaryBase is to be subclassed, it’s no ad-
vantage at all. The improved model in KeyedCollection is almost certainly due to
the  fact  that  it  was  written  some  years  later,  with  the  benefit  of  hindsight.
DictionaryBase is best considered useful for backward compatibility.

c# for dummies: c sharp programming

c# for dummies: c sharp programming: Collection and CollectionBase Collection class is a customizable wrapper for List. As well as implementing IList and IList, it d...

c sharp programming

Collection<T> and CollectionBase

Collection<T> class is a customizable wrapper for List<T>.
As well as implementing IList<T> and IList, it defines four additional virtual meth-
ods and a protected property as follows:
public class Collection<T> :
  IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
{
   // ...
   protected virtual void ClearItems();
   protected virtual void InsertItem (int index, T item);
   protected virtual void RemoveItem (int index);
   protected virtual void SetItem (int index, T item);
   protected IList<T> Items { get; }
}
The virtual methods provide the gateway by which you can “hook in” to change or
enhance the list’s normal behavior. The protected Items property allows the imple-
menter to directly access the “inner list”—this is used to make changes internally
without the virtual methods firing.
The virtual methods need not be overridden; they can be left alone until there’s a
requirement to alter the list’s default behavior. The following example demonstrates
the typical “skeleton” use of Collection<T>:

public class Animal
{
  public string Name;
  public int Popularity;
  public Animal (string name, int popularity)
  {
    Name = name; Popularity = popularity;
  }
}
public class AnimalCollection : Collection <Animal>
{
  // AnimalCollection is already a fully functioning list of animals.
  // No extra code is required.
}
public class Zoo   // The class that will expose AnimalCollection.
{                  // This would typically have additional members.
  public readonly AnimalCollection Animals = new AnimalCollection();
}
class Program
{
  static void Main()
  {
    Zoo zoo = new Zoo();
    zoo.Animals.Add (new Animal ("Kangaroo", 10));
    zoo.Animals.Add (new Animal ("Mr Sea Lion", 20));
    foreach (Animal a in zoo.Animals) Console.WriteLine (a.Name);
  }
}


As it stands, AnimalCollection is no more functional than a simple List<Animal>; its
role  is  to provide a base  for  future  extension. To  illustrate, we’ll now add a  Zoo
property to Animal, so it can reference the Zoo in which it lives and override each of
the virtual methods in Collection<Animal> to maintain that property automatically:
public class Animal
{
  public string Name;
  public int Popularity;
  public Zoo Zoo { get; internal set; }
  public Animal(string name, int popularity)
  {
    Name = name; Popularity = popularity;
  }
}
public class AnimalCollection : Collection <Animal>
{
  Zoo zoo;
  public AnimalCollection (Zoo zoo) { this.zoo = zoo; }


  protected override void InsertItem (int index, Animal item)
  {
    base.InsertItem (index, item);
    item.Zoo = zoo;
  }
  protected override void SetItem (int index, Animal item)
  {
    base.SetItem (index, item);
    item.Zoo = zoo;
  }
  protected override void RemoveItem (int index)
  {
    this [index].Zoo = null;
    base.RemoveItem (index);
  }
  protected override void ClearItems()
  {
    foreach (Animal a in this) a.Zoo = null;
    base.ClearItems();
  }
}
public class Zoo
{
  public readonly AnimalCollection Animals;
  public Zoo() { Animals = new AnimalCollection (this); }
}

Collection<T> also has a constructor accepting an existing IList<T>. Unlike with
other collection classes, the supplied list is proxied rather than copied, meaning that
subsequent  changes  will  be  reflected  in  the  wrapping  Collection<T>  (although
without Collection<T>’s virtual methods firing). Conversely, changes made via the
Collection<T> will change the underlying list.

CollectionBase
CollectionBase is the nongeneric version of Collection<T> introduced in Framework
1.0. This provides most of the same features as Collection<T> but is clumsier to use.
Instead of  the  template methods InsertItem, RemoveItem SetItem, and ClearItem,
CollectionBase has “hook” methods that double the number of methods required:
OnInsert,  OnInsertComplete,  OnSet,  OnSetComplete,  OnRemove,  OnRemoveComplete,
OnClear, and OnClearComplete. Because CollectionBase is nongeneric, you must also
implement typed methods when subclassing it—at a minimum, a typed indexer and
Add method.

c# for dummies: c sharp programming

c# for dummies: c sharp programming: Sorted Dictionaries The Framework provides two dictionary classes internally structured such that their content is always sorted by key: • S...

c sharp programming

Sorted Dictionaries
The Framework provides two dictionary classes internally structured such that their
content is always sorted by key:
• SortedDictionary<TKey,TValue>
• SortedList<TKey,TValue>*
(In this section, we will abbreviate <TKey,TValue> to <,>.)
SortedDictionary<,> uses  a  red/black  tree:  a data  structure designed  to perform
consistently well in any insertion or retrieval scenario.
SortedList<,> is implemented internally with an ordered array pair, providing fast
retrieval (via a binary-chop search) but poor insertion performance (because existing
values have to be shifted to make room for a new entry).
SortedDictionary<,>  is much  faster than SortedList<,> at  inserting elements  in a
random sequence (particularly with large lists). SortedList<,>, however, has an ex-
tra ability: to access items by index as well as by key. With a sorted list, you can go
directly  to  the nth element  in  the  sorting  sequence  (via  the  indexer on  the Keys/
Values properties). To do the same with a SortedDictionary<,>, you must manually
enumerate over n items. (Alternatively, you could write a class that combines a sorted
dictionary with a list class.)
None  of  the  three  collections  allows  duplicate  keys  (as  is  the  case  with  all
dictionaries).
The  following  example  uses  reflection  to  load  all  the  methods  defined  in
System.Object  into  a  sorted  list keyed by name,  and  then  enumerates  their keys
and values:

var sorted = new SortedList <string, MethodInfo>();
foreach (MethodInfo m in typeof (object).GetMethods())
  sorted [m.Name] = m;
foreach (string name in sorted.Keys)
  Console.WriteLine (name);
foreach (MethodInfo m in sorted.Values)
  Console.WriteLine (m.Name + " returns a " + m.ReturnType);
Here’s the result of the first enumeration:
Equals
GetHashCode
GetType
ReferenceEquals
ToString
Here’s the result of the second enumeration:
Equals returns a System.Boolean
GetHashCode returns a System.Int32
GetType returns a System.Type
ReferenceEquals returns a System.Boolean
ToString returns a System.String
Notice that we populated the dictionary through its indexer. If we instead used the
Add method, it would throw an exception because the object class upon which we’re
reflecting overloads the Equals method, and you can’t add the same key twice to a
dictionary. By using  the  indexer,  the  later entry overwrites  the earlier entry, pre-
venting this error.

You can store multiple members of the same key by making each
value element a list:
SortedList <string, List<MethodInfo>>
Extending our example, the following retrieves the MethodInfo whose key is "GetHash
Code", just as with an ordinary dictionary:
Console.WriteLine (sorted ["GetHashCode"]);      // Int32 GetHashCode()
So  far, everything we’ve done would also work with a SortedDictionary<,>. The
following two lines, however, which retrieve the last key and value, work only with
a sorted list:
Console.WriteLine (sorted.Keys  [sorted.Count - 1]);            // ToString
Console.WriteLine (sorted.Values[sorted.Count - 1].IsVirtual);  // True

c# for dummies: c sharp programming

c# for dummies: c sharp programming: OrderedDictionary An OrderedDictionary is a nongeneric dictionary that maintains elements in the same order that they were added. With an Or...

c sharp programming

OrderedDictionary
An OrderedDictionary is a nongeneric dictionary that maintains elements in the same
order that they were added. With an OrderedDictionary, you can access elements
both by index and by key.
An OrderedDictionary is not a sorted dictionary.
An OrderedDictionary  is a combination of a Hashtable and an ArrayList. This means
it has all the functionality of a Hashtable, plus functions such as RemoveAt, as well as
an integer indexer. It also exposes Keys and Values properties that return elements
in their original order.
This class was introduced in .NET 2.0, yet peculiarly, there’s no generic version.

ListDictionary and HybridDictionary
ListDictionary uses a singly linked list to store the underlying data. It doesn’t pro-
vide  sorting,  although  it  does  preserve  the  original  entry  order  of  the  items.
ListDictionary is extremely slow with large lists. Its only real “claim to fame” is its
efficiency with very small lists (fewer than 10 items).
HybridDictionary  is  a  ListDictionary  that  automatically  converts  to  a  Hashtable
upon reaching a certain size, to address ListDictionary’s problems with perform-
ance. The idea is to get a low memory footprint when the dictionary is small, and
good performance when  the dictionary  is  large. However, given  the overhead  in
converting from one to the other—and the fact that a Dictionary is not excessively
heavy  or  slow  in  either  scenario—you wouldn’t  suffer  unreasonably  by  using  a
Dictionary to begin with.
Both classes come only in nongeneric form.

c# for dummies: c sharp programming

c# for dummies: c sharp programming: IDictionary The  nongeneric  IDictionary  interface  is  the  same  in  principle  as IDictionary,  apart  from  two  important...

c sharp programming

IDictionary
The  nongeneric  IDictionary  interface  is  the  same  in  principle  as
IDictionary<TKey,TValue>,  apart  from  two  important  functional  differences.
It’s  important  to  be  aware  of  these  differences,  because  IDictionary  appears  in
legacy code (including the .NET Framework itself in places):

• Retrieving a nonexistent key via the indexer returns null (rather than throwing
an exception).
• Contains tests for membership rather than ContainsKey.
Enumerating  over  a  nongeneric  IDictionary  returns  a  sequence  of  DictionaryEn
try structs:
public struct DictionaryEntry
{
  public object Key   { get; set; }
  public object Value { get; set; }
}

Dictionary<TKey,TValue> and Hashtable
The generic Dictionary class is one of the most commonly used collections (along
with  the List<T> collection).  It uses a hashtable data  structure  to  store keys and
values, and it is fast and efficient.
The  nongeneric  version  of  Dictionary<TKey,TValue>  is
called  Hashtable;  there  is  no  nongeneric  class  called  Diction
ary. When we refer simply to Dictionary, we mean the generic
Dictionary<TKey,TValue> class.
Dictionary implements both the generic and nongeneric IDictionary interfaces, the
generic IDictionary being exposed publicly. Dictionary is, in fact, a “textbook” im-
plementation of the generic IDictionary.
Here’s how to use it:
var d = new Dictionary<string, int>();
d.Add("One", 1);
d["Two"] = 2;     // adds to dictionary because "two" not already present
d["Two"] = 22;    // updates dictionary because "two" is now present
d["Three"] = 3;
Console.WriteLine (d["Two"]);                // Prints "22"
Console.WriteLine (d.ContainsKey ("One"));   // true (fast operation)
Console.WriteLine (d.ContainsValue (3));     // true (slow operation)
int val = 0;
if (!d.TryGetValue ("onE", out val))
  Console.WriteLine ("No val");              // "No val" (case sensitive)

// Three different ways to enumerate the dictionary:
foreach (KeyValuePair<string, int> kv in d)          //  One ; 1
  Console.WriteLine (kv.Key + "; " + kv.Value);      //  Two ; 22
                                                     //  Three ; 3
foreach (string s in d.Keys) Console.Write (s);      // OneTwoThree
Console.WriteLine();
foreach (int i in d.Values) Console.Write (i);       // 1223

Its underlying hashtable works by converting each element’s key into an integer hash
code—a pseudounique value—and then applying an algorithm to convert the hash
code into a hash key. This hash key is used internally to determine which “bucket”
an entry belongs to. If the bucket contains more than one value, a linear search is
performed on the bucket. A hashtable typically starts out maintaining a 1:1 ratio of
buckets to values (a 1:1 load factor), meaning that each bucket contains only one
value. However, as more items are added to the hashtable, the load factor dynami-
cally increases, in a manner designed to optimize insertion and retrieval performance
as well as memory requirements.
A dictionary can work with keys of any type, providing it’s able to determine equality
between keys and obtain hash codes. By default, equality is determined via the key’s
object.Equals method, and the pseudounique hash code is obtained via the key’s
GetHashCode method.  This  behavior  can  be  changed,  either  by  overriding  these
methods or by providing an IEqualityComparer object when constructing the dic-
tionary. A common application of this is to specify a case-insensitive equality com-
parer when using string keys:
var d = new Dictionary<string, int> (StringComparer.OrdinalIgnoreCase);

c# for dummies: c sharp programming

c# for dummies: c sharp programming: The DictionaryEntry Structure System.Collections defines one structure type called DictionaryEntry. Non-generic collections that hold key/...

c sharp programming

The DictionaryEntry Structure
System.Collections defines one structure type called DictionaryEntry. Non-generic
collections that hold key/value pairs store those pairs in a DictionaryEntry object. This
structure defines the following two properties:
public object Key { get; set; }
public object Value { get; set; }
These properties are used to access the key or value associated with an entry. You can
construct a DictionaryEntry object by using the following constructor:
public DictionaryEntry(object k, object v)
Here, k is the key and v is the value.




A dictionary is a collection in which each element is a key/value pair. Dictionaries
are most commonly used for lookups and sorted lists.
The  Framework  defines  a  standard  protocol  for  dictionaries,  via  the  interfaces
IDictionary and IDictionary <TKey, TValue>, as well as a set of general-purpose
dictionary classes. The classes each differ in the following regard:
• Whether or not items are stored in sorted sequence
• Whether or not items can be accessed by position (index) as well as by key
• Whether generic or nongeneric
• Their performance when large



IDictionary<TKey,TValue>
IDictionary<TKey,TValue> defines the standard protocol for all key/value-based col-
lections. It extends ICollection<T> by adding methods and properties to access el-
ements based on a key of arbitrary type:
public interface IDictionary <TKey, TValue> :
  ICollection <KeyValuePair <TKey, TValue>>, IEnumerable
{
   bool ContainsKey (TKey key);
   bool TryGetValue (TKey key, out TValue value);
   void Add         (TKey key, TValue value);
   bool Remove      (TKey key);
   TValue this [TKey key]      { get; set; }  // Main indexer - by key
   ICollection <TKey> Keys     { get; }       // Returns just keys
   ICollection <TValue> Values { get; }       // Returns just values
}
To add an item to a dictionary, you either call Add or use the index’s set accessor—
the latter adds an item to the dictionary if the key is not already present (or updates
the item if it is present). Duplicate keys are forbidden in all dictionary implementa-
tions, so calling Add twice with the same key throws an exception.
To  retrieve  an  item  from  a dictionary, use  either  the  indexer or  the  TryGetValue
method. If the key doesn’t exist, the indexer throws an exception whereas TryGet
Value returns false. You can test for membership explicitly by calling ContainsKey;
however, this incurs the cost of two lookups if you then subsequently retrieve the
item.


Enumerating  directly  over  an  IDictionary<TKey,TValue>  returns  a  sequence  of
KeyValuePair structs:
public struct KeyValuePair <TKey, TValue>
{
  public TKey Key     { get; }
  public TValue Value { get; }
}
You  can  enumerate over  just  the keys or  values  via  the dictionary’s  Keys/Values
properties.
We demonstrate the use of this  interface with the generic Dictionary class  in the
following section.

Monday 26 November 2012

c# for dummies: c sharp programming

c# for dummies: c sharp programming: String equality comparison Despite ordinal’s  limitations,  string’s  == operator  always performs  ordinal  case- sensitive comparison. The...

c sharp programming

String equality comparison


Despite ordinal’s  limitations,  string’s  == operator  always performs  ordinal  case-
sensitive comparison. The same goes for the instance version of string.Equals when
called without arguments; this defines the “default” equality comparison behavior
for the string type.
The ordinal algorithm was chosen  for string’s == and Equals
functions because  it’s both highly  efficient  and  deterministic.
String  equality  comparison  is  considered  fundamental  and  is
performed far more frequently than order comparison.
A “strict” notion of equality is also consistent with the general
use of the == operator.


The following methods allow culture-aware or case-insensitive comparisons:
public bool Equals(string value, StringComparison comparisonType);
public static bool Equals (string a, string b,
                           StringComparison comparisonType);
The static version is advantageous in that it still works if one or both of the strings
are null. StringComparison is an enum defined as follows:


The static version is advantageous in that it still works if one or both of the strings
are null. StringComparison is an enum defined as follows:
public enum StringComparison
{
  CurrentCulture,               // Case-sensitive
  CurrentCultureIgnoreCase,
  InvariantCulture,             // Case-sensitive
  InvariantCultureIgnoreCase,
  Ordinal,                      // Case-sensitive
  OrdinalIgnoreCase
}


For example:
Console.WriteLine (string.Equals ("foo", "FOO",
                   StringComparison.OrdinalIgnoreCase));   // True
Console.WriteLine ("?" == "u");                            // False
Console.WriteLine (string.Equals ("?", "u",
                   StringComparison.CurrentCulture));      // ?
(The result of the final comparison is determined by the computer’s current language
settings.)

c# for dummies: StringBuilder in c#

c# for dummies: StringBuilder in c#: StringBuilder The StringBuilder class (System.Text namespace) represents a mutable (editable) string. With a StringBuilder, you can Append, ...

StringBuilder in c#

StringBuilder



The StringBuilder class (System.Text namespace) represents a mutable (editable)
string. With a StringBuilder, you can Append, Insert, Remove, and Replace substrings
without replacing the whole StringBuilder.
StringBuilder’s constructor optionally accepts an  initial string value, as well as a
starting size for its internal capacity (default is 16 characters). If you go above this,
StringBuilder  automatically  resizes  its  internal  structures  to  accommodate  (at  a
slight performance cost) up to its maximum capacity (default is int.MaxValue).
A popular use of StringBuilder  is to build up a  long string by repeatedly calling
Append. This approach is much more efficient than repeatedly concatenating ordi-
nary string types:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 50; i++) sb.Append (i + ",");
To get the final result, call ToString():
Console.WriteLine (sb.ToString());

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,
27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,

In our example, the expression i + "," means that we’re still
repeatedly concatenating strings. However,  this  incurs only a
small performance cost in that the strings in question are small
and don’t grow with each  loop  iteration. For maximum per-
formance, however, we could change the loop body to this:
{ sb.Append (i.ToString()); sb.Append (","); }
AppendLine performs an Append that adds a new line sequence ("\r\n" in Windows).
AppendFormat accepts a composite format string, just like String.Format.
As well  as  the  Insert,  Remove,  and  Replace methods  (Replace  functions  such  as
string’s Replace), StringBuilder defines a Length property and a writable indexer for
getting/setting individual characters.
To  clear  the  contents of  a  StringBuilder,  either  instantiate  a new one or  set  its
Length to zero.


Setting a StringBuilder’s Length to zero doesn’t shrink  its  in-
ternal capacity. So, if the StringBuilder previously contained 1
million characters, it will continue to occupy around 2 MB of
memory after zeroing its Length. If you want to release the mem-
ory, you must create a new StringBuilder and allow the old one
to drop out of scope (and be garbage-collected).

c# for dummies: c sharp programming

c# for dummies: c sharp programming: String order comparison String’s CompareTo instance method performs culture-sensitive, case-sensitive order comparison. Unlike the == operat...

c sharp programming

String order comparison



String’s CompareTo instance method performs culture-sensitive, case-sensitive order
comparison. Unlike the == operator, CompareTo does not use ordinal comparison: for
ordering, a culture-sensitive algorithm is much more useful.
Here’s the method’s definition:
public int CompareTo (string strB);
The  CompareTo  instance  method  implements  the  generic
IComparable  interface,  a  standard  comparison  protocol  used
across  the  .NET  Framework. This means  string’s  CompareTo
defines the default ordering behavior strings,  in such applica-
tions as sorted collections, for instance. For more information


For other kinds of comparison, you can call the static Compare and CompareOrdinal
methods:
public static int Compare (string strA, string strB,
                           StringComparison comparisonType);
public static int Compare (string strA, string strB, bool ignoreCase,
                           CultureInfo culture);
public static int Compare (string strA, string strB, bool ignoreCase);
public static int CompareOrdinal (string strA, string strB);
The last two methods are simply shortcuts for calling the first two methods.
All of the order comparison methods return a positive number, a negative number,
or zero, depending on whether the first value comes after, before, or alongside the
second value:
Console.WriteLine ("Boston".CompareTo ("Austin"));    // 1
Console.WriteLine ("Boston".CompareTo ("Boston"));    // 0
Console.WriteLine ("Boston".CompareTo ("Chicago"));   // -1
Console.WriteLine ("?".CompareTo ("u"));              // 0
Console.WriteLine ("foo".CompareTo ("FOO"));          // -1


The following performs a case-insensitive comparison using the current culture:
Console.WriteLine (string.Compare ("foo", "FOO", true));   // 0
By supplying a CultureInfo object, you can plug in any alphabet:
// CultureInfo is defined in the System.Globalization namespace
CultureInfo german = CultureInfo.GetCultureInfo ("de-DE");
int i = string.Compare ("Müller", "Muller", false, german);

c# for dummies: c sharp programming

c# for dummies: c sharp programming: Ordinal versus culture comparison There are two basic algorithms for string comparison: ordinal and culture-sensitive. Ordinal  comparisons ...

c sharp programming

Ordinal versus culture comparison



There are two basic algorithms for string comparison: ordinal and culture-sensitive.
Ordinal  comparisons  interpret  characters  simply  as numbers  (according  to  their
numeric Unicode  value);  culture-sensitive  comparisons  interpret  characters with
reference to a particular alphabet. There are two special cultures: the “current cul-
ture,” which is based on settings picked up from the computer’s control panel, and
the “invariant culture,” which  is  the  same on every computer  (and closely maps
American culture).
For equality comparison, both ordinal and culture-specific algorithms are useful.
For ordering, however, culture-specific comparison is nearly always preferable: to
order strings alphabetically, you need an alphabet. Ordinal relies on the numeric
Unicode  point  values,  which  happen  to  put  English  characters  in  alphabetical


order—but even then not exactly as you might expect. For example, assuming case-
sensitivity, consider the strings “Atom”, “atom”, and “Zamia”. The invariant culture
puts them in the following order:
"Atom", "atom", "Zamia"
Ordinal arranges them instead as follows:
"Atom", "Zamia", "atom"
This is because the invariant culture encapsulates an alphabet, which considers up-
percase characters adjacent to their lowercase counterparts (AaBbCcDd…). The or-
dinal  algorithm,  however,  puts  all  the  uppercase  characters  first,  and  then  all
lowercase characters (A..Z, a..z). This is essentially a throwback to the ASCII char-
acter set invented in the 1960s.

c# for dummies: Comparing Strings in c#

c# for dummies: Comparing Strings in c#: Comparing Strings In comparing two values, the .NET Framework differentiates the concepts of equal- ity comparison and order comparison. Equ...

Comparing Strings in c#

Comparing Strings


In comparing two values, the .NET Framework differentiates the concepts of equal-
ity comparison and order comparison. Equality comparison  tests whether  two  in-
stances  are  semantically  the  same; order  comparison  tests which of  two  (if  any)
instances comes first when arranging them in ascending or descending sequence.
Equality comparison  is not a subset of order comparison;  the
two systems have different purposes. It’s legal, for instance, to
have two unequal values in the same ordering position. We re-
sume this topic in “Equality Comparison” on page 245.
For  string  equality  comparison,  you  can  use  the  ==  operator  or  one  of  string’s
Equals methods. The  latter  are more  versatile because  they  allow  you  to  specify
options such as case-insensitivity.
Another difference is that == does not work reliably on strings
if the variables are cast to the object type.


For string order comparison, you can use either the CompareTo instance method or
the static Compare and CompareOrdinal methods: these return a positive or negative
number—or  zero—depending on whether  the  first  value  comes before,  after, or
alongside the second.
Before going into the details of each, we need to examine .NET’s underlying string
comparison algorithms.

c# for dummies: c# for dummies

c# for dummies: c# for dummies: String.Format and composite format strings The static Format method provides a convenient way  to build strings  that embed variables.  The ...

c# for dummies

String.Format and composite format strings




The static Format method provides a convenient way  to build strings  that embed
variables.  The  embedded  variables  can  be  of  any  type;  the  Format  simply  calls
ToString on them.
The master string that includes the embedded variables is called a composite format
string. When calling String.Format, you provide a composite format string followed
by each of the embedded variables. For example:
string composite = "It's {0} degrees in {1} on this {2} morning";
string s = string.Format (composite, 35, "Perth", DateTime.Now.DayOfWeek);
// s == "It's 35 degrees in Perth on this Friday morning"
(And that’s Celsius!)
Each number in curly braces is called a format item. The number corresponds to the
argument position and is optionally followed by:


• A comma and a minimum width to apply
• A colon and a format string
The minimum width is useful for aligning columns. If the value is negative, th
is left-aligned; otherwise, it’s right-aligned. For example:
string composite = "Name={0,-20} Credit Limit={1,15:C}";
Console.WriteLine (string.Format (composite, "Mary", 500));
Console.WriteLine (string.Format (composite, "Elizabeth", 20000));
Here’s the result:
Name=Mary                 Credit Limit=        $500.00
Name=Elizabeth            Credit Limit=     $20,000.00
The equivalent without using string.Format is this:
string s = "Name=" + "Mary".PadRight (20) +
           " Credit Limit=" + 500.ToString ("C").PadLeft (15);

c# for dummies: c sharp classes

c# for dummies: c sharp classes: Splitting and joining strings Split takes a sentence and returns an array of words: string[] words = "The quick brown fox".Split(); foreach ...

c sharp classes

Splitting and joining strings



Split takes a sentence and returns an array of words:
string[] words = "The quick brown fox".Split();
foreach (string word in words)
  Console.Write (word + "|");    // The|quick|brown|fox|
By default, Split uses whitespace characters as delimiters;  it’s also overloaded to
accept a params array of char or string delimiters. Split also optionally accepts a
StringSplitOptions  enum, which has an option  to  remove  empty  entries:  this  is
useful when words are separated by several delimiters in a row.



The static Join method does the reverse of Split. It requires a delimiter and string
array:
string[] words = "The quick brown fox".Split();
string together = string.Join (" ", words);      // The quick brown fox
The static Concat method is similar to Join but accepts only a params string array and
applies no separator. Concat is exactly equivalent to the + operator (the compiler, in
fact, translates + to Concat):
string sentence     = string.Concat ("The", " quick", " brown", " fox");
string sameSentence = "The" + " quick" + " brown" + " fox";

c# for dummies: c sharp classes

c# for dummies: c sharp classes:                                             Manipulating strings Because String is immutable, all the methods that “manipulate” a string ret...

c sharp classes

                                            Manipulating strings



Because String is immutable, all the methods that “manipulate” a string return a
new one,  leaving  the original untouched  (the same goes  for when you reassign a
string variable).
Substring extracts a portion of a string:
string left3 = "12345".Substring (0, 3);     // left3 = "123";
string mid3  = "12345".Substring (1, 3);     // mid3 = "234";
If you omit the length, you get the remainder of the string:
string end3  = "12345".Substring (2);        // end3 = "345";
Insert and Remove insert or remove characters at a specified position:
string s1 = "helloworld".Insert (5, ", ");    // s1 = "hello, world"
string s2 = s1.Remove (5, 2);                 // s2 = "helloworld";
PadLeft and PadRight pad a string to a given length with a specified character (or a
space if unspecified):
Console.WriteLine ("12345".PadLeft (9, '*'));  // ****12345
Console.WriteLine ("12345".PadLeft (9));       //     12345
If the input string is longer than the padding length, the original string is returned
unchanged.
TrimStart and TrimEnd remove specified characters from the beginning or end of a
string; Trim does both. By default,  these  functions  remove whitespace characters
(including spaces, tabs, new lines, and Unicode variations of these):
Console.WriteLine ("  abc \t\r\n ".Trim().Length);   // 3


Replace replaces all occurrences of a particular character or substring:
Console.WriteLine ("to be done".Replace (" ", " | ") );  // to | be | done
Console.WriteLine ("to be done".Replace (" ", "")    );  // tobedone
ToUpper and ToLower return upper- and  lowercase versions of the  input string. By
default,  they  honor  the  user’s  current  language  settings;  ToUpperInvariant  and
ToLowerInvariant always apply English alphabet rules.

c# for dummies: c# for dummies

c# for dummies: c# for dummies: using System; class StringMethods { public static void Main( string[] args )    {       string string1 = "hello there";       char[] charact...

c# for dummies

using System;
class StringMethods
{
public static void Main( string[] args )
   {
      string string1 = "hello there";
      char[] characterArray = new char[ 5 ];
      // output string1
      Console.WriteLine( "string1: \"" + string1 + "\"" );
     
     
      // loop through characters in string1 and display reversed
      Console.Write( "The string reversed is: " );
      for ( int i =   - 1; i >= 0; i-- )
         Console.Write(   );
      // copy characters from string1 into characterArray          
     
      Console.Write( "\nThe character array is: " );
      for ( int i = 0; i <  ; i++ )
         Console.Write(   );
      Console.WriteLine( "\n" );
   } // end Main
} // end class StringMethods

// test Length property                                    
Console.WriteLine( "Length of string1: " + string1.Length );
string1.Length
string1[ i ]
string1.CopyTo( 0, characterArray, 0, characterArray.Length );
characterArray.Length
characterArray[ i ]

ouput


string1: "hello there"
Length of string1: 11
The string reversed is: ereht olleh
The character array is: hello






c# for dummies: c# for dummies

c# for dummies: c# for dummies: Strings Manipulation public static void Main( string[] args )  {                    Console.WriteLine( "string1 = " + "\"" + string1 + "\"\n...

c# for dummies

Strings Manipulation



public static void Main( string[] args )
 {
  
   
   
   
    Console.WriteLine( "string1 = " + "\"" + string1 + "\"\n" +
       "string2 = " + "\"" + string2 + "\"\n" +
       "string3 = " + "\"" + string3 + "\"\n" +
       "string4 = " + "\"" + string4 + "\"\n" );
 } // end Main
// end class StringConstructor
// string initialization                            
char[] characterArray =                             
   { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };
string originalString = "Welcome to C# programming!";
string string1 = originalString;                    
string string2 = new string( characterArray );      
string string3 = new string( characterArray, 6, 3 );
string string4 = new string( 'C', 5 ); 
}


output


string1 = "Welcome to C# programming!"
string2 = "birth day"
string3 = "day"
string4 = "CCCCC"            

c# for dummies: c sharp programming

c# for dummies: c sharp programming: The System.Text.StringBuilder Type Given that the string type can be inefficient when used with reckless abandon, the .NET base class librar...

c# for dummies: c sharp programming

c# for dummies: c sharp programming: The System.Text.StringBuilder Type Given that the string type can be inefficient when used with reckless abandon, the .NET base class librar...

c sharp programming

The System.Text.StringBuilder Type


Given that the string type can be inefficient when used with reckless abandon, the .NET base class
libraries provide the System.Text namespace. Within this (relatively small) namespace lives a class
named StringBuilder. Like the System.String class, the StringBuilder defines methods that allow you
to replace or format segments, for example. When you wish to use this type in your C# code files, your
first step is to make sure the following namespace is imported into your code file:

// StringBuilder lives here!
using System.Text;

What is unique about the StringBuilder is that when you call members of this type, you are directly
modifying the object’s internal character data (making it more efficient), not obtaining a copy of the data
in a modified format. When you create an instance of the StringBuilder, you can supply the object’s
initial startup values via one of many constructors. If you are new to the topic of constructors, simply
understand that constructors allow you to create an object with an initial state when you apply the new
keyword. Consider the following usage of StringBuilder:

static void FunWithStringBuilder()
{
  Console.WriteLine("=> Using the StringBuilder:");
  StringBuilder sb = new StringBuilder("**** Fantastic Games ****");
  sb.Append("\n");
  sb.AppendLine("Half Life");
  sb.AppendLine("Beyond Good and Evil");
  sb.AppendLine("Deus Ex 2");
  sb.AppendLine("System Shock");
  Console.WriteLine(sb.ToString());

  sb.Replace("2", "Invisible War");
  Console.WriteLine(sb.ToString());
  Console.WriteLine("sb has {0} chars.", sb.Length);
  Console.WriteLine();
}

Here we have constructed a StringBuilder set to the initial value "**** Fantastic Games ****". As
you can see, we are appending to the internal buffer and are able to replace or remove characters at will.
By default, a StringBuilder is only able to initially hold a string of 16 characters or fewer (but will expand
automatically if necessary); however, this default starting value can be changed via an additional
constructor argument.

c# for dummies: c sharp programming

c# for dummies: c sharp programming: Strings Are Immutable One of the interesting aspects of System.String is that once you assign a string object with its initial value, the ...

c sharp programming

Strings Are Immutable


One of the interesting aspects of System.String is that once you assign a string object with its initial
value, the character data cannot be changed. At first glance, this might seem like a flat-out lie, given that
we are always reassigning strings to new values and because the System.String type defines a number of
methods that appear to modify the character data in one way or another (such as uppercasing and

lowercasing). However, if you look more closely at what is happening behind the scenes, you will notice
the methods of the string type are, in fact, returning you a brand-new string object in a modified
format.

static void StringsAreImmutable()
{
  // Set initial string value.
  string s1 = "This is my string.";
  Console.WriteLine("s1 = {0}", s1);

  // Uppercase s1?
  string upperString = s1.ToUpper();
  Console.WriteLine("upperString = {0}", upperString);

  // Nope! s1 is in the same format!
  Console.WriteLine("s1 = {0}", s1);
}

c# for dummies: c sharp programming

c# for dummies: c sharp programming: Strings and Equality As fully explained in Chapter 4, a reference type is an object allocated on the garbage-collected managed heap. By d...

c sharp programming

Strings and Equality



As fully explained in Chapter 4, a reference type is an object allocated on the garbage-collected managed
heap. By default, when you perform a test for equality on reference types (via the C# == and !=
operators), you will be returned true if the references are pointing to the same object in memory.
However, even though the string data type is indeed a reference type, the equality operators have been
redefined to compare the values of string objects, not the object in memory to which they refer.

static void StringEquality()
{
  Console.WriteLine("=> String equality:");
  string s1 = "Hello!";
  string s2 = "Yo!";
  Console.WriteLine("s1 = {0}", s1);
  Console.WriteLine("s2 = {0}", s2);
  Console.WriteLine();

  // Test these strings for equality.
  Console.WriteLine("s1 == s2: {0}", s1 == s2);
  Console.WriteLine("s1 == Hello!: {0}", s1 == "Hello!");
  Console.WriteLine("s1 == HELLO!: {0}", s1 == "HELLO!");
  Console.WriteLine("s1 == hello!: {0}", s1 == "hello!");
  Console.WriteLine("s1.Equals(s2): {0}", s1.Equals(s2));
  Console.WriteLine("Yo.Equals(s2): {0}", "Yo!".Equals(s2));
  Console.WriteLine();
}




The C# equality operators perform a case-sensitive, character-by-character equality test on string
objects. Therefore, "Hello!" is not equal to "HELLO!", which is different from "hello!". Also, keeping the
connection between string and System.String in mind, notice that we are able to test for equality using
the Equals() method of String as well as the baked-in equality operators. Finally, given that every string
literal (such as "Yo") is a valid System.String instance, we are able to access string-centric functionality
from a fixed sequence of characters.

c# for dummies: c sharp programming

c# for dummies: c sharp programming: String Concatenation string variables can be connected together to build larger strings via the C# + operator. As you may know, this tec...

c sharp programming

String Concatenation




string variables can be connected together to build larger strings via the C# + operator. As you may
know, this technique is formally termed string concatenation. Consider the following new helper
function:

static void StringConcatenation()
{
  Console.WriteLine("=> String concatenation:");
  string s1 = "Programming the ";
  string s2 = "PsychoDrill (PTP)";
  string s3 = s1 + s2;
  Console.WriteLine(s3);
  Console.WriteLine();
}

c# for dummies: string manipulation

c# for dummies: string manipulation:                           Basic String Manipulation Working with the members of System.String is as you would expect. Simply declare a strin...

string manipulation

                          Basic String Manipulation

Working with the members of System.String is as you would expect. Simply declare a string variable
and make use of the provided functionality via the dot operator. Be aware that a few of the members of
System.String are static members and are, therefore, called at the class (rather than the object) level.
Assume you have created a new Console Application project named FunWithStrings. Author the
following method, which should be called from within Main():

static void BasicStringFunctionality()
{
  Console.WriteLine("=> Basic String functionality:");
  string firstName = "Freddy";
  Console.WriteLine("Value of firstName: {0}", firstName);
  Console.WriteLine("firstName has {0} characters.", firstName.Length);
  Console.WriteLine("firstName in uppercase: {0}", firstName.ToUpper());
  Console.WriteLine("firstName in lowercase: {0}", firstName.ToLower());
  Console.WriteLine("firstName contains the letter y?: {0}",
    firstName.Contains("y"));
  Console.WriteLine("firstName after replace: {0}", firstName.Replace("dy", ""));
  Console.WriteLine();
}

Not too much to say here, as this method simply invokes various members, such as ToUpper() and
Contains(), on a local string variable to yield various formats and transformations.




Tuesday 13 November 2012

c# for dummies: c sharp programming

c# for dummies: c sharp programming: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace StringManipulationPost {     class Program  ...

c sharp programming

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

namespace StringManipulationPost
{
    class Program
    {
        static void Main(string[] args)
        {
          
                string[] strings = { "started", "starting", "ended", "ending" };
 // test every string to see if it starts with "st"
 for ( int i = 0; i < strings.Length; i++ )
    if (strings[ i ].StartsWith( "st" )   )
       Console.WriteLine( "\"" + strings[ i ] + "\"" +
          " starts with \"st\"" );
 Console.WriteLine();
 // test every string to see if it ends with "ed"
 for ( int i = 0; i < strings.Length; i++ )
    if (  strings[ i ].EndsWith( "ed" ) )
       Console.WriteLine( "\"" + strings[ i ] + "\"" +
          " ends with \"ed\"" );
 Console.WriteLine();
//strings[ i ].StartsWith( "st" )
//strings[ i ].EndsWith( "ed" )


              Console.ReadLine();
 
        }
    }
}