Covers topics on the Microsoft Certification Exam for the .NET Framework (Exam 70-536, Microsoft .NET Framework - Application Development Foundation)

Monday, April 14, 2008

How can you sort a list of strings in the .NET Framework?

The .NET Framework provides a simple efficient mechanism to sort a list of strings. The ArrayList class contains a Sort() method that allows the items in the ArrayList to be sorted in ascending order.

ArrayList Sort Example
using System;
using System.Collections;

// This is an example of using the .NET Framework and the
// Sort() method of the ArrayList class
namespace CollectionSortExample
{
class Program
{
static void Main(string[] args)
{
//Let's fill the ArrayList with some strings
ArrayList exampleList = new ArrayList();
exampleList.Add("C# Video Game Programming");
exampleList.Add("Really Thick Programming Book");
exampleList.Add("1984");
exampleList.Add("Alice in Wonderland");

//Print the list before it is sorted
Console.WriteLine("The unsorted list looks like:");
foreach (string item in exampleList)
{
Console.WriteLine("- {0}", item);
}

//Now we can do some "magic" and sort the
// ArrayList by caling the Sort() method.
exampleList.Sort();

//Print the list after it is sorted
Console.WriteLine("\n\nThe sorted list looks like:");
foreach (string item in exampleList)
{
Console.WriteLine("- {0}", item);
}
}
}
}

Output of ArrayList Sort Example

The unsorted list looks like:
- C# Video Game Programming
- Really Thick Programming Book
- 1984
- Alice in Wonderland

The sorted list looks like:
- 1984
- Alice in Wonderland
- C# Video Game Programming
- Really Thick Programming Book
Additional Resources
ArrayList Class (Microsoft)

Tuesday, April 8, 2008

What attribute should you add to a class to allow it to be serialized?

A class can be serialized when it has the "Serializable" attribute. Serialization is the process of storing the contents of an object to long term storage. If you will only be using .NET Framework based applications to process your serialized objects, then the BinaryFormatter class will be the most efficient. If you will be transmitting your serialized object across a network or sharing it with a non .NET Application, then the SoapFormatter class should be used to export the object to the XML based SOAP format.

Serialization Example
Note that you need to add a reference to System.Runtime.Serialization.Formatters.Soap.dll to your project before compiling the code below. Unlike the BinaryFormatter class, the SoapFormatter class is not included by default.


using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

namespace SerializationExample
{
[Serializable]
class ObjectToSerialize
{
string sName;
int iCount;
double dPrice;

public ObjectToSerialize(string newName, int newCount, double newPrice)
{
sName = newName;
iCount = newCount;
dPrice = newPrice;
}
}

class Program
{
static void Main(string[] args)
{
ObjectToSerialize o = new ObjectToSerialize("Video Game", 1, 50.00);

// Create the files to save the data to
FileStream binary_fs = new FileStream("Serialization_Example.bin", FileMode.Create);
FileStream xml_fs = new FileStream("Serialization_Example.xml", FileMode.Create);

// Create a BinaryFormatter object to perform the serialization
BinaryFormatter bf = new BinaryFormatter();
// Create a SoapFormatter object to perform serialization
SoapFormatter sf = new SoapFormatter();

// Use the BinaryFormatter object to serialize the data to the file
bf.Serialize(binary_fs, o);
// Use the SoapFormatter object to serialize the data to the file
sf.Serialize(xml_fs, o);

// Close the files
binary_fs.Close();
xml_fs.Close();
}
}
}


Binary Results
SOAP Results
The XML results of the SOAP Serialization can be found here: SOAP Serialization Results

Additional Resources
BinaryFormatter Class (Microsoft)
SoapFormatter Class (Microsoft)

Support This Site

LinkShare  Referral  Prg