techmore.in

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

csharp
using System; // Import the System namespace class Program { static void Main() { Console.WriteLine("Hello, World!"); } }
  • using System; imports the System namespace.
  • class Program defines a class named Program.
  • static void Main() is the entry point of a C# application.

Variables and Data Types

2. Declaring Variables

csharp
int 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

csharp
const double Pi = 3.14159;
  • const declares a constant value that cannot be changed.

Control Flow

4. Conditional Statements

csharp
int age = 20; if (age >= 18) { Console.WriteLine("Adult"); } else { Console.WriteLine("Minor"); }

5. Switch Statement

csharp
int 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

csharp
for (int i = 0; i < 5; i++) { Console.WriteLine(i); }

7. While Loop

csharp
int count = 0; while (count < 5) { Console.WriteLine(count); count++; }

8. Do-While Loop

csharp
int number = 0; do { Console.WriteLine(number); number++; } while (number < 5);

Functions and Methods

9. Defining and Calling Methods

csharp
public 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

csharp
public 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

csharp
public 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

csharp
public 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

csharp
int[] numbers = { 1, 2, 3, 4, 5 }; foreach (int number in numbers) { Console.WriteLine(number); }

14. Lists

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

csharp
try { 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

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