Skip to content

Virtual member call in constructor warning #1627

@ArthurMa1978

Description

@ArthurMa1978

UPDATE https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/resourcemanager/Azure.ResourceManager/src/ArmResource.cs#L67

Be Careful with Virtual Method Calls from the Constructor (and Destructor) of your Classes in C++/C#/Java! - CodeProject.

While the following is probably ok, it would be best and follow convention to call into base()'s constructor.

public abstract class TrackedResource<TModel> : TrackedResource
    where TModel : class
{
    /// <summary>
    /// Initializes a new instance of the <see cref="TrackedResource{TModel}"/> class.
    /// </summary>
    /// <param name="id"> The identifier of the resource that is the target of operations. </param>
    /// <param name="location"> The location of the resource. </param>
    /// <param name="data"> The model to copy from. </param>
    protected TrackedResource(ResourceIdentifier id, LocationData location, TModel data)
    {
        Id = id;
        Location = location;
        Model = data;
    }

Copied from Repro Steps when changed from Bug to User Story

Copied from Description when changed from Feature to Bug
Be Careful with Virtual Method Calls from the Constructor (and Destructor) of your Classes in C++/C#/Java! - CodeProject.

While the following is probably ok, it would be best and follow convention to call into base()'s constructor.

public abstract class TrackedResource<TModel> : TrackedResource
    where TModel : class
{
    /// <summary>
    /// Initializes a new instance of the <see cref="TrackedResource{TModel}"/> class.
    /// </summary>
    /// <param name="id"> The identifier of the resource that is the target of operations. </param>
    /// <param name="location"> The location of the resource. </param>
    /// <param name="data"> The model to copy from. </param>
    protected TrackedResource(ResourceIdentifier id, LocationData location, TModel data)
    {
        Id = id;
        Location = location;
        Model = data;
    }

Metadata

Metadata

Assignees

Labels

MgmtThis issue is related to a management-plane library.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions