0% found this document useful (0 votes)
7 views3 pages

C# Programming Basics Examples

The document contains 13 C# programs demonstrating various programming concepts including basic input/output, control structures, methods, classes, inheritance, exception handling, file I/O, LINQ, and asynchronous programming. Each program is self-contained and illustrates a specific feature or functionality of the C# language. The examples range from simple 'Hello World' to more complex operations like file handling and asynchronous tasks.

Uploaded by

tarunmhanta30
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)
7 views3 pages

C# Programming Basics Examples

The document contains 13 C# programs demonstrating various programming concepts including basic input/output, control structures, methods, classes, inheritance, exception handling, file I/O, LINQ, and asynchronous programming. Each program is self-contained and illustrates a specific feature or functionality of the C# language. The examples range from simple 'Hello World' to more complex operations like file handling and asynchronous tasks.

Uploaded by

tarunmhanta30
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

Program 1 - Hello World

using System;
class Program
{
static void Main()
{
[Link]("Hello, World!");
}
}
Program 2 - Read Input and Print
using System;
class Program
{
static void Main()
{
[Link]("Enter your name: ");
string name = [Link]();
[Link]("Hello, " + name + "!");
}
}
Program 3 - If-Else (Even/Odd)
using System;
class Program
{
static void Main()
{
[Link]("Enter an integer: ");
int n = [Link]([Link]());
if (n % 2 == 0)
[Link](n + " is even.");
else
[Link](n + " is odd.");
}
}
Program 4 - Switch (Day of Week)
using System;
class Program
{
static void Main()
{
[Link]("Enter a number (1-7): ");
int d = [Link]([Link]());
switch (d)
{
case 1: [Link]("Sunday"); break;
case 2: [Link]("Monday"); break;
case 3: [Link]("Tuesday"); break;
case 4: [Link]("Wednesday"); break;
case 5: [Link]("Thursday"); break;
case 6: [Link]("Friday"); break;
case 7: [Link]("Saturday"); break;
default: [Link]("Invalid"); break;
}
}
}
Program 5 - For Loop (Sum 1..N)
using System;
class Program
{
static void Main()
{
[Link]("Enter N: ");
int N = [Link]([Link]());
int sum = 0;
for (int i = 1; i <= N; i++)
sum += i;
[Link]("Sum = " + sum);
}
}
Program 6 - Array and Find Max
using System;
class Program
{
static void Main()
{
[Link]("Enter number of elements: ");
int n = [Link]([Link]());
int[] a = new int[n];
for (int i = 0; i < n; i++)
{
[Link]("a[" + i + "] = ");
a[i] = [Link]([Link]());
}
int max = a[0];
for (int i = 1; i < n; i++)
if (a[i] > max) max = a[i];
[Link]("Max = " + max);
}
}
Program 7 - Method and Return
using System;
class Program
{
static int Add(int x, int y)
{
return x + y;
}
static void Main()
{
[Link]("3 + 5 = " + Add(3, 5));
}
}
Program 8 - Class and Object
using System;
class Person
{
public string Name;
public int Age;
public void Display()
{
[Link](Name + " (" + Age + " years)");
}
}
class Program
{
static void Main()
{
Person p = new Person();
[Link] = "Amit";
[Link] = 25;
[Link]();
}
}
Program 9 - Inheritance
using System;
class Animal
{
public void Eat() { [Link]("Eating"); }
}
class Dog : Animal
{
public void Bark() { [Link]("Barking"); }
}
class Program
{
static void Main()
{
Dog d = new Dog();
[Link]();
[Link]();
}
}
Program 10 - Exception Handling
using System;
class Program
{
static void Main()
{
try
{
[Link]("Enter divisor: ");
int d = [Link]([Link]());
int r = 10 / d;
[Link]("Result = " + r);
}
catch (DivideByZeroException)
{
[Link]("Cannot divide by zero.");
}
catch (FormatException)
{
[Link]("Invalid input.");
}
}
}
Program 11 - File I/O (Write & Read)
using System;
using [Link];
class Program
{
static void Main()
{
string path = "[Link]";
[Link](path, "Hello from C# file.");
string text = [Link](path);
[Link]("File content: " + text);
}
}
Program 12 - Simple LINQ Example
using System;
using [Link];
class Program
{
static void Main()
{
int[] numbers = {1,2,3,4,5,6};
var evens = [Link](n => n % 2 == 0);
foreach (var e in evens)
[Link](e);
}
}
Program 13 - Async/Await (Basic)
using System;
using [Link];
class Program
{
static async Task<string> GetDataAsync()
{
await [Link](500);
return "Done";
}
static async Task Main()
{
string res = await GetDataAsync();
[Link](res);
}
}

You might also like