Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8272317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:05:34+00:00 2026-06-08T07:05:34+00:00

What’s the difference between these two, and what is the correct one? public interface

  • 0

What’s the difference between these two, and what is the “correct” one?

public interface IMessage
{
    /// <summary>
    /// Array used to hold all bytes that will be written.
    /// </summary>
    IList Buffer { get; set; }
}

and

public interface IMessage<T> where T : IList
{
    /// <summary>
    /// Array used to hold all bytes that will be written.
    /// </summary>
    T Buffer { get; set; }
}

Edit 1: Fixed – can’t have fields on interfaces. (Thanks BoltClock)
Edit 2: Fixed – can’t have encapsulation on interfaces. (Thanks KeithS)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:05:36+00:00Added an answer on June 8, 2026 at 7:05 am

    In the first case, your buffer is defined to be of type IList. It can, at runtime, be assigned any implementation of IList, and any creator of implementations of this interface does not have to know exactly what implementation will be used. However, code cannot know, at runtime, which exact concrete implementation is used, and so at design-time when referring to this property as a member of the interface, you will never have access to any methods not exposed explicitly by the IList interface.

    In the second case, you are defining a generic. Wherever this generic definition remains “open” (T is not defined), you are still restricted to using the methods of IList. However, something must close this generic, either an implementation of the interface or the definition of a particular instance of an implementation that keeps the generic open. Once the generic is closed, the exact, concrete type of buffer is known, and methods can be called on it that are not necessarily defined by IList. But, once the generic is closed, an instance or class that defines a particular generic type can’t be given a different implementation of IList to use as the buffer.

    So, some examples (assuming that the protected field, which you can’t specify on an interface, is actually a mutable public property):

    //this class implements the non-generic interface, so buffer is an IList.
    class MyMessage1: IMessage
    {
       public IList buffer {get;set;}
    
       public MyMessage1()
       {
          buffer = new List<string>();
    
          //even though you "know" what you just assigned, 
          //you cannot refer to buffer as a List<string>, even here.
          buffer.Sort(); //error
       }
    }
    
    ...
    
    //The exact type of buffer cannot be known statically, 
    //so only non-generic IList methods are allowed
    var myMessage = new MyMessage1();
    myMessage.buffer.Add("my message"); //valid; string literals are Objects
    var firstLen = myMessage.buffer[0].Length; //error: indexer returns Objects.
    myMessage.Sort(); //error: IList does not have a Sort() method.
    firstLen = GetFirstLength(myMessage); //error: not an IMessage<List<string>>
    //but, an IList is an IList no matter what, so this works.
    myMessage.buffer = new List<int>(); 
    
    ...
    
    //this class keeps the generic open so T can be any IList, determined at instantiation.
    class MyMessage2<T>:IMessage<T> where T:IList
    {
        public T buffer {get;set;}
    
        //buffer's exact type is still not known here,
        //so inside this class you are still restricted to IList members only
        public int BufferCount{get{return buffer.Count;}}
    
        public void SortBuffer()
        {
           buffer.Sort(); //error; no such method
        }    
    }
    
    ...
    
    //but, once you define an instance, you know exactly what buffer is
    var myMessage = new MyMessage2<List<string>>();
    myMessage.buffer.Add("my message");
    var firstLen = myMessage.buffer[0].Length; //now we know the indexer produces strings.
    myMessage.buffer.Sort(); //buffer is known to be a List<T> which has Sort()
    firstLen = GetFirstLength(myMessage);
    
    ...
    
    //and when you pass it as a parameter, you can close the generic of the interface
    public string GetFirstLength(IMessage<List<string>> message) 
    {
       //...so you still know what you're dealing with
       return message.buffer[0].Length;
    }
    
    ...
    
    //however, buffer is now "strongly typed" and the implementation can't change
    myMessage.buffer = new List<int>(); //error; buffer is of type List<string>
    
    ...
    
    //this class closes the generic within the declaration.
    class MyMessage3:IMessage<IList<string>>
    {
       //now we're closing the generic in the implementation itself,
       //so internally we know exactly what we're dealing with
       public List<string> buffer {get;set;}
    
       //...so this call is valid
       public void SortBuffer() { buffer.Sort(); }
    }
    
    //...and consuming code doesn't have to (get to?) specify the implementation of T
    var myMessage = new MyMessage3();
    //... but still knows exactly what that implementation is
    myMessage.buffer.Add("my message");
    var firstLen = myMessage.buffer[0].Length;
    myMessage.buffer.Sort();
    
    //and btw, MyMessage3 is still an IMessage<List<string>>
    firstLen = GetFirstLength(myMessage);
    
    //... and buffer's still a strongly-typed List<string>
    myMessage.buffer = new List<int>(); //error
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.