- C# Basics
- C# - Introduction
- C# - Install
- C# - Hello World
- C# - Syntax
- C# - Comments
- C# - Variables
- C# - Data Types
- C# - Operators
- C# - Strings
- C# - Conditions
- C# - Loops
- C# - Advanced
- C# - Methods
- C# - Classes
- C# - OOP
- C# - Files
- C# - Data Structures
- C# - Enums
- C# - Interface
- C# - Access Modifiers
- C# - Abstract Class
- C# - Concrete Class
- C# - Function Pointers
- C# - Events
- C# - Class Decorators
- C# - Design Patterns
- C# - LINQ
- C# - Type Conversion
- C# - Generics
- C# - DLLs
- C# - Reference Type
- C# - Scripts
- C# - Database
- C# - With SQL Server
- C# - With Oracle
- C# - Parameterization
- C# - With MySQL
C# - Syntax
C# (pronounced C-sharp) is a statically typed, object-oriented programming language developed by Microsoft. It is widely used for developing a range of applications, from web and desktop apps to games and cloud services. Here’s an overview of C# syntax, covering basic constructs and concepts.
Basic Syntax
1. Hello World Program
csharpusing System; // Import the System namespace
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}
using System;
imports theSystem
namespace.class Program
defines a class namedProgram
.static void Main()
is the entry point of a C# application.
Variables and Data Types
2. Declaring Variables
csharpint number = 10;
double price = 19.99;
char initial = 'A';
string name = "Alice";
bool isActive = true;
- Primitive Types:
int
,double
,char
,bool
- Non-Primitive Type:
string
3. Constants
csharpconst double Pi = 3.14159;
const
declares a constant value that cannot be changed.
Control Flow
4. Conditional Statements
csharpint age = 20;
if (age >= 18)
{
Console.WriteLine("Adult");
}
else
{
Console.WriteLine("Minor");
}
5. Switch Statement
csharpint day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Weekend");
break;
}
Loops
6. For Loop
csharpfor (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
7. While Loop
csharpint count = 0;
while (count < 5)
{
Console.WriteLine(count);
count++;
}
8. Do-While Loop
csharpint number = 0;
do
{
Console.WriteLine(number);
number++;
} while (number < 5);
Functions and Methods
9. Defining and Calling Methods
csharppublic class MathOperations
{
public int Add(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main()
{
MathOperations math = new MathOperations();
int result = math.Add(5, 7);
Console.WriteLine(result);
}
}
public int Add(int a, int b)
defines a method that returns an integer.static void Main()
is where execution starts.
Classes and Objects
10. Defining a Class
csharppublic class Person
{
public string Name { get; set; }
public int Age { get; set; }
public void Greet()
{
Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
}
}
class Program
{
static void Main()
{
Person person = new Person();
person.Name = "John";
person.Age = 30;
person.Greet();
}
}
Inheritance
11. Base and Derived Classes
csharppublic class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Woof!");
}
}
class Program
{
static void Main()
{
Dog dog = new Dog();
dog.Eat();
dog.Bark();
}
}
Interfaces
12. Defining and Implementing Interfaces
csharppublic interface IDrivable
{
void Drive();
}
public class Car : IDrivable
{
public void Drive()
{
Console.WriteLine("Car is driving.");
}
}
class Program
{
static void Main()
{
IDrivable drivable = new Car();
drivable.Drive();
}
}
Collections
13. Arrays
csharpint[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}
14. Lists
csharpusing System.Collections.Generic;
List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
names.Add("David");
foreach (string name in names)
{
Console.WriteLine(name);
}
Exception Handling
15. Try-Catch
csharptry
{
int[] array = new int[5];
array[10] = 1; // This will throw an exception
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
finally
{
Console.WriteLine("Execution completed.");
}
LINQ (Language Integrated Query)
16. Basic LINQ Query
csharpusing System.Linq;
int[] numbers = { 1, 2, 3, 4, 5 };
var evenNumbers = from number in numbers
where number % 2 == 0
select number;
foreach (var number in evenNumbers)
{
Console.WriteLine(number);
}
Summary
C# syntax is designed to be familiar to users of other C-based languages (like C++ and Java) while incorporating modern features. It supports object-oriented programming, strong typing, and a range of features that make it suitable for various application development needs.