Day 1: Basics of C# – Part 1
Objective: Get familiar with the basic syntax and concepts of C#.
Topics Covered:
- Introduction to C# and .NET framework
- Basic structure of a C# program
- Data types and variables
- Operators (arithmetic, relational, logical)
Topic 1: Introduction to C# and .NET Framework
C# is a modern, object-oriented programming language developed by Microsoft. It’s part of the .NET framework, which provides a large library and runtime for building various types of applications.
Topic 2: Basic Structure of a C# Program
Here is a simple C# program that prints “Hello, World!” to the console:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
Explanation:
using System;– Imports the System namespace, which contains fundamental classes for base functionalities.namespace HelloWorld– Declares a namespace named HelloWorld to organize code.class Program– Defines a class named Program.static void Main(string[] args)– The entry point of the program. TheMainmethod is where the program starts executing.Console.WriteLine("Hello, World!");– Prints “Hello, World!” to the console.
Topic 3: Data Types and Variables
Variables are used to store data in a program. Each variable in C# has a type, which determines what kind of data it can store.
Example:
using System;
namespace VariablesDemo
{
class Program
{
static void Main(string[] args)
{
int age = 25; // Integer
double height = 5.9; // Floating point number
char initial = 'C'; // Character
string name = "Carolyn"; // String
bool isStudent = true; // Boolean
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
Console.WriteLine("Height: " + height);
Console.WriteLine("Initial: " + initial);
Console.WriteLine("Is Student: " + isStudent);
}
}
}
Explanation:
int age = 25;– Declares an integer variable namedageand initializes it to 25.double height = 5.9;– Declares a double variable namedheightand initializes it to 5.9.char initial = 'C';– Declares a char variable namedinitialand initializes it to ‘C’.string name = "Carolyn";– Declares a string variable namednameand initializes it to “Carolyn”.bool isStudent = true;– Declares a boolean variable namedisStudentand initializes it to true.
Topic 4: Operators
Operators are symbols that perform operations on variables and values. C# has several types of operators.
Arithmetic Operators:
using System;
namespace ArithmeticOperators
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 5;
Console.WriteLine("Addition: " + (a + b));
Console.WriteLine("Subtraction: " + (a - b));
Console.WriteLine("Multiplication: " + (a * b));
Console.WriteLine("Division: " + (a / b));
Console.WriteLine("Modulus: " + (a % b));
}
}
}
Relational Operators:
using System;
namespace RelationalOperators
{
class Program
{
static void Main(string[] args)
{
int x = 10;
int y = 5;
Console.WriteLine("x == y: " + (x == y));
Console.WriteLine("x != y: " + (x != y));
Console.WriteLine("x > y: " + (x > y));
Console.WriteLine("x < y: " + (x < y));
Console.WriteLine("x >= y: " + (x >= y));
Console.WriteLine("x <= y: " + (x <= y));
}
}
}
Logical Operators:
using System;
namespace LogicalOperators
{
class Program
{
static void Main(string[] args)
{
bool a = true;
bool b = false;
Console.WriteLine("a && b: " + (a && b)); // Logical AND
Console.WriteLine("a || b: " + (a || b)); // Logical OR
Console.WriteLine("!a: " + (!a)); // Logical NOT
}
}
}
Example Exercise
Write a program that takes two numbers as input from the user and prints their sum, difference, product, and quotient.
using System;
namespace SimpleCalculator
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the first number: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second number: ");
int num2 = Convert.ToInt32(Console.ReadLine());
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
double quotient = (double)num1 / num2;
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Difference: " + difference);
Console.WriteLine("Product: " + product);
Console.WriteLine("Quotient: " + quotient);
}
}
}
Explanation:
Console.ReadLine()reads a line of input from the console.Convert.ToInt32()converts the input string to an integer.- The program performs arithmetic operations and prints the results.
This concludes the Day 1 topics. For Day 2 click here




