0% found this document useful (0 votes)
17 views2 pages

C# Syntax and Code Examples Guide

This document provides a comprehensive guide to C# syntax, including basic structure, class definitions, methods, variables, constants, and more. It covers essential programming concepts such as conditional statements, loops, arrays, lists, inheritance, interfaces, exception handling, properties, LINQ, async programming, casting, delegates, events, and lambda expressions. Each section includes syntax examples and explanations of their purpose in C# programming.

Uploaded by

tawac24212
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

C# Syntax and Code Examples Guide

This document provides a comprehensive guide to C# syntax, including basic structure, class definitions, methods, variables, constants, and more. It covers essential programming concepts such as conditional statements, loops, arrays, lists, inheritance, interfaces, exception handling, properties, LINQ, async programming, casting, delegates, events, and lambda expressions. Each section includes syntax examples and explanations of their purpose in C# programming.

Uploaded by

tawac24212
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C# Syntax, Code Examples, and Usage Guide

Category Syntax / Code Example Use / Purpose

Basic Structure using System; Imports namespaces and defines


namespace MyApp { } code organization.

Class Definition class Person { Defines an object blueprint with


public string Name; properties and methods.
}

Method void Greet() { Defines reusable blocks of code.


[Link]("Hello");
}

Variables int age = 25; Stores integer data.

Constants const double PI = 3.14; Declares a constant that cannot


change.

Conditional if (x > 0) { Executes code when condition is


[Link]("Positive"); true.
}

Loop for (int i = 0; i < 5; i++) { Repeats a block of code.


[Link](i);
}

Array int[] nums = {1, 2, 3}; Stores multiple values of same


type.

List List list = new List(); Dynamic collection of elements.

Inheritance class Dog : Animal { } Inherits behavior from another


class.

Interface interface IShape { Defines contract for


void Draw(); implementation.
}

Exception Handling try { Handles runtime errors.


// code
} catch (Exception ex) {
[Link]([Link]);
}

Property public int Age { get; set; } Encapsulates field access.

LINQ var result = from x in list Query collections using LINQ


where x > 0 syntax.
select x;

Async async Task DoWork() { Asynchronous programming


await [Link](1000); pattern.
}

Casting int y = (int)x; Converts one data type to


another.
Category Syntax / Code Example Use / Purpose

Delegate delegate void MyDelegate(); Defines reference type for


methods.

Event event Action OnClick; Implements event handling


system.

Lambda var square = x => x * x; Anonymous inline function.

You might also like