Getting Started with C#

Loading concept...

🏠 Building Your First C# Home

Imagine you’re building a house. You need a blueprint, rooms, and a way to talk to visitors. C# works the same way!


🎬 The Story Begins

Once upon a time, there was a builder named C# (pronounced “C-Sharp”). This builder was famous for creating amazing digital houses called programs. Today, you’ll learn how C# builds these houses from the ground up!


🏗️ Program Structure: The Blueprint

Every house needs a blueprint. In C#, the blueprint looks like this:

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello!");
    }
}

Think of it like this:

  • using System; = Bringing your toolbox
  • class Program = The house name
  • static void Main() = The front door (where everything starts!)

🚪 The Main Door

The Main method is special. It’s the front door of your program. When you run your program, C# always looks for this door first!

static void Main()
{
    // Your code starts here
}

📦 Variables: The Storage Boxes

Imagine you have boxes in your house to store things. Variables are those boxes!

string name = "Alex";
int age = 10;

Breaking it down:

  • string = A box for words (text)
  • int = A box for whole numbers
  • name and age = Labels on your boxes
  • "Alex" and 10 = What’s inside!

🔒 Constants: Locked Boxes

Some boxes should never change. Like your birthday - it’s always the same!

const int BirthYear = 2015;

The const keyword means “this box is locked forever!”


🧱 Primitive Data Types: Different Box Sizes

Just like you use small boxes for jewelry and big boxes for toys, C# has different box types:

Type What It Holds Example
int Whole numbers 42
double Decimal numbers 3.14
bool True or False true
char Single letter 'A'
string Words/sentences "Hello"
int score = 100;
double price = 9.99;
bool isHappy = true;
char grade = 'A';
string message = "Great job!";

🔮 Type Inference with var: The Smart Box

Sometimes C# is so smart, it can guess what type of box you need!

var fruit = "Apple";  // C# knows it's a string
var count = 5;        // C# knows it's an int
var cost = 2.50;      // C# knows it's a double

The magic word var tells C#: “You figure out the box type!”

⚠️ Important: You must put something in the box right away when using var. C# needs to see what’s inside to know the type!


📺 Console Input and Output: Talking to Users

Your program can talk to people and listen to them too!

📢 Speaking Out (Output)

Console.WriteLine("Hello, friend!");
Console.Write("Same line... ");
Console.Write("still same line!");
  • WriteLine = Say something, then go to new line
  • Write = Say something, stay on same line

👂 Listening In (Input)

Console.Write("What's your name? ");
string name = Console.ReadLine();
Console.WriteLine("Hi, " + name + "!");

ReadLine() waits for someone to type and press Enter!


✨ Top-Level Statements: The Quick Start

Modern C# has a shortcut! Instead of writing all that blueprint stuff, you can just start coding:

The old way:

using System;
class Program
{
    static void Main()
    {
        Console.WriteLine("Hi!");
    }
}

The new way (top-level):

Console.WriteLine("Hi!");

That’s it! Just one line! C# handles the blueprint automatically.


➕ Basic Operators: The Math Tools

C# can do math and comparisons for you!

🔢 Math Operators

int a = 10;
int b = 3;

int sum = a + b;      // 13 (addition)
int diff = a - b;     // 7 (subtraction)
int product = a * b;  // 30 (multiplication)
int quotient = a / b; // 3 (division)
int remain = a % b;   // 1 (remainder)

⚖️ Comparison Operators

bool isEqual = (5 == 5);   // true
bool notEqual = (5 != 3);  // true
bool greater = (5 > 3);    // true
bool less = (3 < 5);       // true

🔗 Logical Operators

bool both = true && true;   // AND: both must be true
bool either = true || false; // OR: one must be true
bool opposite = !true;       // NOT: flips the value

🌫️ Null Operators: Handling Empty Boxes

Sometimes a box might be empty (null). C# has special tools to handle this safely!

❓ The ? Operator (Nullable Types)

int? mysteryNumber = null;  // This box CAN be empty

The ? after the type means “this could be empty!”

?? The Null-Coalescing Operator

string name = null;
string display = name ?? "Unknown";
// If name is empty, use "Unknown" instead

Think of ?? as saying: “Use this, OR if it’s empty, use that instead!”

??= The Null-Coalescing Assignment

string message = null;
message ??= "Default";
// Only fills the box if it's empty

?. The Null-Conditional Operator

string name = null;
int? length = name?.Length;
// If name is null, don't crash - just return null

The ?. is like asking: “If this exists, then do this… if not, never mind!”


🎯 Quick Summary

graph LR A[🏠 C# Program] --> B[📋 Structure] A --> C[📦 Variables] A --> D[📺 Console I/O] A --> E[➕ Operators] B --> B1[Main Method] B --> B2[Top-Level Statements] C --> C1[Primitive Types] C --> C2[var Keyword] C --> C3[Constants] D --> D1[WriteLine/Write] D --> D2[ReadLine] E --> E1[Math: + - * / %] E --> E2[Comparison: == != > <] E --> E3[Null: ?? ?. ??=]

🎉 You Did It!

You just learned the foundations of C#! You now know how to:

  • ✅ Write a program structure
  • ✅ Create variables and constants
  • ✅ Use different data types
  • ✅ Let C# guess types with var
  • ✅ Talk to users with Console
  • ✅ Use the modern top-level shortcut
  • ✅ Do math and comparisons
  • ✅ Handle empty boxes with null operators

You’re now ready to build amazing things with C#! 🚀


Remember: Every expert was once a beginner. Keep building, keep learning!

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.