INTRODUCTION TO PROGRAMMING & BASIC SYNTAX IN C#

Site: Newgate University Minna - Elearning Platform
Course: Problem Solving
Book: INTRODUCTION TO PROGRAMMING & BASIC SYNTAX IN C#
Printed by: Guest user
Date: Thursday, 26 March 2026, 8:56 AM

1. Introduction to C# Programming

What is C#?

C# (pronounced "C-sharp") is a modern, object-oriented programming language developed by Microsoft. It is designed for building a wide range of applications from desktop software to web applications and games. C# combines the power of C++ with the simplicity of Visual Basic.

Key Features of C#

  • Simple and Modern: Clean syntax that is easy to learn
  • Object-Oriented: Supports encapsulation, inheritance, and polymorphism
  • Type-Safe: Prevents many common programming errors
  • Platform Independent: Can run on Windows, Linux, and macOS via .NET
  • Rich Library Support: Extensive framework library for common tasks

The .NET Ecosystem

  • .NET Framework: The platform on which C# programs run
  • Common Language Runtime (CLR): Executes C# code and manages memory
  • Base Class Library (BCL): Provides pre-built functionality for common tasks

2. Setting Up the Development Environment

Integrated Development Environment (IDE)

An IDE is a software application that provides comprehensive facilities for programming. For C#, we use:

Visual Studio (Recommended)

  • Free Community edition available
  • Includes code editor, debugger, and compiler
  • Supports project management and version control

Visual Studio Code (Alternative)

  • Lightweight, cross-platform code editor
  • Requires C# extension installation
  • Good for beginners who want a simpler setup

Installation Steps

  1. Download Visual Studio Community from Microsoft website
  2. Run installer and select ".NET desktop development" workload
  3. Complete installation and launch Visual Studio
  4. Create new project using "Console App (.NET)" template

Your First C# Program Structure

using System;

namespace HelloWorld

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Hello World!");

        }

    }

}

Program Structure Explanation:

  • using System; - Imports system functionalities
  • namespace - Organizes code into logical groups
  • class - Blueprint for creating objects
  • Main method - Entry point of every C# program
  • Console.WriteLine() - Outputs text to console

3. Basic C# Syntax and Fundamentals

C# Syntax Rules

  • Case Sensitivity: C# is case-sensitive (myVariable ≠ MyVariable)
  • Semicolons: Every statement must end with a semicolon (;)
  • Code Blocks: Use curly braces {} to group statements
  • Whitespace: Spaces and tabs are generally ignored (except in strings)

Comments in C#
Comments are non-executable text for documentation:

Single-line Comments:

// This is a single-line comment

Console.WriteLine("Hello"); // Comment after code

Multi-line Comments:

/* This is a multi-line comment

   that spans across multiple

   lines of text */


4. Variables and Data Types

What are Variables?

Variables are named storage locations in memory that hold data which can be changed during program execution.

Common Data Types in C#

Data Type

Description

Example

Memory

int

Integer numbers

int age = 25;

4 bytes

double

Decimal numbers

double price = 19.99;

8 bytes

float

Decimal numbers (smaller)

float temperature = 98.6f;

4 bytes

char

Single character

char grade = 'A';

2 bytes

string

Text of any length

string name = "John";

varies

bool

True/False values

bool isActive = true;

1 bit

 

 Variable Declaration and Initialization

// Declaration only

int number;

// Declaration with initialization

string name = "Habiba";

double salary = 45000.50;

// Multiple variables of same type

int x = 5, y = 10, z = 15;

// Using var for implicit typing

var message = "Hello World"; // compiler infers string type

Constants
Variables that cannot change during program execution:

const double PI = 3.14159;

const int MAX_SCORE = 100;


5. Basic Input and Output Operations

Output to Console

Console.WriteLine("Hello World"); // Prints with new line

Console.Write("Hello");          // Prints without new line

Console.WriteLine($"Name: {name}"); // String interpolation

Input from Console

Console.Write("Enter your name: ");

string userName = Console.ReadLine();

Console.Write("Enter your age: ");

int userAge = Convert.ToInt32(Console.ReadLine());

Formatting Output

string name = "John";

int age = 25;

double salary = 50000.75;

Console.WriteLine("Name: {0}, Age: {1}, Salary: {2:C}", name, age, salary);

Console.WriteLine($"Name: {name}, Age: {age}, Salary: {salary:C}");


6. Basic Operations and Expressions

6.1 Arithmetic Operators

int a = 10, b = 3;

int sum = a + b;        // 13

int difference = a - b; // 7

int product = a * b;    // 30

int quotient = a / b;   // 3

int remainder = a % b;  // 1

6.2 Assignment Operators int x = 10;

x += 5;  // x = x + 5 → 15

x -= 3;  // x = x - 3 → 12

x *= 2;  // x = x * 2 → 24

x /= 4;  // x = x / 4 → 6

6.3 Operator Precedence

  1. Parentheses ()
  2. Multiplication /, *, %
  3. Addition +, -
  4. Assignment =