Home

Class Abstraction

Foundations of Abstract Classes

Introduction

A class is said to be abstract if its primary role is to serve as parent for other classes. This means that another class must be derived from the abstract class. After that, the abstract class can be used somehow in an application.

Practical LearningPractical Learning: Introducing Class Abstraction

  1. Start Microsoft Visual Studio. On the Visual Studio 2019 dialog box, click Create a New Project (if Microsoft Visual Studio was already opened, on the main menu, click File -> New Project...)
  2. In the list of projects templates of the Create New Project dialog box, click Windows Forms App (.NET Framework)
  3. Click Next
  4. Replace the suggested name of the project with GasUtilityCompany2
  5. Click Create
  6. In the Solution Explorer, right-click App_Code -> Add -> Class...
  7. Change the name to BillPreparation
  8. Click Add
  9. Change the class as follows:
    namespace GasUtilityCompany2
    {
        public class BillPreparation
        {
            public int InvoiceNumber { get; set; }
            public double CounterReadingStart { get; set; }
            public double CounterReadingEnd { get; set; }
    
            public double CCFTotal
            {
                get
                {
                    return CounterReadingEnd - CounterReadingStart;
                }
            }
    
            public double TotalTherms
            {
                get
                {
                    return CCFTotal * 1.0367;
                }
            }
    
            public double DistributionAdjustment
            {
                get
                {
                    return TotalTherms * 0.13086;
                }
            }
    
            public double CalculateTransportationCharges()
            {
                if (TotalTherms <= 5000)
                    return TotalTherms * 0.016289;
                else
                    return TotalTherms * 0.009577;
            }
    
            public double CalculateDeliveryTotal()
            {
                double first50Therms = 0, over50Therms = 0;
    
                if (TotalTherms < 5000)
                {
                    first50Therms = TotalTherms * 0.05269;
                    over50Therms = 0;
                }
                else
                {
                    first50Therms = 5000 * 0.5269;
                    over50Therms = (TotalTherms - 5000) * 0.04995;
                }
    
                return CalculateTransportationCharges() + DistributionAdjustment + first50Therms + over50Therms;
            }
    
            public double EnvironmentalCharges
            {
                get
                {
                    return CalculateDeliveryTotal() * 0.0045;
                }
            }
    
            public double AmountDue
            {
                get
                {
                    return CalculateDeliveryTotal() + EnvironmentalCharges;
                }
            }
        }
    }
  10. On the main menu, click Window -> Form1.cs [Design]
  11. Design the form as follows:

    Introducing Class Abstraction

    Control (Name) Text
    Label   Invoice #:
    TextBox txtInvoiceNumber  
    Label   Counter Reading Start:
    TextBox txtCounterReadingStart  
    Label   Counter Reading End:
    TextBox txtCounterReadingEnd  
    Button btnPrepareInvoice Prepare Invoice
    Label   CCF Total:
    TextBox txtCCFTotal
    Label   Total Therms:
    TextBox txtTotalTherms
    Label   Ditribution Adjustment:
    TextBox txtDitributionAdjustment
    Label   Transportation Charges:
    TextBox txtTransportationCharges
    Label   ______________
    Label   Total Delivery:
    TextBox txtTotalDelivery
    Label   Environment Charges:
    TextBox txtEnvironmentCharges
    Label   ______________
    Label   Amount Due:
    TextBox txtAmountDue
  12. Double-click the Prepare Invoice button
  13. Change the document as follows:
    using System;
    using System.Windows.Forms;
    
    namespace GasUtilityCompany2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnPrepareInvoice_Click(object sender, EventArgs e)
            {
                BillPreparation bp = new BillPreparation();
                
                bp.InvoiceNumber = int.Parse(txtInvoiceNumber.Text);
                bp.CounterReadingStart = double.Parse(txtCounterReadingStart.Text);
                bp.CounterReadingEnd = double.Parse(txtCounterReadingEnd.Text);
    
                txtCCFTotal.Text = bp.CCFTotal.ToString("F");
                txtTotalTherms.Text = bp.TotalTherms.ToString("F");
                txtDitributionAdjustment.Text = bp.DistributionAdjustment.ToString("F");
                txtTransportationCharges.Text = bp.CalculateTransportationCharges().ToString("F");
                txtTotalDelivery.Text = bp.CalculateDeliveryTotal().ToString("F");
                txtEnvironmentCharges.Text = bp.EnvironmentalCharges.ToString("F");
                txtAmountDue.Text = bp.AmountDue.ToString("F");
            }
        }
    }
  14. To execute the application to test it, on the main menu, click Debug -> Start Without Debugging:

    Creating and Using an Abstract Class

  15. In the Invoice # text box, type a random number such as 100001
  16. In the Counter Reading Start text box, enter a number such as 214485
  17. In the Counter Reading End text box, enter a number greater than that of the Counter Start, such as 216079:

    Creating and Using an Abstract Class

  18. Click the Calculate button:

    Creating and Using an Abstract Class

  19. Close the form and return to your programming environment
  20. In the Solution Explorer, right-click App_Code -> Add -> Class...
  21. Change the name to CustomerInvoice
  22. Click Add:
    namespace GasUtilityCompany2
    {
        public class CustomerInvoice
        {
        }
    }

