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# Errors


Share

Errors

Everyone makes errors when they write code, even experienced programmers. An error in a program is also called a bug.

Errors are a normal part of programming. What matters is that you can find them and fix them.

There are three kinds of errors in C#:

  • Compile errors: C# finds them before the program runs. The program does not start.
  • Runtime errors: the program starts, but it stops with an error while it runs.
  • Logic errors: the program runs, but it gives the wrong answer.

Compile Errors

Before a program runs, the C# compiler reads your code and turns it into a program that the computer can run.

If the compiler finds a mistake, it shows an error message, and the program does not run at all. This is called a compile error.

Here are three common compile errors.

Missing Semicolon

Every statement must end with a semicolon ;. Here, the semicolon is missing after Console.WriteLine(x):

Example

int x = 5;
Console.WriteLine(x)

The compiler shows an error like this:

prog.cs(11,5): error CS1002: ; expected

Try it Yourself »

To fix it, add the semicolon at the end of the line: Console.WriteLine(x);

Unknown Variable

You must create a variable before you can use it. Here, the variable score was never created:

Example

Console.WriteLine(score);

The compiler shows an error like this:

prog.cs(9,25): error CS0103: The name `score' does not exist in the current context

Try it Yourself »

To fix it, create the variable first, for example int score = 50;. Also check the spelling: score and Score are two different names in C#.

Wrong Type

A value must match the type of the variable. Here, "25" is a string (text in double quotes), but age is an int:

Example

int age = "25";
Console.WriteLine(age);

The compiler shows an error like this:

prog.cs(9,17): error CS0029: Cannot implicitly convert type `string' to `int'

Try it Yourself »

"Cannot implicitly convert" means that C# cannot change the string into an int by itself. To fix it, remove the double quotes: int age = 25;


How to Read an Error Message

An error message can look strange at first, but it always has the same parts. Look at the message from the example above:

prog.cs(9,25): error CS0103: The name `score' does not exist in the current context
PartMeaning
prog.csThe file with the error. The Try it Yourself editor calls your file prog.cs. In Visual Studio, it is often Program.cs.
(9,25)Line 9, position 25. This is where C# found the problem.
CS0103The error code. You can search for it online to learn more about the error.
The name `score' does not exist in the current contextA short text that tells you what is wrong.


Runtime Errors

A runtime error happens while the program runs. The code compiles, but when a line cannot run, the program stops.

For example, you cannot divide a number by zero:

Example

int x = 10;
int y = 0;
int result = x / y;
Console.WriteLine(result);

The program stops with an error like this:

System.DivideByZeroException: Attempted to divide by zero.

Another common runtime error is an array index that does not exist. This array has 3 items, so the indexes are 0, 1 and 2. There is no index 3:

Example

int[] numbers = {1, 2, 3};
Console.WriteLine(numbers[3]);

The program stops with an error like this:

System.IndexOutOfRangeException: Index was outside the bounds of the array.

Logic Errors

A logic error is the hardest kind to find. The program runs without any error message, but the answer is wrong.

This code should print the average of two scores, 7 and 8. The average is 7.5, but the program prints 7:

Example

int score1 = 7;
int score2 = 8;
double average = (score1 + score2) / 2;
Console.WriteLine(average);  // 7

Try it Yourself »

The problem: score1 + score2 and 2 are both integers. When you divide an integer by an integer, C# removes the decimals. So 15 divided by 2 gives 7, not 7.5.

To fix it, divide by 2.0 instead. Then C# keeps the decimals:

Example

int score1 = 7;
int score2 = 8;
double average = (score1 + score2) / 2.0;
Console.WriteLine(average);  // 7.5

Try it Yourself »

C# cannot find logic errors for you, because the code is correct C#. It just does not do what you wanted. In the next chapter, Debugging, you will learn how to find errors like this.



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.