Classes and Objects

This section deals with questions related to object-oriented development in C#.

[Main index]

Is C# an object-oriented language?

Yes. C# provides language support for the so-called "Three Pillars of Object Oriented Programming": encapsulation, polymorphism, and inheritance (or reusability).

(Contributed by Paul Parks)

[Section index] [Main index]

Does C# support multiple inheritance?

No. A class may only inherit a single base implementation.

C# does support implementation of multiple interfaces, which when combined with object aggregation can provide the same effect as multiple implementation inheritance.

interface ITestOne
{
    void MethodOne();
}

interface ITestTwo
{
    void MethodTwo();
}

class TestOne : ITestOne
{
    public virtual void MethodOne()
    {
        System.Console.WriteLine("Hello from TestOne.MethodOne");
    }
}

class TestTwo : ITestTwo
{
    public virtual void MethodTwo()
    {
        System.Console.WriteLine("Hello from TestTwo.MethodTwo");
    }
}

class TestThree : ITestOne, ITestTwo
{
    TestOne testOneImplementation = new TestOne();
    TestTwo testTwoImplementation = new TestTwo();

    public virtual void MethodOne()
    {
        testOneImplementation.MethodOne();
    }

    public virtual void MethodTwo()
    {
        testTwoImplementation.MethodTwo();
    }
}
(Contributed by Paul Parks)

[Section index] [Main index]

Does C# have destructors?

Yes, but they are not at all like C++ destructors.

In a C# class the destructor is just an alias for the Finalize method. When the garbage collector frees the memory used by an object, it calls the Finalize method on that object (if implemented) to give the object a chance to release any resources that it may hold, such as open files or database connections.

Because garbage collection is handled by the system and not by the developer, it is impossible to know for certain when an object's Finalize method will be called. Objects that must predictably release resources should not rely solely on Finalize. Instead, these objects should implement the IDispose interface and implement its Dispose method. Clients of the object should then call Dispose when the object may safely release its resources.

(Contributed by Paul Parks)

[Section index] [Main index]

Does C# have copy constructors?

No. Objects in C# are always created by reference rather than by value, so a copy constructor is never implicitly called the way that it might be in C++.

That being said, nothing prevents a class from implementing a constructor that accepts a reference to an object of the same type. This constructor may be called explicitly.

class Abc
{
    public int xyz;

    public Abc()
    {
        xyz = 1;
    }

    public Abc(Abc existingObject)
    {
        xyz = existingObject.xyz;
    }
}

class MainClass
{
    static void Main(string[] args)
    {
        Abc firstAbc = new Abc();
        firstAbc.xyz = 123;

        Abc secondAbc = new Abc(firstAbc);
        System.Console.WriteLine(secondAbc.xyz); 
    }
}
							
(Contributed by Paul Parks)

[Section index] [Main index]