Creating an Abstract Class

To let you create an abstract class, the C# language provides the abstract keyword. Therefore, to create an abstract class, type this keyword to the left of the class keyword. Here is an example:

abstract class Triangle
{
}

If you decide to apply an access level (public or internal) on the class creation, the abstract keyword can appear before or after the access level.

In the class, you can add any type of member, like any of the types of constructors, methods, or properties we have used so far.

Practical LearningPractical Learning: Creating an Abstract Class

  1. Click the BillPreparation.cs tab to accesss the file
  2. Between the public and the class keywords, type the abstract keyword (the other changes are optional):
    namespace GasUtilityCompany2
    {
        public abstract class BillPreparation
        {
            public int InvoiceNumber          { get; set; }
            public double CounterReadingStart { get; set; }
            public double CounterReadingEnd   { get; set; }
    
            public double CCFTotal
            {
                get => CounterReadingEnd - CounterReadingStart;
            }
    
            public double TotalTherms
            {
                get => CCFTotal * 1.0367;
            }
    
            public double DistributionAdjustment
            {
                get => TotalTherms * 0.13086;
            }
    
            public double CalculateTransportationCharges()
            {
                if (TotalTherms <= 5000)
                    return TotalTherms * 0.016289;
                else
                    return TotalTherms * 0.009577;
            }
    
            public double CalculateDeliveryTotal()
            {
                double first50Therms = 0, over50Therms = 0;
    
                if (TotalTherms < 5000)
                {
                    first50Therms = TotalTherms * 0.05269;
                    over50Therms = 0;
                }
                else
                {
                    first50Therms = 5000 * 0.5269;
                    over50Therms = (TotalTherms - 5000) * 0.04995;
                }
    
                return CalculateTransportationCharges() + DistributionAdjustment + first50Therms + over50Therms;
            }
    
            public double EnvironmentalCharges
            {
                get => CalculateDeliveryTotal() * 0.0045;
            }
    
            public double AmountDue
            {
                get => CalculateDeliveryTotal() + EnvironmentalCharges;
            }
        }
    }
  3. To execute, on the main menu, click Debug -> Start Without Debugging
  4. Notice that you recieve an error:

    Modeling a Glass of Wine

    Read the error on the message box and click No

Deriving from an Abstract Class

An abstract class cannot be directly used like any of the classes we have used so far. This means that you cannot use a constructor of an abstract class to instantiate an object. You have various solutions to solve this problem. The first thing you must do is to create a class derived from the abstract class. You can then create an object of that class.

Practical LearningPractical Learning: Deriving from an Abstract Class

  1. Click the CustomerInvoice tab to access the other class
  2. Derive the CustomerInvoice class from the BillPreparation class as follows:
    namespace GasUtilityCompany2
    {
        public class CustomerInvoice : BillPreparation
        {
        }
    }

An Object of an Abstract Class

Although you must always derive a class from an abstract class in order to make the abstract class useful, you can declare a variable of an abstract class. Such a variable must be initialized with a constructor of a class derived from the abstract class.

