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. Declare variable as Interface vs. Class

Declare variable as Interface vs. Class

Scheduled Pinned Locked Moved C#
visual-studio
4 Posts 3 Posters 1 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.
  • T Offline
    T Offline
    trinh nguyen
    wrote on last edited by
    #1

    Hi, Could anyone explain me the difference between interface and class variable. E.g.: ISomething ivar; //interface variable CSomething cvar; //class variable or IList<MyClass> var1; List<MyClass> var2; I am so confused the difference among them. Thanks in advance!

    D A 3 Replies Last reply
    0
    • T trinh nguyen

      Hi, Could anyone explain me the difference between interface and class variable. E.g.: ISomething ivar; //interface variable CSomething cvar; //class variable or IList<MyClass> var1; List<MyClass> var2; I am so confused the difference among them. Thanks in advance!

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

      Using an interface makes it simpler to use multiple varying class instances that implement an interface without the need for extra coding as the interface defines properties, methods and events that must be accessible.

      Dave
      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      1 Reply Last reply
      0
      • T trinh nguyen

        Hi, Could anyone explain me the difference between interface and class variable. E.g.: ISomething ivar; //interface variable CSomething cvar; //class variable or IList<MyClass> var1; List<MyClass> var2; I am so confused the difference among them. Thanks in advance!

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        Technically, there is little to no difference, you would need to instantiate both these variables. However, by defining var1 as an interface type, you have left the actual definition open for later, giving additional flexibility. Based on some options, you could switch between different types at runtime when using an IList. For e.g.

        if (flag)
        {
        var1 = new List();
        }
        else
        {
        var 1 = new MyOwnList(); //a custom implementation of IList.
        }

        Build your own survey - http://www.factile.net

        1 Reply Last reply
        0
        • T trinh nguyen

          Hi, Could anyone explain me the difference between interface and class variable. E.g.: ISomething ivar; //interface variable CSomething cvar; //class variable or IList<MyClass> var1; List<MyClass> var2; I am so confused the difference among them. Thanks in advance!

          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #4

          Technically, there is little to no difference, you would need to instantiate both these variables. However, by defining var1 as an interface type, you have left the actual definition open for later, giving additional flexibility. Based on some options, you could switch between different types at runtime when using an IList. For e.g.

          if (flag)
          {
          var1 = new List();
          }
          else
          {
          var 1 = new MyOwnList(); //a custom implementation of the IList interface.
          }

          Build your own survey - http://www.factile.net

          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