C# - Interface - Type initialization












0














Consider the example:



IList<string> _lstColl = new { "Alex", "Sam", "Gates", "Riaz" };


I can initialize it
as:



string _stringBag =   new { "Alex", "Sam", "Gates", "Riaz" };


What are the advantages of having Interface here?










share|improve this question





























    0














    Consider the example:



    IList<string> _lstColl = new { "Alex", "Sam", "Gates", "Riaz" };


    I can initialize it
    as:



    string _stringBag =   new { "Alex", "Sam", "Gates", "Riaz" };


    What are the advantages of having Interface here?










    share|improve this question



























      0












      0








      0







      Consider the example:



      IList<string> _lstColl = new { "Alex", "Sam", "Gates", "Riaz" };


      I can initialize it
      as:



      string _stringBag =   new { "Alex", "Sam", "Gates", "Riaz" };


      What are the advantages of having Interface here?










      share|improve this question















      Consider the example:



      IList<string> _lstColl = new { "Alex", "Sam", "Gates", "Riaz" };


      I can initialize it
      as:



      string _stringBag =   new { "Alex", "Sam", "Gates", "Riaz" };


      What are the advantages of having Interface here?







      c#






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 2:41









      Cœur

      17.4k9102143




      17.4k9102143










      asked Aug 22 '09 at 9:26









      user160677

      1,80393553




      1,80393553
























          4 Answers
          4






          active

          oldest

          votes


















          5














          The advantage is that it shows to readers that you're not using the fact that it happens to be an array. I tend to declare local variables as the most general type which contains all the functionality I need. For instance, I'll do:



          using (TextReader reader = File.OpenText(...))


          instead of specifying StreamReader.



          It's rarely hugely important, but it does make it easier to change the details of the implementation. For example, suppose for whatever reason we wanted to change your example to use a List<T> instead of an array.



          You can change this:



          IList<string> _lstColl = new { "Alex", "Sam", "Gates", "Riaz" };


          to this:



          IList<string> _lstColl = new List<string> { "Alex", "Sam", "Gates", "Riaz" };


          and know it'll still compile. Some of the semantics may not be the same, so you still need to be careful - but at least you know it won't be using any array-specific methods.






          share|improve this answer





















          • not C and not Python! *g
            – Henrik P. Hessel
            Aug 22 '09 at 9:37



















          2














          This is an example of generics and you are creating an IList with string datatype.




          Generics introduce to the .NET
          Framework the concept of type
          parameters, which make it possible to
          design classes and methods that defer
          the specification of one or more types
          until the class or method is declared
          and instantiated by client code. For
          example, by using a generic type
          parameter T you can write a single
          class that other client code can use
          without incurring the cost or risk of
          runtime casts or boxing operations.




          Read more here



          What is cool about generics, why use them?



          Do C# Generics Have a Perfomance Benefit?






          share|improve this answer























          • sorry ! would have framed my question ,more specific. What is the advantage of initializiing the interface indirectly.(Ofcourse we can not create instance for interface).
            – user160677
            Aug 22 '09 at 9:36










          • I don't think that this question is really about generics.
            – Stefan Steinegger
            Aug 22 '09 at 9:42



















          2














          There are no advantages or disadvantages. If your code requires an IList, then give it one. If your code requires an array, then give it one.






          share|improve this answer





















          • There are disadvantages. see my answer.
            – Stefan Steinegger
            Aug 22 '09 at 9:43



















          1














          I don't really understand why an array implements IList. When you call Add, it throws an exception. An array does not have IList semantics, because it is not resizable. That's why I avoid having an array as IList. If I need a list, i would write it like this:



          IList<string> _lstColl = new List<string>() { "Alex", "Sam", "Gates", "Riaz" };


          (PS: There are quite a few problems with using of the collection interfaces of the standard framework, like missing methods and even missing interfaces. When you are working with collection interfaces, as usual for instance when using NHibernate, you recognize that they are sometimes badly designed. The developers of this interfaces didn't seem to use them a lot I think. Just my opinion.)






          share|improve this answer























          • It's because there isn't a separate interface that only has an indexer and a count.
            – Jon Skeet
            Aug 22 '09 at 9:35






          • 1




            Yes, see my PS about collection interfaces. I'm really disappointed about the .NET collection design. I mean, this list types and semantics are well known since decades. Why do they come up with a new framework with such a bad collection design?
            – Stefan Steinegger
            Aug 22 '09 at 9:40











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1315603%2fc-sharp-interface-type-initialization%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          The advantage is that it shows to readers that you're not using the fact that it happens to be an array. I tend to declare local variables as the most general type which contains all the functionality I need. For instance, I'll do:



          using (TextReader reader = File.OpenText(...))


          instead of specifying StreamReader.



          It's rarely hugely important, but it does make it easier to change the details of the implementation. For example, suppose for whatever reason we wanted to change your example to use a List<T> instead of an array.



          You can change this:



          IList<string> _lstColl = new { "Alex", "Sam", "Gates", "Riaz" };


          to this:



          IList<string> _lstColl = new List<string> { "Alex", "Sam", "Gates", "Riaz" };


          and know it'll still compile. Some of the semantics may not be the same, so you still need to be careful - but at least you know it won't be using any array-specific methods.






          share|improve this answer





















          • not C and not Python! *g
            – Henrik P. Hessel
            Aug 22 '09 at 9:37
















          5














          The advantage is that it shows to readers that you're not using the fact that it happens to be an array. I tend to declare local variables as the most general type which contains all the functionality I need. For instance, I'll do:



          using (TextReader reader = File.OpenText(...))


          instead of specifying StreamReader.



          It's rarely hugely important, but it does make it easier to change the details of the implementation. For example, suppose for whatever reason we wanted to change your example to use a List<T> instead of an array.



          You can change this:



          IList<string> _lstColl = new { "Alex", "Sam", "Gates", "Riaz" };


          to this:



          IList<string> _lstColl = new List<string> { "Alex", "Sam", "Gates", "Riaz" };


          and know it'll still compile. Some of the semantics may not be the same, so you still need to be careful - but at least you know it won't be using any array-specific methods.






          share|improve this answer





















          • not C and not Python! *g
            – Henrik P. Hessel
            Aug 22 '09 at 9:37














          5












          5








          5






          The advantage is that it shows to readers that you're not using the fact that it happens to be an array. I tend to declare local variables as the most general type which contains all the functionality I need. For instance, I'll do:



          using (TextReader reader = File.OpenText(...))


          instead of specifying StreamReader.



          It's rarely hugely important, but it does make it easier to change the details of the implementation. For example, suppose for whatever reason we wanted to change your example to use a List<T> instead of an array.



          You can change this:



          IList<string> _lstColl = new { "Alex", "Sam", "Gates", "Riaz" };


          to this:



          IList<string> _lstColl = new List<string> { "Alex", "Sam", "Gates", "Riaz" };


          and know it'll still compile. Some of the semantics may not be the same, so you still need to be careful - but at least you know it won't be using any array-specific methods.






          share|improve this answer












          The advantage is that it shows to readers that you're not using the fact that it happens to be an array. I tend to declare local variables as the most general type which contains all the functionality I need. For instance, I'll do:



          using (TextReader reader = File.OpenText(...))


          instead of specifying StreamReader.



          It's rarely hugely important, but it does make it easier to change the details of the implementation. For example, suppose for whatever reason we wanted to change your example to use a List<T> instead of an array.



          You can change this:



          IList<string> _lstColl = new { "Alex", "Sam", "Gates", "Riaz" };


          to this:



          IList<string> _lstColl = new List<string> { "Alex", "Sam", "Gates", "Riaz" };


          and know it'll still compile. Some of the semantics may not be the same, so you still need to be careful - but at least you know it won't be using any array-specific methods.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 22 '09 at 9:35









          Jon Skeet

          1076k67478728402




          1076k67478728402












          • not C and not Python! *g
            – Henrik P. Hessel
            Aug 22 '09 at 9:37


















          • not C and not Python! *g
            – Henrik P. Hessel
            Aug 22 '09 at 9:37
















          not C and not Python! *g
          – Henrik P. Hessel
          Aug 22 '09 at 9:37




          not C and not Python! *g
          – Henrik P. Hessel
          Aug 22 '09 at 9:37













          2














          This is an example of generics and you are creating an IList with string datatype.




          Generics introduce to the .NET
          Framework the concept of type
          parameters, which make it possible to
          design classes and methods that defer
          the specification of one or more types
          until the class or method is declared
          and instantiated by client code. For
          example, by using a generic type
          parameter T you can write a single
          class that other client code can use
          without incurring the cost or risk of
          runtime casts or boxing operations.




          Read more here



          What is cool about generics, why use them?



          Do C# Generics Have a Perfomance Benefit?






          share|improve this answer























          • sorry ! would have framed my question ,more specific. What is the advantage of initializiing the interface indirectly.(Ofcourse we can not create instance for interface).
            – user160677
            Aug 22 '09 at 9:36










          • I don't think that this question is really about generics.
            – Stefan Steinegger
            Aug 22 '09 at 9:42
















          2














          This is an example of generics and you are creating an IList with string datatype.




          Generics introduce to the .NET
          Framework the concept of type
          parameters, which make it possible to
          design classes and methods that defer
          the specification of one or more types
          until the class or method is declared
          and instantiated by client code. For
          example, by using a generic type
          parameter T you can write a single
          class that other client code can use
          without incurring the cost or risk of
          runtime casts or boxing operations.




          Read more here



          What is cool about generics, why use them?



          Do C# Generics Have a Perfomance Benefit?






          share|improve this answer























          • sorry ! would have framed my question ,more specific. What is the advantage of initializiing the interface indirectly.(Ofcourse we can not create instance for interface).
            – user160677
            Aug 22 '09 at 9:36










          • I don't think that this question is really about generics.
            – Stefan Steinegger
            Aug 22 '09 at 9:42














          2












          2








          2






          This is an example of generics and you are creating an IList with string datatype.




          Generics introduce to the .NET
          Framework the concept of type
          parameters, which make it possible to
          design classes and methods that defer
          the specification of one or more types
          until the class or method is declared
          and instantiated by client code. For
          example, by using a generic type
          parameter T you can write a single
          class that other client code can use
          without incurring the cost or risk of
          runtime casts or boxing operations.




          Read more here



          What is cool about generics, why use them?



          Do C# Generics Have a Perfomance Benefit?






          share|improve this answer














          This is an example of generics and you are creating an IList with string datatype.




          Generics introduce to the .NET
          Framework the concept of type
          parameters, which make it possible to
          design classes and methods that defer
          the specification of one or more types
          until the class or method is declared
          and instantiated by client code. For
          example, by using a generic type
          parameter T you can write a single
          class that other client code can use
          without incurring the cost or risk of
          runtime casts or boxing operations.




          Read more here



          What is cool about generics, why use them?



          Do C# Generics Have a Perfomance Benefit?







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 23 '17 at 12:30









          Community

          11




          11










          answered Aug 22 '09 at 9:29









          rahul

          149k42202246




          149k42202246












          • sorry ! would have framed my question ,more specific. What is the advantage of initializiing the interface indirectly.(Ofcourse we can not create instance for interface).
            – user160677
            Aug 22 '09 at 9:36










          • I don't think that this question is really about generics.
            – Stefan Steinegger
            Aug 22 '09 at 9:42


















          • sorry ! would have framed my question ,more specific. What is the advantage of initializiing the interface indirectly.(Ofcourse we can not create instance for interface).
            – user160677
            Aug 22 '09 at 9:36










          • I don't think that this question is really about generics.
            – Stefan Steinegger
            Aug 22 '09 at 9:42
















          sorry ! would have framed my question ,more specific. What is the advantage of initializiing the interface indirectly.(Ofcourse we can not create instance for interface).
          – user160677
          Aug 22 '09 at 9:36




          sorry ! would have framed my question ,more specific. What is the advantage of initializiing the interface indirectly.(Ofcourse we can not create instance for interface).
          – user160677
          Aug 22 '09 at 9:36












          I don't think that this question is really about generics.
          – Stefan Steinegger
          Aug 22 '09 at 9:42




          I don't think that this question is really about generics.
          – Stefan Steinegger
          Aug 22 '09 at 9:42











          2














          There are no advantages or disadvantages. If your code requires an IList, then give it one. If your code requires an array, then give it one.






          share|improve this answer





















          • There are disadvantages. see my answer.
            – Stefan Steinegger
            Aug 22 '09 at 9:43
















          2














          There are no advantages or disadvantages. If your code requires an IList, then give it one. If your code requires an array, then give it one.






          share|improve this answer





















          • There are disadvantages. see my answer.
            – Stefan Steinegger
            Aug 22 '09 at 9:43














          2












          2








          2






          There are no advantages or disadvantages. If your code requires an IList, then give it one. If your code requires an array, then give it one.






          share|improve this answer












          There are no advantages or disadvantages. If your code requires an IList, then give it one. If your code requires an array, then give it one.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 22 '09 at 9:31









          Christian Hayter

          26.5k56090




          26.5k56090












          • There are disadvantages. see my answer.
            – Stefan Steinegger
            Aug 22 '09 at 9:43


















          • There are disadvantages. see my answer.
            – Stefan Steinegger
            Aug 22 '09 at 9:43
















          There are disadvantages. see my answer.
          – Stefan Steinegger
          Aug 22 '09 at 9:43




          There are disadvantages. see my answer.
          – Stefan Steinegger
          Aug 22 '09 at 9:43











          1














          I don't really understand why an array implements IList. When you call Add, it throws an exception. An array does not have IList semantics, because it is not resizable. That's why I avoid having an array as IList. If I need a list, i would write it like this:



          IList<string> _lstColl = new List<string>() { "Alex", "Sam", "Gates", "Riaz" };


          (PS: There are quite a few problems with using of the collection interfaces of the standard framework, like missing methods and even missing interfaces. When you are working with collection interfaces, as usual for instance when using NHibernate, you recognize that they are sometimes badly designed. The developers of this interfaces didn't seem to use them a lot I think. Just my opinion.)






          share|improve this answer























          • It's because there isn't a separate interface that only has an indexer and a count.
            – Jon Skeet
            Aug 22 '09 at 9:35






          • 1




            Yes, see my PS about collection interfaces. I'm really disappointed about the .NET collection design. I mean, this list types and semantics are well known since decades. Why do they come up with a new framework with such a bad collection design?
            – Stefan Steinegger
            Aug 22 '09 at 9:40
















          1














          I don't really understand why an array implements IList. When you call Add, it throws an exception. An array does not have IList semantics, because it is not resizable. That's why I avoid having an array as IList. If I need a list, i would write it like this:



          IList<string> _lstColl = new List<string>() { "Alex", "Sam", "Gates", "Riaz" };


          (PS: There are quite a few problems with using of the collection interfaces of the standard framework, like missing methods and even missing interfaces. When you are working with collection interfaces, as usual for instance when using NHibernate, you recognize that they are sometimes badly designed. The developers of this interfaces didn't seem to use them a lot I think. Just my opinion.)






          share|improve this answer























          • It's because there isn't a separate interface that only has an indexer and a count.
            – Jon Skeet
            Aug 22 '09 at 9:35






          • 1




            Yes, see my PS about collection interfaces. I'm really disappointed about the .NET collection design. I mean, this list types and semantics are well known since decades. Why do they come up with a new framework with such a bad collection design?
            – Stefan Steinegger
            Aug 22 '09 at 9:40














          1












          1








          1






          I don't really understand why an array implements IList. When you call Add, it throws an exception. An array does not have IList semantics, because it is not resizable. That's why I avoid having an array as IList. If I need a list, i would write it like this:



          IList<string> _lstColl = new List<string>() { "Alex", "Sam", "Gates", "Riaz" };


          (PS: There are quite a few problems with using of the collection interfaces of the standard framework, like missing methods and even missing interfaces. When you are working with collection interfaces, as usual for instance when using NHibernate, you recognize that they are sometimes badly designed. The developers of this interfaces didn't seem to use them a lot I think. Just my opinion.)






          share|improve this answer














          I don't really understand why an array implements IList. When you call Add, it throws an exception. An array does not have IList semantics, because it is not resizable. That's why I avoid having an array as IList. If I need a list, i would write it like this:



          IList<string> _lstColl = new List<string>() { "Alex", "Sam", "Gates", "Riaz" };


          (PS: There are quite a few problems with using of the collection interfaces of the standard framework, like missing methods and even missing interfaces. When you are working with collection interfaces, as usual for instance when using NHibernate, you recognize that they are sometimes badly designed. The developers of this interfaces didn't seem to use them a lot I think. Just my opinion.)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 22 '09 at 9:40

























          answered Aug 22 '09 at 9:33









          Stefan Steinegger

          54k13110178




          54k13110178












          • It's because there isn't a separate interface that only has an indexer and a count.
            – Jon Skeet
            Aug 22 '09 at 9:35






          • 1




            Yes, see my PS about collection interfaces. I'm really disappointed about the .NET collection design. I mean, this list types and semantics are well known since decades. Why do they come up with a new framework with such a bad collection design?
            – Stefan Steinegger
            Aug 22 '09 at 9:40


















          • It's because there isn't a separate interface that only has an indexer and a count.
            – Jon Skeet
            Aug 22 '09 at 9:35






          • 1




            Yes, see my PS about collection interfaces. I'm really disappointed about the .NET collection design. I mean, this list types and semantics are well known since decades. Why do they come up with a new framework with such a bad collection design?
            – Stefan Steinegger
            Aug 22 '09 at 9:40
















          It's because there isn't a separate interface that only has an indexer and a count.
          – Jon Skeet
          Aug 22 '09 at 9:35




          It's because there isn't a separate interface that only has an indexer and a count.
          – Jon Skeet
          Aug 22 '09 at 9:35




          1




          1




          Yes, see my PS about collection interfaces. I'm really disappointed about the .NET collection design. I mean, this list types and semantics are well known since decades. Why do they come up with a new framework with such a bad collection design?
          – Stefan Steinegger
          Aug 22 '09 at 9:40




          Yes, see my PS about collection interfaces. I'm really disappointed about the .NET collection design. I mean, this list types and semantics are well known since decades. Why do they come up with a new framework with such a bad collection design?
          – Stefan Steinegger
          Aug 22 '09 at 9:40


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1315603%2fc-sharp-interface-type-initialization%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python