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 6340599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:52:00+00:00 2026-05-24T19:52:00+00:00

Java has Collections.singletonList(T) which returns a List<T> of exactly one element. Is there something

  • 0

Java has Collections.singletonList(T) which returns a List<T> of exactly one element. Is there something similar in C# that returns an IList?

  • 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-05-24T19:52:00+00:00Added an answer on May 24, 2026 at 7:52 pm

    To answer your question, no. Sadly there is nothing built in, although it would often be useful when working with IEnumerable. You’ll have to roll your own.

    Update

    Instead of using workarounds, here’s an example of an efficient and immutable SingletonList that implements IList<T>:

    Usage

    SingletonList<int> bling = new SingletonList<int>(10);    
    

    Code

    public class SingletonList<T> : IList<T>
    {
        private readonly T _item;
    
        public SingletonList(T item)
        {
            _item = item;
        }
    
        public IEnumerator<T> GetEnumerator()
        {
            yield return _item;
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    
        public void Add(T item)
        {
            throw new NotSupportedException("Add not supported.");
        }
    
        public void Clear()
        {
            throw new NotSupportedException("Clear not supported.");
        }
    
        public bool Contains(T item)
        {
            if (item == null) return _item == null;
    
            return item.Equals(_item);
        }
    
        public void CopyTo(T[] array, int arrayIndex)
        {
            if (array == null) throw new ArgumentNullException("array");
    
            array[arrayIndex] = _item;
        }
    
        public bool Remove(T item)
        {
            throw new NotSupportedException("Remove not supported.");
        }
    
        public int Count
        {
            get { return 1; }
        }
    
        public bool IsReadOnly
        {
            get { return true; }
        }
    
        public int IndexOf(T item)
        {
            return Contains(item) ? 0 : -1;
        }
    
        public void Insert(int index, T item)
        {
            throw new NotSupportedException("Insert not supported.");
        }
    
        public void RemoveAt(int index)
        {
            throw new NotSupportedException("RemoveAt not supported.");
        }
    
        public T this[int index]
        {
            get
            {
                if (index == 0) return _item;
    
                throw new IndexOutOfRangeException();
            }
            set { throw new NotSupportedException("Set not supported."); }
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I want an empty map or a one element map, Java Collections has
The Java Collections class has the following method: static <T> List<T> nCopies(int n, T
Java has the thread dump which is triggered by a signal 3 sent to
Java has a lot of frameworks / APIs that help you do logging in
Which static analysis tools for Java has easiest extension mechanism. I checked PMD But
I'm using a new work computer that has an old sdk, Java 1.3.1, on
I need a Java data structure that has: fast (O(1)) insertion fast removal fast
I have a web app that has to do something every, let's say first
I have a singleton class, that has a map which can be accessed by
Java has a tendency to create a large number objects that needs to be

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.