Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧠 Introduction
Every programming language has its own set of rules—its syntax—that
defines how programs are written and interpreted. In this chapter, we explore
the foundational elements of C# syntax, including statements, variables,
and data types, which are essential to writing any program.
📚 1. C# Syntax Overview
C# syntax is influenced by C, C++, and Java. It's clean,
structured, and consistent—great for beginners and professionals alike.
✅ Basic Syntax Elements
Element |
Example |
Description |
Namespace |
using System; |
Imports built-in or
user-defined libraries |
Class |
class MyClass |
Blueprint for
objects |
Method |
static void Main() |
Function that contains
code logic |
Statement |
Console.WriteLine("Hi"); |
Single
executable instruction |
Block |
{ ... } |
Groups of code
enclosed in braces |
Comments |
// single, /*
multi */ |
Notes ignored
by compiler |
✅ Example Program:
using
System;
class
Hello
{
static void Main()
{
Console.WriteLine("Welcome to C#
syntax!");
}
}
📦 2. Statements and
Expressions
int
x = 5; // statement with
expression
Console.WriteLine(x
+ 2); // expression in output
🧾 3. Variables in C#
A variable is a name given to a memory location to
store data. It can be changed during program execution.
✅ Syntax:
dataType
variableName = value;
📋 Variable Declaration
Examples
Code |
Description |
int age = 25; |
Integer variable |
float price = 9.99f; |
Floating-point
number |
bool isActive =
true; |
Boolean variable |
string name = "Alice"; |
String
variable |
🧠 Variable Naming Rules
🔢 4. C# Data Types
C# is a strongly-typed language—every variable must
have a declared type.
🔹 Categories of Data
Types
Type Category |
Examples |
Value Types |
int, float, bool,
char, struct |
Reference Types |
string, object,
class, interface |
Nullable Types |
int?, bool? |
Pointer Types |
Used in
unsafe code |
🔹 Value Types
Stored directly in memory and hold data.
Type |
Size |
Example |
Default |
int |
4 bytes |
1, 10 |
0 |
float |
4 bytes |
10.5f |
0.0f |
double |
8 bytes |
20.99 |
0.0 |
bool |
1 byte |
true |
false |
char |
2 bytes |
'A' |
'\0' |
🔹 Reference Types
They refer to memory addresses and store a reference,
not the data itself.
Type |
Example |
string |
"Hello
World" |
object |
Can store any
data type |
class |
Blueprint for objects |
interface |
Defines a
contract |
✅ Example: Value vs Reference
int
x = 10;
string
name = "John";
🧠 5. Constants and
Readonly
🔹 Constant (const)
A compile-time constant that cannot be changed after it's
declared.
const
double Pi = 3.14159;
🔹 Readonly
Can be assigned at declaration or in constructor, but not
modified later.
readonly
int id;
🔁 6. Type Conversion in
C#
C# supports both implicit and explicit type
conversion.
✅ Implicit Conversion
Safe conversions (e.g., int to float):
int
a = 10;
float
b = a;
✅ Explicit Conversion (Casting)
Risky conversions that require a cast:
double
x = 10.5;
int
y = (int)x; // y = 10
🔹 Using Convert Class
string
s = "123";
int
num = Convert.ToInt32(s);
📋 Type Conversion Table
Conversion |
Method |
Example |
Implicit |
Automatic |
int → float |
Explicit |
Using (type)
cast |
(int) 3.14 |
Using Convert |
Convert.ToInt32() |
Converts from string |
Using Parse() |
int.Parse("123") |
Parses string
to int |
📌 7. Variable Scope in C#
Scope determines the accessibility and lifetime
of a variable.
Scope |
Where Accessible |
Lifetime |
Local |
Inside method or block
only |
Until method ends |
Global |
Inside class |
As long as
object exists |
Static |
Shared across instances |
Life of program |
✅ Example:
class
Test {
int age = 30; // instance variable
void Show() {
int x = 5; // local variable
Console.WriteLine(x + age);
}
}
🧪 Sample Program: Type
Conversion and Variables
using
System;
class
Program
{
static void Main()
{
int a = 20;
double b = a; // implicit
double c = 12.9;
int d = (int)c; // explicit
Console.WriteLine("b = " +
b); // 20
Console.WriteLine("d = " +
d); // 12
}
}
🧠 Real-World Usage
Use Case |
Example |
Banking System |
decimal balance, int
accountNumber |
E-commerce App |
string
productName, float price, bool inStock |
Game Development |
char keyPress, bool
isAlive, float velocity |
✅ Summary Table
Concept |
Key Point |
Syntax |
Rules of writing valid
C# code |
Variables |
Named memory
locations |
Data Types |
Define what kind of
data a variable holds |
Constants |
Cannot change
once declared |
Type Conversion |
Changing a variable
from one type to another |
Scope |
Visibility
and lifetime of variables |
A: Yes, especially with Visual Studio and .NET’s extensive documentation and community support.
A: Not at all. C# is independent and designed for new learners.
A: Yes, using ASP.NET Core you can build scalable web apps and APIs.
A: Both are similar syntactically, but C# is part of the Microsoft .NET ecosystem and often integrates better with Windows technologies.
A: Not anymore. With .NET Core and .NET 8, C# is cross-platform.
A: Absolutely. It is the main language used in Unity.
A: Visual Studio is the most powerful and popular IDE for C# development.
A: Yes, with Xamarin or .NET MAUI, C# can build cross-platform mobile apps.
A: C# continues to evolve with modern features and has strong backing from Microsoft, making it future-proof.
A: No, you can code, compile, and run C# locally, though some online libraries/tools may require internet.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)