Inline arrays in c#

Inline Arrays: 

In C#, you can use inline arrays by declaring and initializing them directly within your code. Here are a few examples of how you can do this in different scenarios:

1. Basic Inline Array Initialization

You can initialize an array inline with a list of values using curly braces {}:

int[] numbers = { 1, 2, 3, 4, 5 }; this creates an integer array with 5 elements.

2. Inline Array with Explicit Type Declaration

You can also use the new keyword if you want to be explicit:

int[] numbers = new int[] { 1, 2, 3, 4, 5 };

Both the above examples will give you the same result.

3. Inline Array for Method Parameters

You can pass an inline array directly to a method that expects an array:

void PrintNumbers(int[] nums) {   foreach (int num in nums) {

 Console.WriteLine(num);  } } 

PrintNumbers(new int[] { 10, 20, 30 }); // Passing inline array

4. Inline Array in LINQ Queries

If you're using LINQ, you can also use inline arrays:

var evenNumbers = (new int[] { 1, 2, 3, 4, 5, 6 }).Where(n => n % 2 == 0);

foreach (var num in evenNumbers)

{   Console.WriteLine(num); }


5. Inline Array with params Keyword

If a method uses the params keyword, you can pass an inline array without the new keyword:

void SumNumbers(params int[] numbers)

{

    int sum = numbers.Sum();

    Console.WriteLine($"Sum: {sum}");

}

SumNumbers(1, 2, 3, 4); // Passing inline array directly


6. Using Inline Arrays in Object Initializers

For complex types, you can also use inline arrays with object initializers:

var people = new[]

{

    new { Name = "Alice", Age = 30 },

    new { Name = "Bob", Age = 25 }

};


foreach (var person in people)

{

    Console.WriteLine($"{person.Name}, {person.Age}");

}


7. Using Array Class Methods

If you need to initialize an array and then work with it immediately, you can use Array class methods:

Array.ForEach(new int[] { 1, 2, 3 }, Console.WriteLine);

This code prints each element of the array using Console.WriteLine.

8. Inline Multidimensional Arrays

If you need a multidimensional array, you can also initialize it inline:

int[,] matrix = { { 1, 2 }, { 3, 4 }, { 5, 6 } };

for (int i = 0; i < matrix.GetLength(0); i++)

{

    for (int j = 0; j < matrix.GetLength(1); j++)

    {

        Console.Write(matrix[i, j] + " ");

    }

    Console.WriteLine();

}

9. Using List<T> with Inline Initialization

If you prefer to work with lists instead of arrays, you can use inline collection initializers:

var fruits = new List<string> { "Apple", "Banana", "Cherry" };

fruits.ForEach(Console.WriteLine);


Summary

  • Use {} for quick inline initialization.

  • Use new[] { ... } for type inference with anonymous types.

  • Use params for passing arrays directly into methods.

  • Use Array.ForEach or LINQ for processing inline arrays.

  • Lists can also be initialized inline using collection initializers.

These are some of the ways you can use inline arrays in C#. Let me know if you have a specific use case in mind!


Comments

Popular posts from this blog

Performance Optimization in Sitecore

Strategies for Migrating to Sitecore from legacy or upgrading from older Sitecore

Azure Event Grid Sample code