Declaring Arrays
Create and fill an array.
Declaring Arrays is a free C# Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is an Array?
An array stores many values of the same type in one variable. Instead of score1, score2, score3, you keep all scores together.
Arrays have a fixed size that you set when you create them. They are perfect for lists like days of the week or test results.
Declaring an Array Type
To declare an array you write the element type followed by square brackets [], then the name.
This just creates the variable. It does not hold any values yet, so it is null until you give it an array.
int[] numbers;
string[] names;Creating With new
Use the new keyword with a size to actually build the array in memory.
The number in brackets is the length. Here numbers can hold 5 integers, all starting at the default value 0.
int[] numbers = new int[5];
Console.WriteLine(numbers[0]);Default Values
A new array fills every slot with the type's default value. For numbers that is 0, for bool it is false, and for string (and other objects) it is null.
You do not have to set values to start using the array.
bool[] flags = new bool[3];
Console.WriteLine(flags[0]); // FalseArray Initializer
You can fill an array with values at creation using curly braces { }. This is called an array initializer.
The length is set automatically from the number of values you list, so you do not need to write the size.
int[] scores = new int[] { 90, 85, 100 };
Console.WriteLine(scores[2]); // 100Shorter Initializer Syntax
When you declare and initialize in one line, you can drop new int[] and keep only the braces.
This shorter form is very common in everyday C# code.
string[] fruits = { "apple", "pear", "plum" };
Console.WriteLine(fruits[1]); // pearA Full Program
Let's put it together in a complete program. We declare a string array of colors and print the first one.
Run it to see the output appear in the console.
string[] colors = { "red", "green", "blue" };
Console.WriteLine("First color: " + colors[0]);Arrays Hold One Type
Every element in an array must match the declared type. An int[] can only hold integers, and a string[] can only hold strings.
Trying to put the wrong type inside causes a compile error, which keeps your data consistent.
double[] prices = { 9.99, 19.5, 4.0 };
Console.WriteLine(prices[1]); // 19.5Using var
You can let C# infer the type with var when the initializer makes the type clear.
Here C# sees integers in the braces, so ages becomes an int[] automatically.
var ages = new[] { 18, 25, 40 };
Console.WriteLine(ages[0]); // 18Setting Values by Index
After creating an empty array you can assign values one slot at a time using the index.
The index starts at 0, so the first slot is [0] and the second is [1].
int[] nums = new int[2];
nums[0] = 10;
nums[1] = 20;
Console.WriteLine(nums[1]); // 20Empty Arrays
An array can have length 0. It exists but holds no elements. This is useful as a safe starting point instead of null.
You can create one with new int[0] or the shorthand Array.Empty<int>().
int[] empty = new int[0];
Console.WriteLine(empty.Length); // 0Quick Check
Test what you learned about declaring arrays.
Recap
You learned to declare arrays with type[], build them using new type[size], and fill them with initializers like { 1, 2, 3 }.
New arrays start with default values, every element shares one type, and the length is fixed when created. Next you will read and measure these values.
Frequently asked questions
Is the “Declaring Arrays” lesson free?
Yes — the full text of “Declaring Arrays” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Declaring Arrays”?
Create and fill an array. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Declaring Arrays” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Arrays
- Indexing and Length
- Looping Over Arrays
- Multidimensional Arrays