Practical LearningPractical Learning: Deriving from an Abstract Class

  1. Click the Form1.cs tab to access the document of the form
  2. Change the declaration of the class variable as follows (the other changes int the Form1() constructor are optional):
    using System;
    using System.Windows.Forms;
    
    namespace GasUtilityCompany2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                Random rndNumber = new Random();
                txtInvoiceNumber.Text = rndNumber.Next(100001, 999999).ToString();
                txtCounterReadingStart.Text = rndNumber.Next(2126, 2880).ToString();
                txtCounterReadingEnd.Text = rndNumber.Next(2880, 3306).ToString();
            }
    
            private void btnPrepareInvoice_Click(object sender, EventArgs e)
            {
                BillPreparation bp = new CustomerInvoice();
                
                bp.InvoiceNumber = int.Parse(txtInvoiceNumber.Text);
                bp.CounterReadingStart = double.Parse(txtCounterReadingStart.Text);
                bp.CounterReadingEnd = double.Parse(txtCounterReadingEnd.Text);
    
                txtCCFTotal.Text = bp.CCFTotal.ToString("F");
                txtTotalTherms.Text = bp.TotalTherms.ToString("F");
                txtDitributionAdjustment.Text = bp.DistributionAdjustment.ToString("F");
                txtTransportationCharges.Text = bp.CalculateTransportationCharges().ToString("F");
                txtTotalDelivery.Text = bp.CalculateDeliveryTotal().ToString("F");
                txtEnvironmentCharges.Text = bp.EnvironmentalCharges.ToString("F");
                txtAmountDue.Text = bp.AmountDue.ToString("F");
            }
        }
    }
  3. To execute the application to test it, on the main menu, click Debug -> Start Without Debugging. Here is an example:

    Creating and Using an Abstract Class

  4. Click the Calculate button:

    Creating and Using an Abstract Class

  5. Close the form and return to your programming environment

A Parameter of an Abstract Class

As done for any class, you can create a method that uses a parameter of an abstract class. In the body of the method, use the parameter like any other. For example, you can access the members of the abstract class.

When calling a method that uses a parameter of an abstract class, the argument you pass must hold an appropriate value, which should be an object of a class derived from the abstract class.

Practical LearningPractical Learning: Passing an Object of an Abstract Type

  1. Click the InvoicePreparation.cs tab to access the file
  2. Create and call a method as follows:
    using System;
    using System.Windows.Forms;
    
    namespace GasUtilityCompany2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                Random rndNumber = new Random();
                txtInvoiceNumber.Text = rndNumber.Next(100001, 999999).ToString();
                txtCounterReadingStart.Text = rndNumber.Next(2126, 2880).ToString();
                txtCounterReadingEnd.Text = rndNumber.Next(2880, 3393).ToString();
            }
    
            void PresentInvoice(BillPreparation values)
            {
                txtCCFTotal.Text              = values.CCFTotal.ToString("F");
                txtTotalTherms.Text           = values.TotalTherms.ToString("F");
                txtDitributionAdjustment.Text = values.DistributionAdjustment.ToString("F");
                txtTransportationCharges.Text = values.CalculateTransportationCharges().ToString("F");
                txtTotalDelivery.Text         = values.CalculateDeliveryTotal().ToString("F");
                txtEnvironmentCharges.Text    = values.EnvironmentalCharges.ToString("F");
                txtAmountDue.Text             = values.AmountDue.ToString("F");
            }
    
            private void btnPrepareInvoice_Click(object sender, EventArgs e)
            {
                BillPreparation bp = new CustomerInvoice();
                
                bp.InvoiceNumber = int.Parse(txtInvoiceNumber.Text);
                bp.CounterReadingStart = double.Parse(txtCounterReadingStart.Text);
                bp.CounterReadingEnd = double.Parse(txtCounterReadingEnd.Text);
    
                PresentInvoice(bp);
            }
        }
    }
  3. To execute the application to test the form, on the main menu, click Debug -> Start Without Debugging
  4. In the Counter Start text box, enter a number such as 585702
  5. In the Counter End text box, enter a number greater than that of the Counter Start, such as 586227
  6. Click the Calculate button:

    Creating and Using an Abstract Class

  7. Close the form and return to your programming environment

Returning an Object of Abstract Type

You can create a method that returns an object of an abstract class type. For such a method, make sure it returns an object of that type. Outside the class, when calling the method, you can assign it to a variable of the abstract class.

