The Odd Developer
The Odd Developer

Reputation: 929

C# instantiate a new class object inside same class itself as per the MS documentation

I came across this when I was checking the abstract classes (C# Reference) - MS doc link

Bottom of the documentation the derived class from the abstract class instantiate a new object inside the Main method.

It instantiate a new class object inside same class itself like this:

// Abstract class
abstract class BaseClass
{
    protected int _x = 100;
    protected int _y = 150;

    // Abstract method
    public abstract void AbstractMethod();

    // Abstract properties
    public abstract int X { get; }
    public abstract int Y { get; }
}

class DerivedClass : BaseClass
{
    public override void AbstractMethod()
    {
        _x++;
        _y++;
    }

    public override int X   // overriding property
    {
        get
        {
            return _x + 10;
        }
    }

    public override int Y   // overriding property
    {
        get
        {
            return _y + 10;
        }
    }

    static void Main()
    {
        var o = new DerivedClass();
        o.AbstractMethod();
        Console.WriteLine($"x = {o.X}, y = {o.Y}");
    }
}

I'm referring to this code snippet specifically.

var o = new DerivedClass();

My question is that correct?

And the other question is in the abstract class abstract methods have the following.

public abstract int X { get; }
public abstract int Y { get; }

Is that correct also?

Because in abstract methods cannot have a body.

Upvotes: 0

Views: 529

Answers (1)

Guru Stron
Guru Stron

Reputation: 141575

I'm referring to this code snippet specifically. var o = new DerivedClass(); My question is that correct?

It depends on what do you mean by "correct". In terms of program compiling/working - yes, it is "correct", though a bit unconventional (maybe due to desire to make a shorter code snippet) - more often approach is to place Main method inside a Program class (or skip it completely by using top level statements). There are several points of interest here:

  1. The Main method. It is the entry point of an executable program; it is where the program control starts and ends. Main is declared inside a class or struct. Main must be static (docs).
  2. static modifier can be used to declare a static member, which belongs to the type itself rather than to a specific object.(docs, see also - Static Classes and Static Class Members)
  3. AbstractMethod is an instance member of class, instance members require an instance to be called (i.e. you can call static method via type name DerivedClass.Main() while instance one will require a created instance for example new Derived().AbstractMethod())(for example see this answer)

Because in abstract methods cannot have a body.

public abstract int X { get; } does not have a body, this is a declaration of an abstract property (docs), though except for the abstract keyword it looks exactly like auto-implemented property (docs):

abstract class Abstract
{
   public abstract int X { get; }   
}

class Concrete : Abstract
{
   public Concrete(int x)
   {
       X = x; 
   }

   // readonly (can be set only in ctor) auto-implemented property
   public override int X { get; }   
}

Upvotes: 1

Related Questions