Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. interface members as internal

interface members as internal

Scheduled Pinned Locked Moved C#
csharpdotnetdesigndata-structures
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike Hodnick
    wrote on last edited by
    #1

    Is it possible to implement methods from an interface class as internal in C#? From my first try, it doesn't look like it is possible (the project won't build): public interface IMyInterface{   string Name{ get; set; } } public class MyClass : IMyInterface{   internal string Name{     get{}     set{}   } } ...this results in a build error. But this IS possible to do in VB.Net: Public Interface IMyInterface    Property Name() as String End Interface Public Class MyClass   Implements IMyInterface   Friend Property Name() as String Implements IMyInterface.Name     Get     End Get     Set(value as String)     End Set   End Property End Class ...this generates no errors. Why is this? If VB.Net can do it, the CLR must allow it, so why can't C# do it? Would it be because an internal class member defined in an interface doesn't make much sense from a design standpoint? In other words, an internal interface member kind of defeats the purpose of creating an interface in the first place. Is this just a well-thought-out constraint of C#? - Mike ------------------------- "No human being would stack books like that." - Dr. Venkman

    H 1 Reply Last reply
    0
    • M Mike Hodnick

      Is it possible to implement methods from an interface class as internal in C#? From my first try, it doesn't look like it is possible (the project won't build): public interface IMyInterface{   string Name{ get; set; } } public class MyClass : IMyInterface{   internal string Name{     get{}     set{}   } } ...this results in a build error. But this IS possible to do in VB.Net: Public Interface IMyInterface    Property Name() as String End Interface Public Class MyClass   Implements IMyInterface   Friend Property Name() as String Implements IMyInterface.Name     Get     End Get     Set(value as String)     End Set   End Property End Class ...this generates no errors. Why is this? If VB.Net can do it, the CLR must allow it, so why can't C# do it? Would it be because an internal class member defined in an interface doesn't make much sense from a design standpoint? In other words, an internal interface member kind of defeats the purpose of creating an interface in the first place. Is this just a well-thought-out constraint of C#? - Mike ------------------------- "No human being would stack books like that." - Dr. Venkman

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      C# can, but you're mixing implementation types. You do this in C# using explicit interface implementations, which is what youre VB.NET snippet is doing:

      public class MyClass : IMyInterface
      {
      void IMyInterface.MyMethod()
      {
      }
      }

      Notice that there's no access modifier. This would be private. This fairly common throughout the .NET FCL. Think about ADO.NET: there's several interface methods but they're all typed; the explicit interface implementation still works and is used to essentially "hide" the "generic" method so that a typed method can be used.

      Microsoft MVP, Visual C# My Articles

      M 1 Reply Last reply
      0
      • H Heath Stewart

        C# can, but you're mixing implementation types. You do this in C# using explicit interface implementations, which is what youre VB.NET snippet is doing:

        public class MyClass : IMyInterface
        {
        void IMyInterface.MyMethod()
        {
        }
        }

        Notice that there's no access modifier. This would be private. This fairly common throughout the .NET FCL. Think about ADO.NET: there's several interface methods but they're all typed; the explicit interface implementation still works and is used to essentially "hide" the "generic" method so that a typed method can be used.

        Microsoft MVP, Visual C# My Articles

        M Offline
        M Offline
        Mike Hodnick
        wrote on last edited by
        #3

        Makes perfect sense. This actually helps open up some other doors that explain a few other things. Muchas gracias. - Mike ------------------------- "No human being would stack books like that." - Dr. Venkman

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups