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. about the interface of C#

about the interface of C#

Scheduled Pinned Locked Moved C#
csharpquestion
8 Posts 6 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
    mctramp168
    wrote on last edited by
    #1

    I am a novice of C#,Could you please tell me the advantage and disadvantage of using interface and not using interface in the proggram? I'm gragteful if you give a litte code for contrast. Thanks!

    C R P A 4 Replies Last reply
    0
    • M mctramp168

      I am a novice of C#,Could you please tell me the advantage and disadvantage of using interface and not using interface in the proggram? I'm gragteful if you give a litte code for contrast. Thanks!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Well, a program without an interface can't really do much, can it ? Or do you mean interfaces as a coding construct, not a user interface ? I would suggest you buy a book on C# and read it, rather than hope to get a deep understanding from random forum replies. An interface is useful mostly as a means to get around the lack of multiple inheritance.

      Christian Graus Driven to the arms of OSX by Vista. Please read this[^] if you don't like the answer I gave to your question. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.

      1 Reply Last reply
      0
      • M mctramp168

        I am a novice of C#,Could you please tell me the advantage and disadvantage of using interface and not using interface in the proggram? I'm gragteful if you give a litte code for contrast. Thanks!

        R Offline
        R Offline
        Richard Blythe
        wrote on last edited by
        #3

        I'm assuming like Christian that your question is about using interfaces in your own classes. Interfaces do not offer any significant performance benefit. However, they are very useful in situations where classes do inherit from the same base type. Here's a quick example of using interfaces: public class SecretService : ISecurityCleared .... public class CIA : ISecurityCleared ... public class President : ISecurityCleared ... //ISecurityCleared interface... public interface ISecurityCleared { void ViewTopSecretMessage(string message); } //sample code... private void TopSecretMessage(string topsecretmessage) { List<ISecurityCleared> securityCleared = new List<ISecurityCleared>(); securityCleared.Add(new SecretService()); securityCleared.Add(new CIA()); securityCleared.Add(new President()); for (int i = 0; i < securityCleared.Count; i++) securityCleared.ViewTopSecretMessage(topsecretmessage); } Again, no performance increase, just cleaner code without having to worry about inheritance issues. Cheers, Richard

        There cannot be a crisis today; my schedule is already full.

        L 1 Reply Last reply
        0
        • M mctramp168

          I am a novice of C#,Could you please tell me the advantage and disadvantage of using interface and not using interface in the proggram? I'm gragteful if you give a litte code for contrast. Thanks!

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Another advantage of interfaces (again assuming you are talking about code constructs and not the UI) is that they provide a well defined contract to program against. In other words, if you know that a certain type supports a certain interface, then you know that it implements the methods on that interface. An example:

          public interface IMyInterface
          {
          void DoSomething();
          }

          public class MyInterfaceClass : IMyInterface
          {
          public void DoSomething()
          {
          Console.WriteLine("IMyInterface called");
          }
          }

          Then, you could do something like the following:

          public class MyGenericClass<T> where T : IMyInterface, new()
          {
          public void CallAMethod()
          {
          T value = new T();
          value.DoSomething();
          }
          }

          The generic class is constrained to using classes that support the IMyInterface interface, which means you could write code like this:

          new MyGenericClass<MyInterfaceClass>().CallAMethod();

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • R Richard Blythe

            I'm assuming like Christian that your question is about using interfaces in your own classes. Interfaces do not offer any significant performance benefit. However, they are very useful in situations where classes do inherit from the same base type. Here's a quick example of using interfaces: public class SecretService : ISecurityCleared .... public class CIA : ISecurityCleared ... public class President : ISecurityCleared ... //ISecurityCleared interface... public interface ISecurityCleared { void ViewTopSecretMessage(string message); } //sample code... private void TopSecretMessage(string topsecretmessage) { List<ISecurityCleared> securityCleared = new List<ISecurityCleared>(); securityCleared.Add(new SecretService()); securityCleared.Add(new CIA()); securityCleared.Add(new President()); for (int i = 0; i < securityCleared.Count; i++) securityCleared.ViewTopSecretMessage(topsecretmessage); } Again, no performance increase, just cleaner code without having to worry about inheritance issues. Cheers, Richard

            There cannot be a crisis today; my schedule is already full.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Richard Blythe wrote:

            securityCleared.Add(new President());

            I wish...

            Check out the CodeProject forum Guidelines[^]

            R 1 Reply Last reply
            0
            • L Lost User

              Richard Blythe wrote:

              securityCleared.Add(new President());

              I wish...

              Check out the CodeProject forum Guidelines[^]

              R Offline
              R Offline
              Richard Blythe
              wrote on last edited by
              #6

              :-D No comment

              There cannot be a crisis today; my schedule is already full.

              1 Reply Last reply
              0
              • M mctramp168

                I am a novice of C#,Could you please tell me the advantage and disadvantage of using interface and not using interface in the proggram? I'm gragteful if you give a litte code for contrast. Thanks!

                A Offline
                A Offline
                Alan Balkany
                wrote on last edited by
                #7

                Another benefit of interfaces is that they help with code reuse. If a method parameter is an interface type, you can call it by passing an object of ANY class that implements that interface.

                M 1 Reply Last reply
                0
                • A Alan Balkany

                  Another benefit of interfaces is that they help with code reuse. If a method parameter is an interface type, you can call it by passing an object of ANY class that implements that interface.

                  M Offline
                  M Offline
                  mctramp168
                  wrote on last edited by
                  #8

                  thanks all for your help!

                  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