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

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

Tuesday, March 18, 2008

DriveInfo Properties

The DriveInfo class class models a drive and allows one to query info about the drives in the system. While writing the code to exercise this class I was tripped up by a "gotcha". Trying to query the volume label of a a CD drive when there is not a CD raises an exception. To keep the source code simple below, it was decided o check to see if the drive is ready (A CD drive will report not ready if there is there is not a CD)

// GetDrives returns an array of all the drives on the system
DriveInfo[] drives = DriveInfo.GetDrives();

Console.WriteLine("\n-------------------------------------\n");

//Please keep these two lines if you would like to use this code on your site
Console.WriteLine("DriveInfo Example");
Console.WriteLine("Created by Charles Cozad, http://www.mctscertification.blogspot.com");

Console.WriteLine("\n-------------------------------------\n");

foreach (DriveInfo drive in drives)
{
Console.WriteLine("Drive Name: {0}", drive.Name);
// We check if the drive is ready because calling some of
// the calls can throw exceptions (such as for a CD drive
// without a CD in it)
if (drive.IsReady)
{
Console.WriteLine("Drive Label: {0}", drive.VolumeLabel);
Console.WriteLine("Drive Type: {0}", drive.DriveType);
Console.WriteLine("Drive Formt: {0}", drive.DriveFormat);
Console.WriteLine("Available Free Space: {0}", drive.AvailableFreeSpace);
Console.WriteLine("Total Free Space: {0}", drive.TotalFreeSpace);
Console.WriteLine("Total Space: {0}", drive.TotalSize);
Console.WriteLine("\n+++++++++++++++++++++++++++++\n");
}
else
{
Console.WriteLine("Drive info skipped, because the drive is not ready");
}
}


Additional Resources
DriveInfo Class (Microsoft)

FileSystemInfo Properties

The FileSystemInfo Class is the base class for the FileInfo and DirectoryInfo classes. The best way to understand a particular class in the .NET Framework (and any language for that matter) is to write some code to use it.

Here is a snippet that uses many of the properties available in the FileSystemInfo class.

FileSystemInfo exampleFile = new FileInfo(@"C:\WINDOWS\System32\command.com");

Console.WriteLine("\n-------------------------------------\n");

//Please keep these two lines if you would like to use this code on your site
Console.WriteLine("FileInfo Example");
Console.WriteLine("Created by Charles Cozad, http://www.mctscertification.blogspot.com");

Console.WriteLine("\n-------------------------------------\n");

if (exampleFile.Exists)
{
Console.WriteLine("File Name: {0}", exampleFile.Name);
Console.WriteLine("File Extension: {0}", exampleFile.Extension);
Console.WriteLine("Full File Name: {0}", exampleFile.FullName);
Console.WriteLine("Creation Time: {0}", exampleFile.CreationTime);
Console.WriteLine("Last Access Time: {0}", exampleFile.LastAccessTime);
Console.WriteLine("Last Write Time: {0}", exampleFile.LastWriteTime);
Console.WriteLine("File Attributes: {0}", exampleFile.Attributes);
}
else
{
Console.WriteLine("File does not exist");
}

Additional Resources
FileSystemInfo Class (Microsoft)

Support This Site

LinkShare  Referral  Prg