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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Interface members accesibility

Interface members accesibility

Scheduled Pinned Locked Moved C#
question
3 Posts 3 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.
  • H Offline
    H Offline
    Heinz_
    wrote on last edited by
    #1

    Hi, Methods in an interface must be public? If i have a public method in an interface, can i make this method "internal" (or any other access parameter) in a class derived from the interface? ie: public interface myinter { public void MyMethod(){} } public class myclass : myinter { internal void MyMethod(){//my code} }

    C R 2 Replies Last reply
    0
    • H Heinz_

      Hi, Methods in an interface must be public? If i have a public method in an interface, can i make this method "internal" (or any other access parameter) in a class derived from the interface? ie: public interface myinter { public void MyMethod(){} } public class myclass : myinter { internal void MyMethod(){//my code} }

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Heinz Suez wrote: Methods in an interface must be public? Yes, that is the point. It is defining the external interface* to something. In other words, it is defining the interface* (or part of the interface*) to your class that other classes use. Heinz Suez wrote: If i have a public method in an interface, can i make this method "internal" (or any other access parameter) in a class derived from the interface? No. You cannot. This is because the class would then no longer support the interface it advertises as supporting. Do not confuse "interface" with the C# keyword "interface". An interface is another way of saying how an external entity sees the class. You provide an interface with any public methods. An interface is a way of defining a common set of public methods or properties that a number of classes support.


      My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

      1 Reply Last reply
      0
      • H Heinz_

        Hi, Methods in an interface must be public? If i have a public method in an interface, can i make this method "internal" (or any other access parameter) in a class derived from the interface? ie: public interface myinter { public void MyMethod(){} } public class myclass : myinter { internal void MyMethod(){//my code} }

        R Offline
        R Offline
        Robert Rohde
        wrote on last edited by
        #3

        First of all your code won't compile :) And yes all members of an interface must be public visible to everyone using this interface. But you can restrict them ONLY to explicit interface users:

        public interface ITest
        {
        void TestA();
        }

        public class Class1 : ITest
        {
        void ITest.TestA() {}
        }

        static void Main()
        {
        Class1 c = new Class1();
        c.TestA(); //gives compile error
        ITest t = c;
        t.TestA(); //works perfectly well
        }

        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