What is an Array?

πŸ’‘ Concept Name

Array β€” A classic, fixed-size collection of elements of the same type, stored in back-to-back memory locations for fast access by index.

πŸ“˜ Quick Intro

An array lets you store a list of values (like numbers or names) in a single variable, where each value has its own spot (called an index). Indexing always starts at zero, not one.

🧠 Analogy / Short Story

Imagine an apartment block with a row of mailboxes. Every mailbox has a number starting from 0, and you can quickly check what's inside any mailbox just by knowing its number. Arrays work exactly like this β€” direct, no-nonsense access.

πŸ”§ Technical Explanation

  • πŸ—ƒοΈ Arrays in C# are zero-indexed, so array[0] gives you the first element.
  • πŸ“ They are fixed in size β€” you decide the length up front and can’t resize it later (unless you create a new array).
  • 🧠 Every element must be the same type (e.g., all int, all string).
  • ⚑ You can access or modify any value instantly by its index.
  • πŸ”’ In .NET, arrays are objects and are stored on the managed heap (not the stack).

🎯 Purpose & Use Case

  • βœ… Store a list of scores, names, or objects when you know exactly how many you need.
  • βœ… Use for 2D data like a game board or matrix.
  • βœ… Great for repetitive calculations, searching, or sorting where quick access matters.

πŸ’» Real Code Example

// Declaring and using an array
int[] scores = new int[5] { 90, 85, 88, 76, 95 };

// Accessing first value
Console.WriteLine(scores[0]); // Output: 90

// Changing the second value
scores[1] = 100; // Now scores[1] is 100

// Printing all values
foreach (int score in scores)
{
    Console.WriteLine(score);
}

❓ Interview Q&A

Q1: What is an array?
A: A collection of elements stored in contiguous memory with a fixed size.

Q2: Can you resize an array?
A: No, arrays have a fixed size after creation.

Q3: What is the index of the first element?
A: 0

Q4: Are arrays type-safe?
A: Yes, all elements must be of the declared type.

Q5: Are arrays reference types?
A: Yes, arrays are objects in .NET.

πŸ“ MCQs

Q1. What is the index of the first element in an array?

  • 1
  • 0
  • -1
  • Depends

Q2. Can arrays be resized after declaration?

  • Yes
  • No
  • Only in C++
  • Only using ref keyword

Q3. What type of elements can an array hold?

  • Only one type
  • Mixed types
  • Any object
  • Primitive types only

Q4. Where are arrays stored in memory?

  • Stack
  • Managed heap
  • Static memory
  • Global space

Q5. How is an element in an array accessed?

  • By value
  • By reference
  • By index
  • By name

πŸ’‘ Bonus Insight

Arrays are perfect when you know the number of items won’t change. If you want something that grows and shrinks as needed, check out List<T> β€” it’s like an array’s flexible cousin, though a tiny bit slower for raw access.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: