Module 1 - Chapter 3: Basics of C# Programming

Diving into the world of programming with C# is an exciting journey. This chapter aims to introduce the fundamental concepts of C# programming that every beginner needs to understand. By the end of this section, you will be familiar with the syntax and basic constructs of C#, how to manage data using variables and data types, and how to interact with users through console input and output. Additionally, you'll grasp the basics of operators and expressions in C#.

Mar 28, 2024

Basics of C# Programming

Diving into the world of programming with C# is an exciting journey. This chapter aims to introduce the fundamental concepts of C# programming that every beginner needs to understand. By the end of this section, you will be familiar with the syntax and basic constructs of C#, how to manage data using variables and data types, and how to interact with users through console input and output. Additionally, you'll grasp the basics of operators and expressions in C#.

Syntax and Basic Constructs

C# syntax is the set of rules that define the combinations of symbols that are considered to be correctly structured programs in the C# language. Let's break down a simple "Hello, World!" program to understand its components:
using System; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } } }
  • Using Directives: using System; - This line tells the compiler that we are using the System namespace, which contains fundamental classes for working with basic system operations. For example, Console class is part of this namespace.
  • Namespace: namespace HelloWorld - Namespaces help organize code and prevent name collisions between classes.
  • Class Declaration: class Program - Defines a class, which is a blueprint for objects. In C#, all code resides inside classes.
  • Method Declaration: static void Main(string[] args) - Defines the Main method, which is the entry point of a C# application. static means it belongs to the class itself, void means it does not return any value, and args represents arguments passed from the command line.
  • Statements: Console.WriteLine("Hello, World!"); - This line is a statement, which executes an action. In this case, it prints the text "Hello, World!" to the console.

Variables and Data Types

Variables are used to store data that can be changed during program execution. Each variable in C# has a specific data type that determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
The most common data types in C# include:
  • int for integers.
  • double for floating-point numbers.
  • char for single characters.
  • string for sequences of characters.
  • bool for Boolean values (true/false).
Here’s how you declare and initialize variables:
int age = 30; double weight = 72.5; char grade = 'A'; string name = "John Doe"; bool isRegistered = true;

Console Input and Output

The Console class in the System namespace provides methods for interacting with the command line. Console.WriteLine is used to output text to the console, while Console.ReadLine is used to read a line of text from the console.
Example of writing to the console:
Console.WriteLine("Enter your name:");
Reading from the console:
string name = Console.ReadLine(); Console.WriteLine("Hello, " + name)

Operators and Expressions

Operators are symbols that specify which operations to perform on variables or values. Expressions are combinations of operators and operands that are evaluated to produce a result.
C# supports a rich set of operators, including:
  • Arithmetic operators (+, , , /, %)
  • Comparison operators (==, !=, <, >, <=, >=)
  • Logical operators (&&, ||, !)
Example of using operators in an expression:
int sum = 10 + 5; // 15 bool isAdult = age > 18; // true if age is greater than 18

Summary

This chapter covered the basics of C# programming, including understanding the structure of a C# program, working with variables and data types, interacting with the console, and using operators to perform operations. These fundamental concepts are the building blocks of C# programming, and mastering them will enable you to create simple yet powerful C# applications. As you continue to learn, you'll build upon these basics to understand more complex programming concepts in C#.