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,924 questions

51,857 answers

573 users

How to use string array in class with C#

2 Answers

0 votes
using System;

public class CAString {
    string[] array = { "Blade Runner", "The Matrix", "Star Wars", "The Terminator" };
         
    public string[] Elements {
        get { return array; }
    }
 
    public string this[int index] {
        get { return array[index]; }
    }
}

class Program {
    static void Main(string[] args) {
        CAString o = new CAString();
 
        foreach (string element in o.Elements) {
            Console.WriteLine(element);
        }
 
        Console.WriteLine();
        Console.WriteLine(o[0]);
        Console.WriteLine(o[1]);
    }
}
 
 
 
/*
run:
 
Blade Runner
The Matrix
Star Wars
The Terminator

Blade Runner
The Matrix
 
*/

 



answered Aug 4, 2018 by avibootz
edited May 13, 2024 by avibootz
0 votes
using System;

public class CAString {
    string[] array = { "Blade Runner", "The Matrix", "Star Wars", "The Terminator" };
         
   // property
    public string[] StringElements {
        get { return array; }
    }
 
    // indexer
    public string this[int index] {
        get { return array[index]; }
        set { array[index] = value; }
    }
}

class Program {
    static void Main(string[] args) {
        CAString o = new CAString();
 
        foreach (string s in o.StringElements) {
            Console.WriteLine(s);
        }
 
        Console.WriteLine();
 
        o[0] = "c#";
 
        foreach (string s in o.StringElements) {
            Console.WriteLine(s);
        }
    }
}
 
 
 
/*
run:
 
Blade Runner
The Matrix
Star Wars
The Terminator

c#
The Matrix
Star Wars
The Terminator
 
*/

 



answered May 13, 2024 by avibootz

Related questions

1 answer 138 views
138 views asked Jul 23, 2014 by avibootz
1 answer 91 views
1 answer 150 views
1 answer 122 views
1 answer 153 views
1 answer 132 views
...