0% found this document useful (0 votes)
4 views6 pages

CSharp Programming Foundations With Examples

The document provides an overview of programming foundations in C#, covering key concepts such as variables, data types, operators, control statements, methods, object-oriented programming, collections, exception handling, file I/O, and LINQ basics. It includes syntax examples and explanations for each topic, emphasizing the importance of type safety and error handling. Additionally, it touches on miscellaneous features like nullable types, null-coalescing operators, string interpolation, and DateTime usage.

Uploaded by

Raj Kumar
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)
4 views6 pages

CSharp Programming Foundations With Examples

The document provides an overview of programming foundations in C#, covering key concepts such as variables, data types, operators, control statements, methods, object-oriented programming, collections, exception handling, file I/O, and LINQ basics. It includes syntax examples and explanations for each topic, emphasizing the importance of type safety and error handling. Additionally, it touches on miscellaneous features like nullable types, null-coalescing operators, string interpolation, and DateTime usage.

Uploaded by

Raj Kumar
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

Programming Foundations in C# - With Syntax and Examples

1. Variables and Data Types

Variables store data values. C# is statically typed, so you must declare a variable's type.

Syntax:

int age = 25;

string name = "Sharath";

bool isActive = true;

Examples:

double salary = 55000.75;

char grade = 'A';

Type conversion:

int x = 10;

double y = x; // Implicit

int z = (int)y; // Explicit (cast)

2. Operators and Expressions

Operators perform actions on variables.

Arithmetic: +, -, *, /, %

Comparison: ==, !=, >, <, >=, <=

Logical: &&, ||, !

Example:

int a = 10, b = 20;

bool result = (a < b) && (b > 15); // true


Programming Foundations in C# - With Syntax and Examples

3. Control Statements

Used to control flow of execution.

If-Else:

if (score > 90) {

[Link]("Excellent");

} else {

[Link]("Keep trying!");

Loops:

for (int i = 0; i < 5; i++) {

[Link](i);

while (x > 0) {

x--;

4. Methods

Block of code that performs a task.

Syntax:

returnType MethodName(parameters) {

// code

Example:

int Add(int a, int b) {


Programming Foundations in C# - With Syntax and Examples

return a + b;

Optional parameters:

void Greet(string name = "User") {

[Link]("Hello " + name);

5. Object-Oriented Programming

Encapsulates data and functions into objects.

Class & Object:

class Person {

public string Name;

public void Greet() {

[Link]("Hello " + Name);

Person p = new Person();

[Link] = "Sharath";

[Link](); // Hello Sharath

Inheritance:

class Employee : Person {

public int EmployeeId;

Polymorphism (Override):

public virtual void Speak() { ... }


Programming Foundations in C# - With Syntax and Examples

public override void Speak() { ... }

Interfaces:

interface IAnimal {

void Speak();

6. Collections and Generics

Collections store multiple items.

List:

List<int> numbers = new List<int>() { 1, 2, 3 };

Dictionary:

Dictionary<string, int> ages = new Dictionary<string, int>() {

{"John", 30}, {"Alice", 28}

};

Generics ensure type safety:

List<string> names = new List<string>();

7. Exception Handling

Used to handle runtime errors.

Syntax:

try {

// code

} catch (Exception ex) {

[Link]([Link]);
Programming Foundations in C# - With Syntax and Examples

} finally {

// clean-up

Custom Exception:

class MyException : Exception {

public MyException(string msg) : base(msg) {}

8. File I/O

Read/Write files using [Link].

Reading a file:

string content = [Link]("[Link]");

Writing to file:

[Link]("[Link]", "Hello World");

StreamReader example:

using (StreamReader sr = new StreamReader("[Link]")) {

string line = [Link]();

9. LINQ Basics

Used to query collections.

Example:

int[] nums = {1, 2, 3, 4};

var even = [Link](n => n % 2 == 0);


Programming Foundations in C# - With Syntax and Examples

LINQ with list:

List<string> names = new List<string>{"Ana", "Bob"};

var result = [Link](n => [Link]("A"));

10. Miscellaneous

Nullable types:

int? x = null;

Null-coalescing operator:

int y = x ?? 0;

String interpolation:

string name = "Sharath";

[Link]($"Hello {name}");

DateTime:

DateTime now = [Link];

You might also like