C# Basic Syntax


Basic Syntax Elements in C#

  1. Comments
  2. Variables and Data Types
  3. Operators
  4. Control Flow Statements
  5. Methods
  6. Classes and Objects

Example Program Demonstrating Basic Syntax

Here’s a simple C# program that demonstrates various basic syntax elements:

csharp
1using 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

  1. Comments

    csharp
    1// 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 */.
  2. Variables and Data Types

    csharp
    1int 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., intstringdoublebool) that defines the kind of data it can hold.
  3. Output

    csharp
    1Console.WriteLine("Name: " + name);
    • Console.WriteLine() is used to print output to the console. The + operator concatenates strings.
  4. Control Flow Statements

    csharp
    1if (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 ifelseswitch, etc.) allow you to execute different blocks of code based on conditions.
  5. For Loop

    csharp
    1for (int i = 1; i <= 5; i++) 2{ 3 Console.WriteLine(i); 4}
    • for loop is used to repeat a block of code a certain number of times. In this case, it counts from 1 to 5.
  6. Method Definition and Call

    csharp
    1static 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.
Reserved Keywords

CategoryKeyword
Control Flow Keywordsif
else
switch
case
default
while
do
for
foreach
break
continue
goto
return
try
catch
finally
throw
using
checked
unchecked
Data Typesint
float
double
decimal
char
string
bool
object
var
dynamic
void
Access Modifierspublic
private
protected
internal
protected internal
private protected
Modifiersstatic
readonly
volatile
abstract
sealed
virtual
override
new
async
await
Class and Interface Keywordsclass
struct
interface
enum
delegate
Namespace and Assembly Keywordsnamespace
using
extern
Type Keywordsis
as
sizeof
typeof
default
nameof
Exception Handling Keywordstry
catch
finally
throw
Other Keywordstrue
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

Talk to us?

Post your blog