Basic Syntax Elements in C#
- Comments
- Variables and Data Types
- Operators
- Control Flow Statements
- Methods
- Classes and Objects
Example Program Demonstrating Basic Syntax
Here’s a simple C# program that demonstrates various basic syntax elements:
csharp1using System; // Importing the System namespace 2 3namespace BasicSyntaxExample // Declaring a namespace 4{ 5 class Program // Declaring a class named Program 6 { 7 // The Main method - the entry point of the program 8 static void Main(string[] args) 9 { 10 // 1. Variables and Data Types 11 int age = 25; // Integer variable 12 string name = "Alice"; // String variable 13 double height = 5.7; // Double variable 14 bool isStudent = true; // Boolean variable 15 16 // 2. Output 17 Console.WriteLine("Name: " + name); 18 Console.WriteLine("Age: " + age); 19 Console.WriteLine("Height: " + height); 20 Console.WriteLine("Is Student: " + isStudent); 21 22 // 3. Control Flow Statements 23 if (age < 18) 24 { 25 Console.WriteLine(name + " is a minor."); 26 } 27 else 28 { 29 Console.WriteLine(name + " is an adult."); 30 } 31 32 // 4. For Loop 33 Console.WriteLine("Counting from 1 to 5:"); 34 for (int i = 1; i <= 5; i++) 35 { 36 Console.WriteLine(i); 37 } 38 39 // 5. Method Call 40 GreetUser (name); 41 } 42 43 // 6. Method Definition 44 static void GreetUser (string userName) 45 { 46 Console.WriteLine($"Hello, {userName}! Welcome to the C# program."); 47 } 48 } 49}
Explanation of Each Syntax Element
Comments
csharp1// This is a single-line comment. 2/* This is a 3 multi-line comment. */
- Comments are used to explain code and are ignored by the compiler. Single-line comments start with
//
, while multi-line comments are enclosed between/*
and*/
.
- Comments are used to explain code and are ignored by the compiler. Single-line comments start with
Variables and Data Types
csharp1int age = 25; // Integer variable 2string name = "Alice"; // String variable 3double height = 5.7; // Double variable 4bool isStudent = true; // Boolean variable
- Variables are used to store data. Each variable has a type (e.g.,
int
,string
,double
,bool
) that defines the kind of data it can hold.
- Variables are used to store data. Each variable has a type (e.g.,
Output
csharp1Console.WriteLine("Name: " + name);
Console.WriteLine()
is used to print output to the console. The+
operator concatenates strings.
Control Flow Statements
csharp1if (age < 18) 2{ 3 Console.WriteLine(name + " is a minor."); 4} 5else 6{ 7 Console.WriteLine(name + " is an adult."); 8}
- Control flow statements (like
if
,else
,switch
, etc.) allow you to execute different blocks of code based on conditions.
- Control flow statements (like
For Loop
csharp1for (int i = 1; i <= 5; i++) 2{ 3 Console.WriteLine(i); 4}
- A
for
loop is used to repeat a block of code a certain number of times. In this case, it counts from 1 to 5.
- A
Method Definition and Call
csharp1static void GreetUser (string userName) 2{ 3 Console.WriteLine($"Hello, {userName}! Welcome to the C# program."); 4}
- Methods are blocks of code that perform a specific task. They can take parameters (like
userName
) and can be called from other parts of the program.
- Methods are blocks of code that perform a specific task. They can take parameters (like
Category | Keyword |
---|---|
Control Flow Keywords | if |
else | |
switch | |
case | |
default | |
while | |
do | |
for | |
foreach | |
break | |
continue | |
goto | |
return | |
try | |
catch | |
finally | |
throw | |
using | |
checked | |
unchecked | |
Data Types | int |
float | |
double | |
decimal | |
char | |
string | |
bool | |
object | |
var | |
dynamic | |
void | |
Access Modifiers | public |
private | |
protected | |
internal | |
protected internal | |
private protected | |
Modifiers | static |
readonly | |
volatile | |
abstract | |
sealed | |
virtual | |
override | |
new | |
async | |
await | |
Class and Interface Keywords | class |
struct | |
interface | |
enum | |
delegate | |
Namespace and Assembly Keywords | namespace |
using | |
extern | |
Type Keywords | is |
as | |
sizeof | |
typeof | |
default | |
nameof | |
Exception Handling Keywords | try |
catch | |
finally | |
throw | |
Other Keywords | true |
false | |
null | |
this | |
base | |
lock | |
checked | |
unchecked | |
fixed | |
unsafe | |
implicit | |
explicit | |
operator | |
namespace | |
global |
Contextual Keywords
Contextual Keyword |
---|
async |
await |
from |
select |
where |
let |
orderby |
join |
group |
into |