Practical LearningPractical Learning: Creating an Object of Abstract Type

  1. Access the InvoicePreparation.cs file and change it as follows:
    using System;
    using System.Windows.Forms;
    
    namespace GasUtilityCompany2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                Random rndNumber = new Random();
                txtInvoiceNumber.Text = rndNumber.Next(100001, 999999).ToString();
                txtCounterReadingStart.Text = rndNumber.Next(2126, 2880).ToString();
                txtCounterReadingEnd.Text = rndNumber.Next(2880, 3393).ToString();
            }
    
            void PresentInvoice(BillPreparation values)
            {
                txtCCFTotal.Text              = values.CCFTotal.ToString("F");
                txtTotalTherms.Text           = values.TotalTherms.ToString("F");
                txtDitributionAdjustment.Text = values.DistributionAdjustment.ToString("F");
                txtTransportationCharges.Text = values.CalculateTransportationCharges().ToString("F");
                txtTotalDelivery.Text         = values.CalculateDeliveryTotal().ToString("F");
                txtEnvironmentCharges.Text    = values.EnvironmentalCharges.ToString("F");
                txtAmountDue.Text             = values.AmountDue.ToString("F");
            }
    
            BillPreparation PrepareInvoice()
            {
                BillPreparation preparation = new CustomerInvoice();
    
                preparation.InvoiceNumber = int.Parse(txtInvoiceNumber.Text);
                preparation.CounterReadingStart = double.Parse(txtCounterReadingStart.Text);
                preparation.CounterReadingEnd = double.Parse(txtCounterReadingEnd.Text);
    
                return preparation;
            }
    
            private void btnPrepareInvoice_Click(object sender, EventArgs e)
            {
                BillPreparation bp = PrepareInvoice();
    
                PresentInvoice(bp);
            }
        }
    }
  2. To execute the application, press Ctrl + F5
  3. In the Counter Start text box, enter a number such as 1874
  4. In the Counter End text box, enter a number greater than that of the Counter Start, such as 3139
  5. Click the Calculate button:

    Creating and Using an Abstract Class

  6. Close the form and return to your programming environment
  7. On the main menu of Microsoft Visual Studio, click File -> New -> Project...
  8. In the list of projects templates, click Windows Forms App (.NET Framework)
  9. Click Next
  10. Replace the suggested name of the project with Geometry18
  11. Click Create
  12. On the main menu, click Project -> Add Class...
  13. Set the name of the class as Triangle
  14. Click Add
  15. Change the class as follows:
    namespace Geometry18
    {
        public abstract class Triangle
        {
            public double Angle;
            public double Base { get; set; }
            public virtual double Height { get; set; }
    
            public double Area
            {
                get
                {
                    return Base * Height / 2.00;
                }
            }
        }
    }
  16. On the main menu, click Project -> Add Class...
  17. Change the name of the class to Equilateral
  18. Click Add
  19. Change the class as follows:
    using System;
    
    namespace Geometry18
    {
        public class Equilateral : Triangle
        {
            public Equilateral()
            {
                Angle = 60;
            }
    
    		  public double Side
            {
                get
                {
                    return Base;
                }
            }
        }
    }
  20. Click the Triangle.cs tab to return to the first class

The Members of an Abstract Class

Introduction

Like a normal class, an abstract class can contain virtual methods and properties. As seen for non-abstract classes, if you create a virtual member in an abstract class, an object of the derived class can directly use that member. On the other hand, if you want the derived class to provide a different version of the virtual member, you must override it in the child class. This is done exactly as seen for non-abstract classes.

An Abstract Method

A method is said to be abstract if its class doesn't implement that method but the derived class must implement it. An abstract method can only belong to an abstract class.

To create an abstract method, apply the following rules:

After creating an abstract method, every class that derives from that class must provide an implementation of (each of) the abstract method(s). To implement the method, you must apply the override keyword to it as we reviewed for virtual methods.

Remember that an abstract method must not have a body. If you want the method to have a body, you can mark it with the virtual keyword.

Practical LearningPractical Learning: Creating an Abstract Method

  1. In the Triangle.cs file, in the class, create an abstract method as follows:
    namespace Geometry18
    {
        public abstract class Triangle
        {
            public double Angle;
            public double Base { get; set; }
            public virtual double Height { get; set; }
            
            public double Area
            {
                get
                {
                    return Base * Height / 2.00;
                }
            }
    
            public abstract double CalculatePerimeter();
        }
    }
  2. Click the Equilateral.cs tab to access the second class
  3. Override the abstract method as follows:
    using System;
    
    namespace Geometry18
    {
        public class Equilateral : Triangle
        {
            public Equilateral()
            {
                Angle = 60;
            }
    
    		  public double Side
            {
                get
                {
                    return Base;
                }
            }
    
            public override double CalculatePerimeter()
            {
                return Side * 3.00;
            }
        }
    }
  4. Click the Triangle.cs tab

