This section introduces the fundamental building blocks of C#. Understanding these basics is crucial before diving into more advanced topics.
1. Basic Syntax of C#
A typical C# program consists of:
-
Namespace declaration: A way to group related classes.
-
Class declaration: Defines the blueprint of an object.
-
Main method: Entry point where execution begins.
-
Statements and Expressions: Instructions executed line by line.
Example: Basic Structure
using System; // Namespace declaration
class Program // Class declaration
{
static void Main() // Main method
{
Console.WriteLine("Hello, World!"); // Statement
}
}
Explanation:
-
using System
: Includes the System namespace to use built-in functionality likeConsole
. -
class Program
: Defines a class namedProgram
. All code resides within classes in C#. -
static void Main()
: The main method where program execution starts. -
Statements end with a semicolon (
;
). -
Curly braces
{}
define blocks of code.
2. Data Types in C#
-
In C#, data types are divided into two main categories:
-
Value Types:
-
Store data directly in their own memory space.
-
Examples:
int
,float
,char
,bool
,struct
, and enums. -
Operations on a value type create a copy of the value.
-
-
Reference Types:
-
Store references (memory addresses) that point to the actual data.
-
Examples:
string
,object
, arrays, and classes. -
Operations on a reference type affect the original data because they share the same memory location.
-
Common Data Types
Type |
Size |
Description |
Example |
---|---|---|---|
|
4 bytes |
Whole numbers |
|
|
8 bytes |
Decimal numbers |
|
|
2 bytes |
Single character |
|
|
Variable |
Sequence of characters |
|
|
1 byte |
True or false |
|
Example: Difference Between Value and Reference Types
using System;
class Program
{
static void Main()
{
// Example of a Value Type
int valueType1 = 10;
int valueType2 = valueType1; // Copy of the value is created
valueType2 = 20; // Changing valueType2 does not affect valueType1
Console.WriteLine("ValueType1: " + valueType1); // Output: 10
Console.WriteLine("ValueType2: " + valueType2); // Output: 20
// Example of a Reference Type
string[] referenceType1 = { "A", "B", "C" };
string[] referenceType2 = referenceType1; // Both point to the same memory location
referenceType2[0] = "Z"; // Changing referenceType2 affects referenceType1
Console.WriteLine("ReferenceType1[0]: " + referenceType1[0]); // Output: Z
Console.WriteLine("ReferenceType2[0]: " + referenceType2[0]); // Output: Z
}
}
Key Points
-
Value Types: Stored in the stack, changes do not affect other variables.
-
Reference Types: Stored in the heap, changes are reflected across all references pointing to the same object.
3. Operators in C#
Categories of Operators
-
Arithmetic Operators: Perform basic calculations.
-
Examples:
+
,-
,*
,/
,%
-
-
Comparison Operators: Compare two values.
-
Examples:
==
,!=
,>
,<
,>=
,<=
-
-
Logical Operators: Combine multiple conditions.
-
Examples:
&&
,||
,!
-
Example: Using Operators
int a = 10, b = 3;
// Arithmetic
Console.WriteLine(a + b); // Output: 13
// Comparison
Console.WriteLine(a > b); // Output: True
// Logical
Console.WriteLine(a > b && b > 0); // Output: True
4. Input and Output
Input from User
-
Use
Console.ReadLine()
to read input as a string.
Example: User Input
//input from user
Console.Write("Enter your name: ");
string name = Console.ReadLine();
//output
Console.WriteLine("Hello, " + name);
Output to Console
-
Use
Console.WriteLine()
to print messages or data.
Example: Output
int age = 25;
Console.WriteLine("Your age is: " + age);
Practice Exercises
-
Write a program that calculates the area of a rectangle. Take length and width as inputs.
-
Create a program that checks if a number is even or odd.
-
Build a simple calculator that performs addition, subtraction, multiplication, and division based on user input.
Leave a comment
You must be logged in to post a comment.