Skip to main content

C# String Array Examples


C# String Array

1.  Declaring string arrays

~~~ Program that initializes string arrays (C#) ~~

class Program
{
static void Main()
{
// String arrays with 3 elements:
string[] arr1 = new string[] { "one", "two", "three" }; // 1
string[] arr2 = { "one", "two", "three" }; // 2
var arr3 = new string[] { "one", "two", "three" }; // 3

string[] arr4 = new string[3]; // 4
arr4[0] = "one";
arr4[1] = "two";
arr4[2] = "three";
}
}

Output of the program
 (Four string arrays are initialized.)

2.  String arrays at class level

class Program
{
static void Main()
{
Test test = new Test(); // Create new instance with string array
foreach (string element in test.Elements)
// Loop over elements with property
{
System.Console.WriteLine(element);
}
System.Console.WriteLine(test[0]); // Get first string element
}
}
public class Test
{
/// String array field instance.
string[] _elements = { "one", "two", "three" };
/// String array property getter.
public string[] Elements
{
get { return _elements; }
}
/// String array indexer.
public string this[int  index]
{
get { return _elements[index]; }
}
}

Output of the program
one
two
three
one

Comments

Popular posts from this blog

Collections in Java

Collections in Java Introduction to Collections Framework The Collections Framework provides a well defined set of interfaces and classes for storing and manipulating the groups of data into a single unit. Introduction to Collections Framework Collections Framework: The Collections Framework provides a well-designed set of interfaces and classes for storing and manipulating the groups of data into a single unit. The collections framework is a unified architecture which is used to represent and manipulate collections. The framework allows the collections to get manipulated independently, additionally it reduces the programming efforts and increases performance. It includes implementation of interfaces and algorithms. Basically it is a unified architecture that consists the following collections: Interfaces: These are the abstract data types that represent collections. With the help of interfaces we manipulate collections independently. A hierarchy is generally formed ...

Basic Java Interview Questions

1. Why java does not support multiple inheritance ? Answer :- To say why java doesn't support inheritance directly like c++ should be the diamond problem faced by c++,(Diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden this method differently, then via which class does it inherit: B, or C?) 2. JVM crash is an Error or Exception.? Answer:- JVM crash is an Error as a normal developer as we cannot handle it, but its exception for JVM developer as they can fix it. 3. Print “Hello World!” Without Main Method in Java? Answer:- public class Testing {    static    {        System.out.println("Hello World");    }    public static void main(String[] args)    {    } } ...

Reading and Writing Properties file in Java

Reading and Writing Properties file Java import java.io.FileInputStream; import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.Set; public class App {     @SuppressWarnings("rawtypes") public static void main( String[] args )     {           Properties prop = new Properties();         try {          // Loading Property file          // This method to load file in Simple Java Application.                 prop.load(new FileInputStream("messages.properties"));                                 ...