banner



How To Create String Array In C#

A string array in C# can be created, initialized, and assigned values as described below.

Declaring a string type array:

string[] strArr;

Initializing the string array:

As array in C# is a reference type, the new keyword is used to create an array instance:

string[] strArr = new string[5];

Assigning values at the time of declaration:

string[] strArrCities = new string[] { "New York", "London", "Berlin", "Paris", "Dubai" };

You may also assign values as follows:

string [ ] strArr = new string [ 5 ] ;

strArr [ 0 ] = "New York" ;

strArr [ 1 ] = "London" ;

strArr [ 2 ] = "Berlin" ;

strArr [ 3 ] = "Paris" ;

strArr [ 4 ] = "Dubai" ;

The examples below shows declaring, initializing and accessing string arrays in different ways.

An example of a string array

In this example, an array is declared and initialized without specifying the array size. The values are also assigned at the time of declaration. Then, a foreach loop is used to iterate through the array elements and displaying values:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

using System ;

class string_array_demo

{

static void Main ( )

{

//Declaring and initializing a string array

string [ ] strArrFruit = new string [ ] { "Mango" , "Apple" , "Strawberry" , "Cherry" , "Pine Apple" } ;

Console . WriteLine ( "****The Fruit Names in Strig Array:****\n" ) ;

foreach ( string fru in strArrFruit )

{

Console . WriteLine ( "\t" + fru ) ;

}

Console . ReadLine ( ) ;

}

}

c# string-array

Declaring a string array with size example

In this string array example, we have declared an array of five size. After declaration, the values are assigned by using the index number and then foreach loop is used to display array elements:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

using System ;

class string_array_demo

{

static void Main ( )

{

//Declaring and initializing a string array

string [ ] ArrAnimals = new string [ 5 ] ;

ArrAnimals [ 0 ] = "Lion" ;

ArrAnimals [ 1 ] = "Monkey" ;

ArrAnimals [ 2 ] = "Donkey" ;

ArrAnimals [ 3 ] = "Bear" ;

ArrAnimals [ 4 ] = "Wolf" ;

Console . WriteLine ( "****Animals in Strig Array:****\n" ) ;

foreach ( string ani in ArrAnimals )

{

Console . WriteLine ( "\t" + ani ) ;

}

Console . ReadLine ( ) ;

}

}

c# string-array-animals

Using for loop to iterate through a string array

The example below shows using a for loop to iterate through the array elements and displaying on the screen. For the condition part of the for loop, I used the length property of Array as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

using System ;

class array_for_loop

{

static void Main ( )

{

string [ ] Arrstr = new string [ 5 ] ;

Arrstr [ 0 ] = "abc" ;

Arrstr [ 1 ] = "def" ;

Arrstr [ 2 ] = "jhi" ;

Arrstr [ 3 ] = "jkl" ;

Arrstr [ 4 ] = "xyz" ;

for ( int l = 0 ; l < Arrstr . Length ; l++)

{

Console . WriteLine ( Arrstr [ l ] ) ;

}

Console . ReadLine ( ) ;

}

}

How to convert a string array to string

One of the ways for converting an array to string is using the join method. The join is the method of string class where you may specify the array as shown in the example below:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

using System ;

class array_to_string

{

static void Main ( )

{

string [ ] Arrstr = new string [ 5 ] ;

Arrstr [ 0 ] = "This" ;

Arrstr [ 1 ] = "is" ;

Arrstr [ 2 ] = "String" ;

Arrstr [ 3 ] = "Array" ;

Arrstr [ 4 ] = "Tutorial" ;

Console . WriteLine ( string . Join ( " " , Arrstr ) ) ;

Console . ReadLine ( ) ;

}

}

The output of the above code is:

This is String Array Tutorial

This div height required for enabling the sticky sidebar

How To Create String Array In C#

Source: https://www.jquery-az.com/c-sharp-string-array/

Posted by: johnsonwhowerromed56.blogspot.com

0 Response to "How To Create String Array In C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel