Tuesday, February 23, 2010

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

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

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)

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) 

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)