Home Page
"Join my free Amazing ASP.NET newsletter"
It's safe and easy to signup. You get:
  • Notified anytime a new article comes out
  • Technical step-by-step tutorials on all important ASP.NET topics
  • Expert guidance so you can reach unlimited ASP.NET success
  • Important ASP.NET news updates
Enter your Email

Click here to download the sample source code for this lesson.

In previous lessons in this category we talked about User defined Classes and Objects.  Before we move on to additional related topics, let’s review terminology that has been covered already:

  • Class - A user defined type.  A Class is a collection of related Methods (i.e. functions or procedures) that are logically grouped together.  Classes are used to organize your application code and facilitate re-use.
  • Object - An instantiation of a Class.  In order to use a Class in C#, the Class must be instantiated by using the “new” operator.  Instantiation creates an “object of the class”.

Up to this point, we have experimented with User defined Classes, but now we will also talk about Classes that are not defined by you the programmer, but are already available in the .NET Framework Class Library.  The .NET Framework Library contains a lot of prebuilt Classes.  These Classes are provided for you by the Framework and contain many different functions that you can use in your program.  These Framework Classes are organized into a distinct set of Class Libraries (.dll files).  Each Library or set of Classes is organized into what is referred to as a Namespace in .NET.  A Namespace is a set of related Classes that are grouped together by functionality.  Here are some of the common .NET Namespaces that you can use in your applications:

  • System.Collections.Generic - Contains Classes that deal with lists and hash tables.
  • System.Configuration - Contains Classes that assist in using configuration files such as web.config
  • System.Data - Contains ADO.NET Classes that assist in interfacing with data sources like databases.  The most common data related Class in this Namespace is the DataSet class.
  • System.IO - Contains Classes that assist with reading and writing to files/streams.
  • System.Text - Containts Classes that encode/decode strings and byte blocks.

There are many more Namespaces in the Framework, just about everything from networking to security is addressed somewhere in the .NET Framework.  *Notice that the Namespace name can be composed of multiple parts, separated by a period, such as System.Collections.Generic which is a three part name.  .NET stores these Namespaces in separate .dll files; each file matches the Namespace name so for example there is a Class Library file called System.Collections.Generic.dll that contains that Namespace.  There is another file called System.Configuration.dll and so on.  Let’s add some new definitions:

  • Class Library - A .dll file that contains a group of related Classes.
  • Namespace - A logical category name for a Class.  The Namespace name usually always matches the Class Library name (e.g. the System.Text Namespace is stored in the System.Text.dll Class Library file).
  • .NET Framework Class Library - A set of pre-built Class Library files that come with .NET.

The only difference between the .NET Framework Classes and User defined Classes that you as the programmer can create yourself is that the Framework Classes are already coded by Microsoft and shipped with .NET when you install it onto your computer.  The way in which your application calls or instantiates either types of Classes is identical.  In both cases, you must use the “new” operator.  Let’s see an example where the .NET Framework library System.Text is called.  In this example, I instantiate and use the Class called StringBuilder.

System.Text.StringBuilder mybuilder = new System.Text.StringBuilder();
mybuilder.Append("This");
mybuilder.Append(" is");
mybuilder.Append(" a");
mybuilder.Append(" string builder.");
TextBox2.Text = mybuilder.ToString();

Let’s compare that with how we call a User defined Class called Arithmetic:

Arithmetic mynewclass = new Arithmetic();
int iResult = mynewclass.AddTwoNumbers(50, 20);
TextBox1.Text = iResult.ToString();

Both types of Classes are instantiated by using the “new” operator”.  You will notice that in the StringBuilder example above that I prefixed the name of the Class in the instantiation with the Namespace name System.Text.  This can be avoided by putting a special statement at the top of the file called the “using” keyword:

using System.Text;

If you put the “using” keyword at the top, you can avoid having to use the Namespace prefix in the instantiation:

StringBuilder mybuilder = new StringBuilder();
mybuilder.Append("This");
mybuilder.Append(" is");
mybuilder.Append(" a");
mybuilder.Append(" string builder.");
TextBox2.Text = mybuilder.ToString();

Now the syntax for instantiating a .NET framework Class versus a User defined Class looks identical.

Did you enjoy this article? Join my free Amazing ASP.NET newsletter.
It's safe and easy to signup. Unleash your unlimited web programming potential. You get:
  • Notified anytime a new article comes out
  • Technical step-by-step tutorials on all important ASP.NET topics
  • Expert guidance so you can reach unlimited ASP.NET success
  • Important ASP.NET news updates
Enter your Email

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment