Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,933 questions

51,870 answers

573 users

How to create a list containing a range of characters in C#

2 Answers

0 votes
using System;
using System.Linq;
using System.Collections.Generic;
 
class Program
{
    static List<char> CreateCharacterRangeList(char startChar, char endChar) {
        if (endChar < startChar) {
            throw new ArgumentException("End character must be greater than or equal to start character.");
        }
 
        return Enumerable.Range(startChar, endChar - startChar + 1)
                         .Select(i => (char)i)
                         .ToList();
    }
 
    static void PrintCharacters(List<char> charList) {
        foreach (char ch in charList) {
            Console.Write(ch + " ");
        }
        Console.WriteLine();
    }
 
    static void Main()
    {
        char start = 'a';
        char end = 'm';
 
        List<char> charList = CreateCharacterRangeList(start, end);
         
        PrintCharacters(charList);
    }
}
 
 
 
/*
run:
 
a b c d e f g h i j k l m 
 
*/

 



answered Mar 21, 2025 by avibootz
edited Mar 21, 2025 by avibootz
0 votes
using System;
using System.Collections.Generic;
 
class Program
{
    static List<char> CreateCharacterRangeList(char start, char end) {
        List<char> charList = new List<char>();
         
        for (char ch = start; ch <= end; ch++) {
            charList.Add(ch);
        }
         
        return charList;
    }
 
    static void PrintCharacters(List<char> charList) {
        foreach (char ch in charList) {
            Console.Write(ch + " ");
        }
        Console.WriteLine();
    }
 
    static void Main()
    {
        char start = 'a';
        char end = 'm';
 
        List<char> charList = CreateCharacterRangeList(start, end);
         
        PrintCharacters(charList);
    }
}
 
 
 
/*
run:
 
a b c d e f g h i j k l m 
 
*/

 



answered Mar 21, 2025 by avibootz
edited Mar 21, 2025 by avibootz

Related questions

...