Subscribe to our YouTube Channel for more Videos

C# Tips and Tricks #21 – Array Initializations

Here’s the list of some of the possible ways of array initializations in C#

// Array Initializations in C#
//1. Creates an array if 2 items with default values
string[] array1 = new string[2]; 

//2. Creates an array of 2 items with 2 values
string[] array2 = new string[] { "Senthil", "Balu" };

//3. Creates an array of 2 items with 2 values
string[] array3 = { "Senthil", "Balu" }; 

//4. Creates an array of 2 items
string[] array4 = new[] { "Senthil", "Balu" };

//5. Initialize Array with a fixes array if pre-initialized values (Empty strings)
var array = Enumerable.Repeat(string.Empty, 10).ToArray();
Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.