Destructor for an abstract class

In C ++, I know that a virtual destructor should usually be used when you inherit a base class. However, with C # I am not sure what to do. Consider the following code:

public abstract class Character
{
    private string characterName;
    public int health;

    Character()
    {

    }

    ~Character(){

    }

    public virtual void SetCharacterName( string tempName )
    {
        characterName = tempName;
    }

    public virtual string GetCharacterName( )
    {
        return characterName;
    }
}

(Note. I heard that the implementation of Unity3Ds C # is slightly different from the standard. Perhaps ignore some minor formatting errors, the code seems to be functional ...)

My first instinct was to make destructor ~ Character () virtual by defining it as:

virtual ~Character(){

}

but the IDE returns an error.

In C #, is it necessary or considered standard to use a virtual destructor for abstract classes that want to inherit? Or are there other ways to create a virtual destructor with C #?

+4
2

# . , , : IDisposable.

GC , , . [ ] , , , . , , . , GC .

, . , "" http://msdn.microsoft.com/en-us/library/system.object.finalize.aspx ,

- undefined. , Close IDisposable.Dispose .

, Finalizer .NET, :

Finalize ?

...

, FinalizationQueue . , , (GC), . . . , GC , Finalizer , GC.

, , GC, GC , .

, , .

- , IDisposable Dispose(), using:

using ( Character foo = CharacterFactory.CreateInstance("Jane") )
{
   // do something useful with Jane
}

using , foo.Dispose() . ( foo):

Character foo = ... ;
try
{
  ...
}
finally
{
  foo.Dispose() ;
}

, IDisposable , , . , 400 , GC'd, . , .

+7

#, , ?

, #. ( ).

, IDisposable using (, ).

. stackoverflow : #

+7

Source: https://habr.com/ru/post/1539989/


All Articles