Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# Arrays - Real-Life Examples


Share

Real-Life Examples

An array stores many values in one variable, and a loop lets you go through them one by one.

Together, they can answer questions about a whole group of values, like "What is the average age?" or "How many students passed the test?"


Average Age

To find the average, add up all the values, and divide the sum by the number of values:

Example

int[] ages = {20, 22, 18, 35, 48, 26, 87, 70};
double sum = 0;

foreach (int age in ages)
{
  sum = sum + age;
}

double average = sum / ages.Length;
Console.WriteLine("The average age is " + average);

Try it Yourself »

The ages add up to 326, and the array has 8 elements, so the average is 326 / 8 = 40.75.

sum is a double, so the division keeps the decimals. With an int, the result would be 40.


Lowest Age

To find the lowest value, start with the first element, and compare it with every element in the array:

Example

int[] ages = {20, 22, 18, 35, 48, 26, 87, 70};
int lowestAge = ages[0];

foreach (int age in ages)
{
  if (age < lowestAge)
  {
    lowestAge = age;
  }
}

Console.WriteLine("The lowest age is " + lowestAge);

Try it Yourself »

Each time the loop finds a lower age, it saves that age in lowestAge. At the end, lowestAge is 18.



List of Test Scores

Two arrays can belong together. Here, names[0] and scores[0] are about the same student, and so on:

Example

string[] names = {"Liam", "Jenny", "Angie", "John", "Emma"};
int[] scores = {85, 42, 77, 91, 58};

for (int i = 0; i < names.Length; i++)
{
  Console.WriteLine(names[i] + ": " + scores[i]);
}

Try it Yourself »

The for loop uses the same index i for both arrays, so each name is printed with the right score.


Count Passed Students

A loop and an if statement can count how many values match a condition. In this example, a score of 50 or more passes the test:

Example

int[] scores = {85, 42, 77, 91, 58};
int passed = 0;

foreach (int score in scores)
{
  if (score >= 50)
  {
    passed++;
  }
}

Console.WriteLine(passed + " of " + scores.Length + " students passed the test.");

Try it Yourself »

Only 42 is less than 50, so the program prints "4 of 5 students passed the test."


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.