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)

Tuesday, March 25, 2008

What type of return types can Main have?

In C#, Main can return a void type and an int type. Returning a void type is perhaps the most commonly used return type for main, but returning an integer value from main is also useful when other applications will be running your application, such as being run by a batch file. The integer value returned from an int main method will be stored in the environment variable %ERRORLEVEL%

Additional Resources
Main() Return Values in C# (Microsoft)

Which access modifiers can be applied to a class in the .NET Framework?

The following access modifiers can be applied to classes. These modifiers determine which classes in the object hierarchy can use the class.
  • public The class can be accessed from any assembly
  • private The class can only be referenced from within the same class
  • protected The class can be accessed from the same class or any descendant of the class
  • internal The class can be accessed from any class within the same assembly
Additional Resources
C# Class Access Modifiers (Microsoft)
VB.NET Access Modifiers (Microsoft)

Monday, March 24, 2008

How can you prevent your class from being inherited by another class in the .NET framework?

You can protect your class from being inherited by another class by using the "sealed" modifier on your class. If any class tries to inherit from your sealed class, they will get an error. Why would anyone want to seal a class? Classes are sealed when you would like to prevent static members from being changed. So if you have a class that define a value for BLACK, you can use sealed to prevent other classes from changing (intentionally or unintentionally) changing BLACK to some other value other than the color of black.

The code below will not compile and will have have the following error:
'SealedExample.SealedErrorClass': cannot derive from sealed type 'SealedExample.ExampleClass'

Sealed Class Example

namespace SealedExample
{
sealed class ExampleClass
{
public void MethodToInherit()
{
Console.WriteLine("This class is sealed so you can't inherit this method");
Console.WriteLine("Even though the method is public");
}
}

class SealedErrorClass : ExampleClass
{
//This does not work because ExampleClass is sealed
}

}

Additional Resources
Using Sealed Classes in .NET (C# Corner)
Sealed Keyword (Microsoft)

Saturday, March 22, 2008

Which classes can be used to compress streams in the .NET Framework?

Stream compression is used to save space and save bandwidth. There are two primary classes used for compression/decompression, the GZipStream and the DeflateStream classes.

The first thing one might like to know is, which compression class to use? That depends on how you would like to use the data. If you will be writing the stream to a file, the GZipStream class includes additional headers that make it suitable for opening with the gzip tool. The DeflateStream class does not include headers and thus has a slightly smaller size at the expense of being less portable (which may not be a concern if the data never leaves the computer that is running the application)

Both compresson methods are based on industry standard algorithms that are free of patent protecton. This allows you to incorporate them into your application with out worrying about intellectual property concerns.

In later posts we will explore source code for compressing and decompressing streams, but it is important to note the compression size limits. (Which will be repeated each time compression is discussed)

GZipStream and DeflateStream classes can't compress files larger than 4GB


Additional Resources
System.IO.Compression.GZipStream
System.IO.Compression.DeflateStream
Using GZIP for compression in .NET

Thursday, March 20, 2008

Name some classes in the .NET Framework that derive from the Stream Class

The following classes all have the Stream class as a base class.
  • FileStream
  • MemoryStream
  • CryptoStream
  • NetworkStream
  • GZipStream
By having a common base class, one is guaranteed that a certain set of properties and methods are available. So in this case, if your code only uses the methods of the stream class, then you can work with data from ANY stream data source.

Additional Resources
System.IO.FileStream
System.IO.MemoryStream
System.Security.Cryptography.CryptoStream
System.Net.Sockets.NetworkStream
System.IO.Compression.GZipStream

Support This Site

LinkShare  Referral  Prg