Defining a Custom C# Class Type                           
          
Let’s look at an example of a C# class type, keeping in mind that:
      •        The default access modifier of a type (class, interface, structure, etc) is internal.  Internal types can
                only be used within the defining assembly.
      •        The default access modifier for any class member (constructor, method, property, etc) is private.


// An internal class.
class Employee
{
 // 3 private fields.  
 string mName;
 private double mWage;
 double mHours;

 // The default access level of a member in a class is private!
 public double ComputePay()
 {
    // Compute pay can use these private fields because it is
    // a member of the Employee Class.
    return mWage * mHours;
 }
}



Since a class is nothing more than a custom type, you use it to declare a variable of that type.  A reference
begins life by referring to nothing (that is, null).  

Typically, you explicitly allocate the memory for an object using the
new keyword.  The new keyword
allocates the memory required for the object, sets the bits to their initial state and returns a reference to the
newly created object.  Some FCL methods also create and return objects. So the new keyword is not the only
way to create an object.


static void Main(string[] args)
{
 // Declare the reference variable
 Employee emp;

 // Create the object
 emp = new Employee();

 // Or we can declare the reference and construct the
 // object all on one line:
 Employee emp2 = new Employee();
}



Partial Class Definitions

C# also allows for the construction of ‘partial’ classes, structures or interfaces.  Simply put, this allows you
define a single class across multiple files.   At compile time, the partial class definitions are merged into a single
type that shares the same fully qualified name.


// Employee.cs
public partial class Employee
{
// Private member variables.

// Public members.
}



// Employee.Internal.cs
public
partial class Employee
{
 // Private helper function.

 // System.Object overrides.

 // Explicit interface implementations.
}


As shown in the previous example, partial classes can help you partition a class in such as way that the ‘grunge’
can be placed into a separate location while you can focus on the public interface of the type.  For example,
Visual Studio uses partial types for Windows Forms / WPF applications, to separate designer generated code.

This can also be helpful for team development.  Multiple people can author aspects of a class at once (without
having to wait for a file to be checked into version control).
Custom C# Classes and Partial Class Definitions
Table of Contents
C# Tutorial | C#.NET Tutorial | Custom Classes Tutorial

Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used
exclusively as an online learning aid.  Any attempts to copy, reproduce, or use for training is
strictly prohibited.
Courseware
Training Resources
Tutorials
Services