Why does a property inherited from an interface become virtual?











up vote
30
down vote

favorite
2












Say I have one interface and two classes, and one of the classes implement this 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 does 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 implements 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 does not implement IAAA
F1 is not virtual
F2 is not virtual
AAA2 which implements IAAA
F1 is virtual
F2 is not virtual


Any reason for this?










share|improve this question




























    up vote
    30
    down vote

    favorite
    2












    Say I have one interface and two classes, and one of the classes implement this 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 does 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 implements 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 does not implement IAAA
    F1 is not virtual
    F2 is not virtual
    AAA2 which implements IAAA
    F1 is virtual
    F2 is not virtual


    Any reason for this?










    share|improve this question


























      up vote
      30
      down vote

      favorite
      2









      up vote
      30
      down vote

      favorite
      2






      2





      Say I have one interface and two classes, and one of the classes implement this 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 does 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 implements 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 does not implement IAAA
      F1 is not virtual
      F2 is not virtual
      AAA2 which implements IAAA
      F1 is virtual
      F2 is not virtual


      Any reason for this?










      share|improve this question















      Say I have one interface and two classes, and one of the classes implement this 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 does 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 implements 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 does not implement IAAA
      F1 is not virtual
      F2 is not virtual
      AAA2 which implements 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








      edited Nov 27 at 13:48









      Boann

      36.5k1287120




      36.5k1287120










      asked Nov 27 at 3:58









      runerback

      18119




      18119
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          28
          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 method also returns false for a method (or getter/setter of a property etc.) declared sealed override in C# (whether it implements an interface or not). Checking .IsVirtual alone in these cases would yield true.
            – Jeppe Stig Nielsen
            Nov 27 at 14:29











          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-does-a-property-inherited-from-an-interface-become-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
          28
          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 method also returns false for a method (or getter/setter of a property etc.) declared sealed override in C# (whether it implements an interface or not). Checking .IsVirtual alone in these cases would yield true.
            – Jeppe Stig Nielsen
            Nov 27 at 14:29















          up vote
          28
          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 method also returns false for a method (or getter/setter of a property etc.) declared sealed override in C# (whether it implements an interface or not). Checking .IsVirtual alone in these cases would yield true.
            – Jeppe Stig Nielsen
            Nov 27 at 14:29













          up vote
          28
          down vote



          accepted







          up vote
          28
          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 Nov 27 at 7:00









          Alexei Levenkov

          83.4k889130




          83.4k889130










          answered Nov 27 at 4:10









          vasily.sib

          1,8871919




          1,8871919












          • Your method also returns false for a method (or getter/setter of a property etc.) declared sealed override in C# (whether it implements an interface or not). Checking .IsVirtual alone in these cases would yield true.
            – Jeppe Stig Nielsen
            Nov 27 at 14:29


















          • Your method also returns false for a method (or getter/setter of a property etc.) declared sealed override in C# (whether it implements an interface or not). Checking .IsVirtual alone in these cases would yield true.
            – Jeppe Stig Nielsen
            Nov 27 at 14:29
















          Your method also returns false for a method (or getter/setter of a property etc.) declared sealed override in C# (whether it implements an interface or not). Checking .IsVirtual alone in these cases would yield true.
          – Jeppe Stig Nielsen
          Nov 27 at 14:29




          Your method also returns false for a method (or getter/setter of a property etc.) declared sealed override in C# (whether it implements an interface or not). Checking .IsVirtual alone in these cases would yield true.
          – Jeppe Stig Nielsen
          Nov 27 at 14:29


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492538%2fwhy-does-a-property-inherited-from-an-interface-become-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

          AnyDesk - Fatal Program Failure

          How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

          QoS: MAC-Priority for clients behind a repeater