0% found this document useful (0 votes)
18 views

C#Fundamentals

The Complete C# Fundamentals Study Guide provides a comprehensive overview of basic C# concepts, including program structure, variables, control flow, loops, and best practices. It includes code examples, key concepts, common pitfalls, and debugging tips, along with exercises for practical application. The guide is intended for beginners and serves as a foundation for understanding C# programming techniques.

Uploaded by

lomzo.etc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

C#Fundamentals

The Complete C# Fundamentals Study Guide provides a comprehensive overview of basic C# concepts, including program structure, variables, control flow, loops, and best practices. It includes code examples, key concepts, common pitfalls, and debugging tips, along with exercises for practical application. The guide is intended for beginners and serves as a foundation for understanding C# programming techniques.

Uploaded by

lomzo.etc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Complete C# Fundamentals Study Guide

A comprehensive guide to basic C# concepts and programming techniques

Table of Contents
1. Basic Program Structure
2. Variables and Types
3. Operators and Logic
4. Arrays and Collections
5. Control Flow
6. Loops and Iteration
7. User Input and Output
8. Programming Patterns
9. Best Practices
10. Common Pitfalls
11. Debug Tips
12. Study Tips
13. Exercises

Basic Program Structure

Hello World Program (File: 01_HelloWorld.cs)

using System;

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}

Key Concepts:

using System : Imports the System namespace


class Program : Main class container
static void Main(string[] args) : Program entry point
Console.WriteLine() : Output method

Variables and Types

String Variables (File: 02_StringVariable.cs)

var something = "Hello World!";


//string something = "Hello World!"; // Alternative explicit declaration

Variable Types (File: 03_VariableTypes.cs)

var something = "Hello World!"; // Implicitly typed string


int _someNumber = 5; // Integer
string someString = "someString"; // String
bool _coolBool = false; // Boolean

Enums (File: 07_Switch&Enum.cs)


enum TheDay
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}

Key Concepts:

Implicit vs explicit typing


Variable naming conventions
Basic data types
Enumerated types
Valid variable names (can start with underscore)

Operators and Logic

Operators (File: 04_Operators.cs)

bool input1 = true;


bool input2 = false;
bool result = input1 && input2;

Key Concepts:

Logical operators ( && , || , ! )


Comparison operators ( == , != , > , < , >= , <= )
Assignment operators ( = , += , -= )
Increment/decrement operators ( ++ , -- )

Arrays and Collections

Arrays (File: 05_Arrays.cs)

string[] theStudentArray = { "student1", "student2", "student3" };

Lists

List<string> colors = new List<string> { "red", "green", "blue" };

Key Concepts:

Array declaration and initialization


Zero-based indexing
Array bounds
Generic collections
List operations

Control Flow

If-Then Statements (File: 06_IfThen.cs)


int _someScore = 93;

if (_someScore >= 90)


{
Console.WriteLine("A");
}
else if (_someScore >= 80)
{
Console.WriteLine("B");
}
else if (_someScore >= 70)
{
Console.WriteLine("C");
}
else if (_someScore >= 60)
{
Console.WriteLine("D");
}
else
{
Console.WriteLine("X");
}

Switch Statements (File: 07_Switch&Enum.cs)

switch (_someGroupSize)
{
case 1:
Console.WriteLine("The number is one.");
break;
case 2:
Console.WriteLine("The number is two.");
break;
default:
Console.WriteLine("The number is not 1 or 2.");
break;
}

TheDay today = TheDay.Monday;


switch (today)
{
case TheDay.Monday:
Console.WriteLine("Today is Monday.");
break;
default:
Console.WriteLine("It's another day.");
break;
}

Loops and Iteration

While Loops (File: 08_WhileLoop.cs)

int _age = 0;
while (_age < 21)
{
Console.WriteLine("Age: " + _age);
_age++;
}
Console.WriteLine("Now 21 or older!");
For Loops (File: 09_ForLoop.cs)

for (int i = 1; i <= 230; i++)


{
if (i != 230)
{
Console.WriteLine(i);
}

if (i == 230)
{
Console.WriteLine("230 REACHED!!");
}
}

ForEach Loops (File: 10_ForEach.cs)

List<string> colors = new List<string> { "red", "green", "blue" };


foreach (string color in colors)
{
Console.WriteLine(color);
}

Loop Comparison Chart

Loop
Best Used When Syntax Example
Type

Number of iterations
While while (condition) { }
unknown

For Number of iterations known for (init; condition; increment) { }

ForEach Iterating through collections foreach (type item in collection) { }

User Input and Output

Getting User Input (File: 11_GetInput.cs)

Console.WriteLine("Please enter your name: ");


string userName = Console.ReadLine();
Console.WriteLine($"Hello, {userName}! Welcome!");

Key Concepts:

Console.ReadLine() for input


String interpolation with $
User prompts
Input processing

Programming Patterns

Common Loop Patterns


// Counter Pattern
for (int i = 0; i < 10; i++)
{
// Do something n times
}

// Collection Processing
foreach (var item in collection)
{
// Process each item
}

// Condition-Based Processing
while (condition)
{
// Process until condition is false
}

Best Practices

1. Code Organization

Use consistent indentation


Group related code
Comment complex logic
Follow naming conventions

2. Control Structures

Always use curly braces


Keep conditions simple
Handle all cases
Choose appropriate loop types

3. Variables and Types

Initialize before use


Use meaningful names
Choose appropriate types
Consider scope

4. Error Prevention

Validate user input


Check array bounds
Handle edge cases
Use proper exception handling

Common Pitfalls

1. Syntax Errors

Missing semicolons
Mismatched braces
Incorrect capitalization
Invalid variable names

2. Loop Issues

Infinite loops
Off-by-one errors
Wrong increment/decrement
Modifying loop counter inside loop

3. Array and Collection Issues

Index out of bounds


Uninitialized arrays
Modifying collection during iteration
Not handling empty collections

Debug Tips

1. Use Console.WriteLine to track:

Loop iterations
Variable values
Program flow
Conditional results

2. Check boundaries:

Array indices
Loop conditions
Edge cases
Input validation

3. Common debugging points:

Before and after loops


Inside conditional blocks
At method entry/exit
After user input

Study Tips

1. For Beginners

Start with Hello World


Practice variable declarations
Write simple loops
Build basic programs

2. For Control Structures

Write nested if statements


Create switch statements
Combine different loops
Handle multiple conditions

3. For Collections

Create and modify arrays


Work with Lists
Process collections
Handle edge cases

Exercises

1. Basic Programs

Create a number guessing game


Build a simple calculator
Make a temperature converter
Create a basic menu system

2. Control Flow

Implement a grading system


Create a day-of-week checker
Build a simple state machine
Make a decision tree

3. Loops and Collections

Print multiplication tables


Process lists of names
Calculate collection statistics
Search and sort arrays

4. Interactive Programs

Build a command menu


Create a quiz program
Make a simple game
Implement a data entry system

Note: This guide covers fundamental C# concepts. For advanced topics, please refer to the official C# documentation. !

To convert your Markdown to PDF simply start by typing in the editor or pasting from your clipboard.

If your Markdown is in a file clear this content and drop your file into this editor.

tip: click on the pencil icon on the left to clear the


editor)

GitHub flavoured styling by default

We now use GitHub flavoured styling by default.

You might also like