An Abstract Property

A property is abstract if its class doesn't include an implementation of that property. Any class based on the abstract class must implement the abstract property using the override keyword.

If you want to create an abstract property that has only a get accessor, the property must use the abstract keyword. Here is an example:

public abstract class GeometricFigure
{
    public abstract double Perimeter { get; }
}

If the property has both a get and a set accessors, it can use either the abstract or the virtual keyword. Here are examples:

public abstract class GeometricFigure
{
    public abstract double Median { get; set;  }
    public virtual double Perimeter { get; set;  }
}

Practical LearningPractical Learning: Creating an Abstract Property

  1. Change the Height property as follows:
    namespace Geometry15
    {
        public abstract class Triangle
        {
            public double Base { get; set; }
            public abstract double Height { get; set; }
    
            public double Area
            {
                get
                {
                    return Base * Height / 2.00;
                }
            }
    
            public abstract double CalculatePerimeter();
        }
    }
  2. Click the Equilateral.cs tab
  3. Override the abstract property as follows:
    using System;
    
    using System;
    
    namespace Geometry15
    {
        public class Equilateral : Triangle
        {
            const double Angle = 60;
    
            private double hgt;
    
            public double Side
            {
                get
                {
                    return Base;
                }
            }
    
            public override double Height
            {
                get
                {
                    return Math.Sqrt(3.00) * Side / 2.00;
                }
    
                set
                {
                    hgt = value;
                }
            }
    
            public override double CalculatePerimeter()
            {
                return Side * 3.00;
            }
        }
    }
  4. Save the following picture somewhere on your computer and return to your programming environment:

    Geometry - Equilateral Triangle

  5. In the Solution Explorer, double-click Form1.cs
  6. Complete the design of the form as follows:

    Creating an Abstract Property

    Control (Name) Text Other properties
    PictureBox     Image: Equilateral
    Label   Equilateral Triangle Font: Times New Roman, 24pt, style=Bold
    Label   __________________  
    Label   Side:  
    TextBox txtSide   TextAlign: Right
    Button btnCalculate Calculate  
    Label   Angle:  
    TextBox txtAngle   TextAlign: Right
    Label   Degrees:  
    Label   Perimeter:  
    TextBox txtPerimeter   TextAlign: Right
    Label   Area:  
    TextBox txtArea   TextAlign: Right
  7. Doble-click the Calculate button
  8. Change the document as follows:
    using System;
    using System.Windows.Forms;
    
    namespace Geometry18
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            (string, string) CreateMessage()
            {
                return ("You must specify the length of each side of the triangle.", "Geometry - Equilateral Triangle");
            }
    
            private void btnCalculate_Click(object sender, EventArgs e)
            {
                Triangle tri = new Equilateral();
    
                try
                {
                    tri.Base = double.Parse(txtSide.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show(CreateMessage().Item1, CreateMessage().Item2);
                }
    
                txtAngle.Text = tri.Angle.ToString(); ;
                txtPerimeter.Text = tri.CalculatePerimeter().ToString();
                txtArea.Text = tri.Area.ToString();
            }
        }
    }
  9. To execute the application, on the main menu, click Debug -> Start Without Debugging:

    Introducing Operators and Operands

  10. In the top text box, type a number such as 997.36

    Introducing Operators and Operands

  11. Click the button:

    Introducing Operators and Operands

  12. Close the DOS window and return to your programming environment

Having Many Methods that Return the Same Type of Abstract Object

Consider an abstract class as follows::

public abstract class Triangle
{
    public double Angle1;
    public double Angle2;
    public double Side1;
}

Imagine you have a method that returns an object of this abstract class. Here is an example:

public abstract class Triangle
{
    public double Angle1;
    public double Angle2;
    public double Side1;
}

public class Oblique : Triangle
{
    public Triangle View()
    {
        Triangle ot = new Oblique();

        return ot;
    }
}

If you need to have one or more other methods that perform the same type(s) of operation(s) and/or return the same type of object, you don't need to declare a variable to call the first method. Here are examples:

