Sponsored Ad

Friday, November 27, 2009

C# String Array

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#)

Code Snippet
  1. class Program
  2. {
  3.     static void Main()
  4.     {
  5.         // String arrays with 3 elements:
  6.         string[] arr1 = new string[] { "one", "two", "three" }; // A
  7.         string[] arr2 = { "one", "two", "three" };              // B
  8.         var arr3 = new string[] { "one", "two", "three" };      // C
  9.         string[] arr4 = new string[3]; // D
  10.         arr4[0] = "one";
  11.         arr4[1] = "two";
  12.         arr4[2] = "three";
  13.     }
  14. }

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#)

Code Snippet
  1. class Program
  2. {
  3.     static void Main()
  4.     {
  5.         Test test = new Test(); // Create new instance with string array
  6.         foreach (string element in test.Elements) // Loop over elements with property
  7.         {
  8.             System.Console.WriteLine(element);
  9.         }
  10.         System.Console.WriteLine(test[0]); // Get first string element
  11.     }
  12. }
  13. public class Test
  14. {
  15.     /// <summary>
  16.     /// String array field instance.
  17.     /// </summary>
  18.     string[] _elements = { "one", "two", "three" };
  19.  
  20.     /// <summary>
  21.     /// String array property getter.
  22.     /// </summary>
  23.     public string[] Elements
  24.     {
  25.         get { return _elements; }
  26.     }
  27.  
  28.     /// <summary>
  29.     /// String array indexer.
  30.     /// </summary>
  31.     public string this[int index]
  32.     {
  33.         get { return _elements[index]; }
  34.     }
  35. }
  36.  
  37. === Output of the program ===
  38. one
  39. two
  40. three
  41. 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

Sponsored Ad



More Related Articles

Website Update

Recent Posts

Followers