Trouble. You have a set of string values that you need to store in a string array using C # programming language. There are different ways of declaring arrays, and you want to view. Solution. Here we can see different ways to declare and use string arrays in C #.
1. Declaring string array
First, here we see that there are several ways to declare and instantiate a String [ ] array variable local. They are equivalent in the compiled code [see footnote], so choose the one you think is clearer to read.
Program that initializes string arrays (C#)
- class Program
- {
- static void Main()
- {
- // String arrays with 3 elements:
- string[] arr1 = new string[] { "one", "two", "three" }; // A
- string[] arr2 = { "one", "two", "three" }; // B
- var arr3 = new string[] { "one", "two", "three" }; // C
- string[] arr4 = new string[3]; // D
- arr4[0] = "one";
- arr4[1] = "two";
- arr4[2] = "three";
- }
- }
Description: The main function above shows four string [ ] arrays, each equivalent to the compiler. The biggest difference is that the first three sets are declared in a single line, while the fourth series is given in separate statements. The fourth matrix allows you to test each value or logic of integration as assigned.
1. String arrays at class level
How to use String [ ] arrays as properties in classes. it is useful for storing values, either static or in cases.it return arrays of strings with the methods. Elements of string can also be returned with an indexer.
Program that uses string array (C#)
- 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
- {
- /// <summary>
- /// String array field instance.
- /// </summary>
- string[] _elements = { "one", "two", "three" };
- /// <summary>
- /// String array property getter.
- /// </summary>
- public string[] Elements
- {
- get { return _elements; }
- }
- /// <summary>
- /// String array indexer.
- /// </summary>
- public string this[int index]
- {
- get { return _elements[index]; }
- }
- }
- === Output of the program ===
- one
- two
- three
- one
Description: The first part of the code is the main method, which is the entry point of the program. A new instance of test class is created. Internally, the class that contains an array of strings. The kind of test that in real life, change their internal strings.
Trial lesson.:The test class contains a String array field. The array elements are actually added in the constructor of the class, the C # compiler automatically generated.
The second part of the test class is access to the property. It provides a clean source of external access to internal array.This properties are not useful in many cases,
This int [ ] code. Final part of the class of test is called an indexer. This is basically a function that receives one parameter, an integer, and returns a value based on that parameter.
0 comments:
Post a Comment