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. C# interface inheritance

C# interface inheritance

Scheduled Pinned Locked Moved C#
csharpoophelptutorialquestion
9 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.
  • S Offline
    S Offline
    Shane5555
    wrote on last edited by
    #1

    Hi, I have a basic enough problem with implementing inheritance in an interface. Here is an example of what I would LIKE to do:

        static void Main(string\[\] args)
        {
            List<ISuperString> myStrings = new List<ISuperString>();
            myStrings.Add(...SOMETHING...);
    
            Console.WriteLine(myStrings\[0\].containsCapitalLetters());
            Console.WriteLine(myStrings\[0\].ToLower());
        }
    
        public interface ISuperString : string
        {
            bool containsCapitalLetters();
        }
    

    I have seen (and implemented) some workarounds for this, however they are a bit messy for my taste. Basically, is it possible to either have an interface inherit from a class or to derive an interface from a class (i.e. an IString interface)? thanks

    realJSOPR D S 3 Replies Last reply
    0
    • S Shane5555

      Hi, I have a basic enough problem with implementing inheritance in an interface. Here is an example of what I would LIKE to do:

          static void Main(string\[\] args)
          {
              List<ISuperString> myStrings = new List<ISuperString>();
              myStrings.Add(...SOMETHING...);
      
              Console.WriteLine(myStrings\[0\].containsCapitalLetters());
              Console.WriteLine(myStrings\[0\].ToLower());
          }
      
          public interface ISuperString : string
          {
              bool containsCapitalLetters();
          }
      

      I have seen (and implemented) some workarounds for this, however they are a bit messy for my taste. Basically, is it possible to either have an interface inherit from a class or to derive an interface from a class (i.e. an IString interface)? thanks

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      What I think you really want is an extension method.

      public class ExtensionMethods
      {
      public static bool ContainsCapitalLetters(this String)
      {
      bool result = false;
      // ...do your code here...
      return result;
      }
      }

      Usage would be

      String myString = "AbcdefG";
      bool containsCapitalLetters = myString.ContainsCapitalLetters();

      .45 ACP - because shooting twice is just silly
      -----
      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

      1 Reply Last reply
      0
      • S Shane5555

        Hi, I have a basic enough problem with implementing inheritance in an interface. Here is an example of what I would LIKE to do:

            static void Main(string\[\] args)
            {
                List<ISuperString> myStrings = new List<ISuperString>();
                myStrings.Add(...SOMETHING...);
        
                Console.WriteLine(myStrings\[0\].containsCapitalLetters());
                Console.WriteLine(myStrings\[0\].ToLower());
            }
        
            public interface ISuperString : string
            {
                bool containsCapitalLetters();
            }
        

        I have seen (and implemented) some workarounds for this, however they are a bit messy for my taste. Basically, is it possible to either have an interface inherit from a class or to derive an interface from a class (i.e. an IString interface)? thanks

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        As the string class is sealed you cannot subclass it to force it to implement your interface. If it's just methods you want to add and you're using 3.5 or 4.0 then you can use extension methods as John suggested. If you want to add more than methods or are using 2.0 then you will need to create a wrapper around the string class - something like this will get you started

        public class SuperString : ISuperString
        {
        private string wrappedString;

        public SuperString(string stringToWrap)
        {
            wrappedString = stringToWrap;
        }
        
        public static implicit operator string(SuperString superString)
        {
            return superString.wrappedString;
        }
        public static implicit operator SuperString(string stringToWrap)
        {
            return new SuperString(stringToWrap);
        }
        
        public bool containsCapitalLetters()
        {
            throw new NotImplementedException();
        }
        

        }

        public interface ISuperString
        {
        bool containsCapitalLetters();
        }

        Dave
        Tip: Passing values between objects using events (C#)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        L 1 Reply Last reply
        0
        • S Shane5555

          Hi, I have a basic enough problem with implementing inheritance in an interface. Here is an example of what I would LIKE to do:

              static void Main(string\[\] args)
              {
                  List<ISuperString> myStrings = new List<ISuperString>();
                  myStrings.Add(...SOMETHING...);
          
                  Console.WriteLine(myStrings\[0\].containsCapitalLetters());
                  Console.WriteLine(myStrings\[0\].ToLower());
              }
          
              public interface ISuperString : string
              {
                  bool containsCapitalLetters();
              }
          

          I have seen (and implemented) some workarounds for this, however they are a bit messy for my taste. Basically, is it possible to either have an interface inherit from a class or to derive an interface from a class (i.e. an IString interface)? thanks

          S Offline
          S Offline
          Shane5555
          wrote on last edited by
          #4

          Thanks guys, however the problem goes a bit deeper than my string example (and btw, I didn't realise a string was sealed so I suppose that turned my oversimplification on its head) What I need is an interface to use within a function (like in the list above) A wrapper class would work fine (and at the moment I am using a wrapper interface) but inheriting from a class to an interface would be a LOT cleaner.

          A P 2 Replies Last reply
          0
          • S Shane5555

            Thanks guys, however the problem goes a bit deeper than my string example (and btw, I didn't realise a string was sealed so I suppose that turned my oversimplification on its head) What I need is an interface to use within a function (like in the list above) A wrapper class would work fine (and at the moment I am using a wrapper interface) but inheriting from a class to an interface would be a LOT cleaner.

            A Offline
            A Offline
            AspDotNetDev
            wrote on last edited by
            #5

            Not really sure what your question is. Sounds like you just want to create an interface and implement it with a class. That's pretty basic C#... that's all I've gathered from your question (now that you've added the disclaimer about not using string as an example anymore because it's sealed). You might want to take a look at my StringBuilderPlus Improves Upon StringBuilder article, which makes use of interfaces and implementing those interfaces. The diagram at the bottom of the article shows the interface IString and the two classes, PlainString and StringsWrapper, that implement IString. Download the code if you want to see how that's done.

            Shane5555 wrote:

            inheriting from a class to an interface would be a LOT cleaner

            An interface cannot inherit from a class, but a class can inherit from a class. And a class can implement an interface. And an interface can inherit from an interface.

            [Forum Guidelines]

            1 Reply Last reply
            0
            • D DaveyM69

              As the string class is sealed you cannot subclass it to force it to implement your interface. If it's just methods you want to add and you're using 3.5 or 4.0 then you can use extension methods as John suggested. If you want to add more than methods or are using 2.0 then you will need to create a wrapper around the string class - something like this will get you started

              public class SuperString : ISuperString
              {
              private string wrappedString;

              public SuperString(string stringToWrap)
              {
                  wrappedString = stringToWrap;
              }
              
              public static implicit operator string(SuperString superString)
              {
                  return superString.wrappedString;
              }
              public static implicit operator SuperString(string stringToWrap)
              {
                  return new SuperString(stringToWrap);
              }
              
              public bool containsCapitalLetters()
              {
                  throw new NotImplementedException();
              }
              

              }

              public interface ISuperString
              {
              bool containsCapitalLetters();
              }

              Dave
              Tip: Passing values between objects using events (C#)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Why are you using VB6? Do you hate yourself? (Christian Graus)

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

              DaveyM69 wrote:

              and you're using 3.5 or 4.0

              Or 2.0! Just add

              namespace System.Runtime.CompilerServices
              {
              [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
              public sealed class ExtensionAttribute : Attribute
              {
              public ExtensionAttribute() { }
              }
              }

              And set the compiler to C#3

              A 1 Reply Last reply
              0
              • L Lost User

                DaveyM69 wrote:

                and you're using 3.5 or 4.0

                Or 2.0! Just add

                namespace System.Runtime.CompilerServices
                {
                [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
                public sealed class ExtensionAttribute : Attribute
                {
                public ExtensionAttribute() { }
                }
                }

                And set the compiler to C#3

                A Offline
                A Offline
                AspDotNetDev
                wrote on last edited by
                #7

                So you're saying extension methods can be used in Visual Studio 2005? Sounds like a really good tip/trick!

                [Forum Guidelines]

                L 1 Reply Last reply
                0
                • A AspDotNetDev

                  So you're saying extension methods can be used in Visual Studio 2005? Sounds like a really good tip/trick!

                  [Forum Guidelines]

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

                  AFAIK no, it does require the new compiler (and it would probably mess up IntelliSense on VS05), but with this trick you can target .NET 2.0 (with VS08 and higher) and still use extension methods

                  1 Reply Last reply
                  0
                  • S Shane5555

                    Thanks guys, however the problem goes a bit deeper than my string example (and btw, I didn't realise a string was sealed so I suppose that turned my oversimplification on its head) What I need is an interface to use within a function (like in the list above) A wrapper class would work fine (and at the moment I am using a wrapper interface) but inheriting from a class to an interface would be a LOT cleaner.

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Shane5555 wrote:

                    would be a LOT cleaner.

                    No, I'm pretty sure not. At most you may be looking for duck-typing, but maybe not. You may need to present your concept better.

                    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