Why a property inherited from interface became virtual?











up vote
9
down vote

favorite












Say I have one interface and two classes, one of the class implement that interface:



interface IAAA
{
int F1 { get; set; }
}

class AAA1
{
public int F1 { get; set; }
public int F2 { get; set; }
}

class AAA2 : IAAA
{
public int F1 { get; set; }
public int F2 { get; set; }
}


In class AAA2, property F1 is 'inherited' (I'm not sure) from interface IAAA, then I use reflection to check whether a property is virtual:



Console.WriteLine("AAA1 which not implement IAAA");
foreach (var prop in typeof(AAA1).GetProperties())
{
var virtualOrNot = prop.GetGetMethod().IsVirtual ? "" : " not";
Console.WriteLine($@"{prop.Name} is{virtualOrNot} virtual");
}

Console.WriteLine("AAA2 which implemented IAAA");
foreach (var prop in typeof(AAA2).GetProperties())
{
var virtualOrNot = prop.GetGetMethod().IsVirtual ? "" : " not";
Console.WriteLine($"{prop.Name} is{virtualOrNot} virtual");
}


the output is:



AAA1 which not implement IAAA
F1 is not virtual
F2 is not virtual
AAA2 which implemented IAAA
F1 is virtual
F2 is not virtual


any reason for this?










share|improve this question






















  • Finally a decent question
    – T.S.
    2 hours ago










  • upovted for clarity of question
    – Ehsan Sajjad
    1 hour ago















up vote
9
down vote

favorite












Say I have one interface and two classes, one of the class implement that interface:



interface IAAA
{
int F1 { get; set; }
}

class AAA1
{
public int F1 { get; set; }
public int F2 { get; set; }
}

class AAA2 : IAAA
{
public int F1 { get; set; }
public int F2 { get; set; }
}


In class AAA2, property F1 is 'inherited' (I'm not sure) from interface IAAA, then I use reflection to check whether a property is virtual:



Console.WriteLine("AAA1 which not implement IAAA");
foreach (var prop in typeof(AAA1).GetProperties())
{
var virtualOrNot = prop.GetGetMethod().IsVirtual ? "" : " not";
Console.WriteLine($@"{prop.Name} is{virtualOrNot} virtual");
}

Console.WriteLine("AAA2 which implemented IAAA");
foreach (var prop in typeof(AAA2).GetProperties())
{
var virtualOrNot = prop.GetGetMethod().IsVirtual ? "" : " not";
Console.WriteLine($"{prop.Name} is{virtualOrNot} virtual");
}


the output is:



AAA1 which not implement IAAA
F1 is not virtual
F2 is not virtual
AAA2 which implemented IAAA
F1 is virtual
F2 is not virtual


any reason for this?










share|improve this question






















  • Finally a decent question
    – T.S.
    2 hours ago










  • upovted for clarity of question
    – Ehsan Sajjad
    1 hour ago













up vote
9
down vote

favorite









up vote
9
down vote

favorite











Say I have one interface and two classes, one of the class implement that interface:



interface IAAA
{
int F1 { get; set; }
}

class AAA1
{
public int F1 { get; set; }
public int F2 { get; set; }
}

class AAA2 : IAAA
{
public int F1 { get; set; }
public int F2 { get; set; }
}


In class AAA2, property F1 is 'inherited' (I'm not sure) from interface IAAA, then I use reflection to check whether a property is virtual:



Console.WriteLine("AAA1 which not implement IAAA");
foreach (var prop in typeof(AAA1).GetProperties())
{
var virtualOrNot = prop.GetGetMethod().IsVirtual ? "" : " not";
Console.WriteLine($@"{prop.Name} is{virtualOrNot} virtual");
}

Console.WriteLine("AAA2 which implemented IAAA");
foreach (var prop in typeof(AAA2).GetProperties())
{
var virtualOrNot = prop.GetGetMethod().IsVirtual ? "" : " not";
Console.WriteLine($"{prop.Name} is{virtualOrNot} virtual");
}


the output is:



AAA1 which not implement IAAA
F1 is not virtual
F2 is not virtual
AAA2 which implemented IAAA
F1 is virtual
F2 is not virtual


any reason for this?










share|improve this question













Say I have one interface and two classes, one of the class implement that interface:



interface IAAA
{
int F1 { get; set; }
}

class AAA1
{
public int F1 { get; set; }
public int F2 { get; set; }
}

class AAA2 : IAAA
{
public int F1 { get; set; }
public int F2 { get; set; }
}


In class AAA2, property F1 is 'inherited' (I'm not sure) from interface IAAA, then I use reflection to check whether a property is virtual:



Console.WriteLine("AAA1 which not implement IAAA");
foreach (var prop in typeof(AAA1).GetProperties())
{
var virtualOrNot = prop.GetGetMethod().IsVirtual ? "" : " not";
Console.WriteLine($@"{prop.Name} is{virtualOrNot} virtual");
}

Console.WriteLine("AAA2 which implemented IAAA");
foreach (var prop in typeof(AAA2).GetProperties())
{
var virtualOrNot = prop.GetGetMethod().IsVirtual ? "" : " not";
Console.WriteLine($"{prop.Name} is{virtualOrNot} virtual");
}


the output is:



AAA1 which not implement IAAA
F1 is not virtual
F2 is not virtual
AAA2 which implemented IAAA
F1 is virtual
F2 is not virtual


any reason for this?







c# reflection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 3 hours ago









runerback

768




768












  • Finally a decent question
    – T.S.
    2 hours ago










  • upovted for clarity of question
    – Ehsan Sajjad
    1 hour ago


















  • Finally a decent question
    – T.S.
    2 hours ago










  • upovted for clarity of question
    – Ehsan Sajjad
    1 hour ago
















Finally a decent question
– T.S.
2 hours ago




Finally a decent question
– T.S.
2 hours ago












upovted for clarity of question
– Ehsan Sajjad
1 hour ago




upovted for clarity of question
– Ehsan Sajjad
1 hour ago












1 Answer
1






active

oldest

votes

















up vote
9
down vote



accepted










As from remarks section of MS docs:




A virtual member may reference instance data in a class and must be referenced through an instance of the class... The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method virtual final




If you need to determine whether this method is overridable then checking IsVirtual is not enough and you need to also check that IsFinal is false.



Here is an extension method that do this check:



public static bool IsOverridable(this MethodInfo method)
=> method.IsVirtual && !method.IsFinal;





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492538%2fwhy-a-property-inherited-from-interface-became-virtual%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    9
    down vote



    accepted










    As from remarks section of MS docs:




    A virtual member may reference instance data in a class and must be referenced through an instance of the class... The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method virtual final




    If you need to determine whether this method is overridable then checking IsVirtual is not enough and you need to also check that IsFinal is false.



    Here is an extension method that do this check:



    public static bool IsOverridable(this MethodInfo method)
    => method.IsVirtual && !method.IsFinal;





    share|improve this answer



























      up vote
      9
      down vote



      accepted










      As from remarks section of MS docs:




      A virtual member may reference instance data in a class and must be referenced through an instance of the class... The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method virtual final




      If you need to determine whether this method is overridable then checking IsVirtual is not enough and you need to also check that IsFinal is false.



      Here is an extension method that do this check:



      public static bool IsOverridable(this MethodInfo method)
      => method.IsVirtual && !method.IsFinal;





      share|improve this answer

























        up vote
        9
        down vote



        accepted







        up vote
        9
        down vote



        accepted






        As from remarks section of MS docs:




        A virtual member may reference instance data in a class and must be referenced through an instance of the class... The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method virtual final




        If you need to determine whether this method is overridable then checking IsVirtual is not enough and you need to also check that IsFinal is false.



        Here is an extension method that do this check:



        public static bool IsOverridable(this MethodInfo method)
        => method.IsVirtual && !method.IsFinal;





        share|improve this answer














        As from remarks section of MS docs:




        A virtual member may reference instance data in a class and must be referenced through an instance of the class... The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method virtual final




        If you need to determine whether this method is overridable then checking IsVirtual is not enough and you need to also check that IsFinal is false.



        Here is an extension method that do this check:



        public static bool IsOverridable(this MethodInfo method)
        => method.IsVirtual && !method.IsFinal;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 14 mins ago









        Alexei Levenkov

        83.4k888129




        83.4k888129










        answered 3 hours ago









        vasily.sib

        1,7411716




        1,7411716






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492538%2fwhy-a-property-inherited-from-interface-became-virtual%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

            Mangá

            Eduardo VII do Reino Unido