Troubleshooting Guide

Purpose: Get unstuck fast when things go wrong

Remember: Every developer gets stuck. This is normal and part of learning!


🚨 Emergency Quick Fixes

Problem: My code won't run at all!

Try these in order:

  1. Check for red squiggles - Compilation errors shown as red underlines

  2. Read error message - It tells you what's wrong!

  3. Build the project - Press Ctrl+Shift+B (Visual Studio)

  4. Clean and rebuild - Build → Clean Solution, then Build → Rebuild

  5. Restart IDE - Close and reopen Visual Studio/VS Code

  6. Check .NET version - Run dotnet --version in terminal

Problem: Program runs but closes immediately!

Solution: Add this at the end of your code:

Console.WriteLine("Press any key to exit...");
Console.ReadKey();

Or run with Ctrl+F5 (without debugging) in Visual Studio.

Problem: "Object reference not set to an instance of an object"

This is a NullReferenceException - most common error!


Compilation Errors (Red Squiggles)

CS0103: The name 'xyz' does not exist in the current context

What it means: You're using a variable/method that doesn't exist or isn't in scope.

Common causes:

  1. Typo in variable name

  2. Variable declared in different scope

  3. Missing using statement

  4. Variable not declared at all

Example:


CS1002: ; expected

What it means: Missing semicolon.

Where to look: Usually the line BEFORE the error!


CS0029: Cannot implicitly convert type 'X' to 'Y'

What it means: Type mismatch - trying to assign wrong type.


CS0161: Not all code paths return a value

What it means: Method should return value but doesn't always.


CS1061: 'Type' does not contain a definition for 'Method'

What it means: Trying to call method that doesn't exist on that type.

Common causes:

  1. Typo in method name

  2. Wrong object type

  3. Missing using statement for extension method

  4. Method is in different class


Runtime Errors (Program Crashes)

NullReferenceException

Most common error! Happens when accessing null object.

How to debug:

  1. Look at line number in error

  2. Check which object is null

  3. Trace back where it comes from

  4. Add null check or initialize properly


IndexOutOfRangeException

Trying to access array/list element that doesn't exist.

Common causes:

  • Using <= instead of < in loop

  • Forgetting arrays start at 0

  • Not checking collection size


DivideByZeroException

Dividing by zero.


FormatException

Usually happens with Parse methods when input is invalid.


FileNotFoundException

File doesn't exist at specified path.

File path issues:

  • Use full path: C:\Users\Name\file.txt

  • Or relative to exe: bin\Debug\net8.0\file.txt

  • Check current directory: Console.WriteLine(Directory.GetCurrentDirectory());


Logic Errors (Wrong Results)

Problem: Loop runs forever (infinite loop)

How to stop infinite loop:

  • Press Ctrl+C in console

  • Click "Stop" button in IDE

  • Close console window


Problem: Wrong calculation results

Common causes:

1. Integer division

2. Operator precedence

3. Floating point precision


Problem: Condition never true


IDE/Environment Issues

Problem: IntelliSense not working

Solutions:

  1. Restart IDE - Fixes 80% of issues

  2. Delete obj/ and bin/ folders, then rebuild

  3. Check C# extension installed (VS Code)

  4. Update IDE to latest version

  5. File → Close Solution, then reopen

Problem: "The type or namespace name 'System' could not be found"

Solutions:

  1. Check .NET SDK installed: dotnet --version

  2. Check project targets correct framework

  3. Restore packages: dotnet restore

  4. Clean and rebuild

Problem: Changes not taking effect

You're editing wrong file!

Check:

  1. Are you editing Program.cs?

  2. Are you in correct project folder?

  3. Did you save the file? (Ctrl+S)

  4. Did you rebuild? (Ctrl+Shift+B)

Problem: Can't create new project

Visual Studio:

  1. Ensure .NET SDK installed

  2. Run installer again, modify installation

  3. Restart Visual Studio

  4. Try "Repair" from installer

VS Code:

  1. Install .NET SDK

  2. Restart VS Code

  3. Install C# Dev Kit extension

  4. Open terminal and try: dotnet new console -n Test


Common Beginner Mistakes

1. Forgetting variable declaration

2. Using = instead of ==

3. Case sensitivity

C# is case-sensitive! Namename

4. Scope confusion

5. Missing break in switch


Debugging Techniques

1. Use Console.WriteLine()

Simple but effective!

2. Use Debugger (Breakpoints)

Visual Studio:

  1. Click in left margin (red dot appears)

  2. Press F5 to run with debugger

  3. Program pauses at breakpoint

  4. Hover over variables to see values

  5. Press F10 to step through line-by-line

  6. Press F5 to continue

VS Code:

  1. Click in left margin

  2. Press F5

  3. Use debug controls at top

3. Check Variable Values

4. Simplify Code

If complex code doesn't work:

  1. Comment out most of it

  2. Test simple version

  3. Gradually add back parts

  4. Find where it breaks

5. Rubber Duck Debugging

Explain your code to:

  • Rubber duck

  • Friend

  • Cat

  • Yourself

Often you'll spot the error while explaining!


When to Ask for Help

Try These First (30 minutes):

  1. Read error message carefully

  2. Check this troubleshooting guide

  3. Add Console.WriteLine() to debug

  4. Try debugger with breakpoints

  5. Google the exact error message

  6. Check Stack Overflow

Still Stuck? Ask for Help!

Where to ask:

  • Stack Overflow (stackoverflow.com)

  • Reddit r/csharp

  • C# Discord servers

  • Programming forums

How to ask (get better answers!):

Good question format:

❌ Bad questions:

  • "My code doesn't work, please fix"

  • [Entire 500-line program pasted]

  • No error message included

  • "It worked yesterday!"


Resources for Getting Unstuck

Official Documentation:

  • Microsoft C# Docs: https://docs.microsoft.com/dotnet/csharp/

  • .NET API Browser: https://docs.microsoft.com/dotnet/api/

Q&A Sites:

  • Stack Overflow: https://stackoverflow.com/questions/tagged/c%23

  • Reddit r/csharp: https://reddit.com/r/csharp

  • Microsoft Q&A: https://docs.microsoft.com/answers/

Search Tips:

  • Include "C#" in search

  • Include exact error code (e.g., "CS0103")

  • Add "2024" or "NET 8" for recent answers


Preventing Problems

Best Practices:

1. Write small, testable code

2. Test as you go

  • Don't write 100 lines then test

  • Write 5-10 lines, run it, test it

  • Add more only when current part works

3. Use meaningful names

4. Handle errors gracefully

5. Keep code simple

  • Simple code = fewer bugs

  • Don't try to be clever

  • Clear is better than concise


Emergency Checklist

When completely stuck, work through this:

Remember: Being stuck is temporary. Every problem has a solution!


Final Tips

When Frustrated:

  1. Take a break - Walk away for 15 minutes

  2. Start fresh - Sometimes easier to rewrite

  3. Sleep on it - Morning brain works better

  4. Ask for help - No shame in asking!

Build Resilience:

  • Every error is a learning opportunity

  • Debugging is a skill - you'll get better

  • Everyone struggles - even experts

  • Persistence pays off - keep going!

You've got this! 💪

Every developer you admire has been exactly where you are now. The only difference? They didn't give up.

Keep coding! 🚀

Last updated