public abstract class Triangle
{
    public double Angle1;
    public double Angle2;
    public double Side1;
}

public class Oblique : Triangle
{
    public Triangle View()
    {
        Triangle ot = new Oblique();

        return ot;
    }

    public Triangle Calculate()
    {
        return View();
    }

    public Triangle Evaluate()
    {
        return View();
    }

    public Triangle Summarize()
    {
        return View();
    }
}

Built-In Abstract Classes: The Type Class

Introduction

When studying conditional statements, we saw how to check the value held by a variable. In some cases, when you have a variable, you may need to identify the name of the class of that variable. To assist you with this, the .NET Framework provides an abstract class named Type. The Type class is created in the System namespace. Because Type is an abstract class, you cannot declare a variable of it.

The Type of a Class

To assist you with finding out the data type or the class of a variable, the C# language provides an operator named typeof. Its formula is:

Type type = typeof(data-type | class-name);

typeof is a unary operator. That is, it acts on one operand. The operand can be a known primitive data type or a class. If you pass a C# data type (such as double, int, or decimal with which we are familiar already) as the operand, the typeof operator produces the equivalent .NET Framework type. In reality, the typeof operator produces the Type name of its operand.

The Type GetType() method can also return the data type of the variable that called it. If the variable that calls this method is of a primitive C# data type, this method returns its .NET Framework equivalent. Here is an example of calling this method:

using System;

public class Program
{
    public static void Main()
    {
        Type tp = typeof(int);

        Console.Write("The type of the int is ");
        Console.WriteLine(tp);
        Console.WriteLine("=========================================");

        return;
    }
}

This would produce:

The type of the int is System.Int32
=========================================
Press any key to continue . . .

The Type of an Object

You will usually need the services of the Type class to know the data type of a variable or of an argument to a method. To make this possible, the Object class is equipped with a method named GetType. Its syntax is:

public Type GetType();

This method takes no argument and returns the name of the class. Here is an example:

using System;

public class Program
{
    public static void Main()
    {
        Square sqr = new Square(1.00);
        Type tp = sqr.GetType();

        Console.Write("The type of the object is ");
        Console.WriteLine(tp);
        
        Console.WriteLine("=============================");

        return;
    }
}

public class Square
{
    public Square(double side)
    {
        Side = side;
    }

    public double Side { get; set; }

    public double Area
    {
        get { return Side * Side; }
    }
}

This would produce:

The type of the object is Square
=============================
Press any key to continue . . .

The Base Type of a Class

The Type.GetType() method gives you the name of the class of a variable. If the class is derived from another class and you want to know the name of its parent class, the Type class can help you, using a property named BaseType. Here is an example of accessing it:

using System;

public class Program
{
    public static void Main()
    {
        var rect = new Rectangle(1.00, 2.00);
        Type tp = rect.GetType();

        Console.Write("The base type of the rectangle is ");
        Console.WriteLine(tp.BaseType);
        
        Console.WriteLine("=============================");

        return;
    }
}

public class Square
{
    public Square(double side)
    {
        Side = side;
    }

    public double Side { get; set; }

    public double Area
    {
        get { return Side * Side; }
    }
}

public class Rectangle : Square
{
    public Rectangle(double width, double height)
         : base(width)
    {
        Height = height;
    }

    public double Height { get; set; }

    public double Area
    {
        get
        {
            return Side * Height;
        }
    }
}

This would produce:

The base type of the rectangle is Square
=============================
Press any key to continue . . .

The Assembly a Type Belongs To

If a class is defined in an assembly, you may want to know what that assembly is. To assist you with getting this information, the Type class is equipped with a property named Assembly.

When accessing the Type.Assembly property, make sure the class of the variable you are using has a formal assembly. Otherwise you would receive an error.

The Full Name of a Type

To get the complete name of the class of a variable without the assembly, use the Type.FullName property.

The Namespace of a Type

Besides the assembly in which the class of a variable exists, you may want to know the namespace in which that class is defined. To let you get this information, the Type class is equipped with the Namespace property.

Finding Out Whether a Type is a Class or an Abstract

Sometimes when using a type, you may want to know whether it is a simple regular class, an abstract class, a structure, an enumeration, or another type we haven't studied yet (interface, generic, etc). To assist you with checking this, the Type class provides various Boolean properties such as IsAbstract or IsClass.

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2002-2021, FunctionX Next