Docsity

C# Example Codes/Exercises, Assignments of Programming Languages

Toggle sidebar
bg1
ARRAYS
1. Display all even numbers from an array
int[] arr = { 10, 15, 20, 25, 30 };
foreach(int n in arr)
{
if(n % 2 == 0)
Console.WriteLine(n);
}
2. Find maximum value in array
int[] arr = { 5, 9, 15, 3, 22 };
int max = arr[0];
foreach(int n in arr)
{
if(n > max)
max = n;
}
Console.WriteLine(max);
3. Count positive numbers
int[] arr = { -1, 5, 8, -3, 10 };
int count = 0;
foreach(int n in arr)
{
if(n > 0)
count++;
}
Console.WriteLine(count);
4. Reverse array
int[] arr = {1,2,3,4,5};
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C# Example Codes/Exercises and more Assignments Programming Languages in PDF only on Docsity!

ARRAYS

1. Display all even numbers from an array

int[] arr = { 10, 15, 20, 25, 30 }; foreach(int n in arr) { if(n % 2 == 0) Console.WriteLine(n); }

2. Find maximum value in array

int[] arr = { 5, 9, 15, 3, 22 }; int max = arr[0]; foreach(int n in arr) { if(n > max) max = n; } Console.WriteLine(max);

3. Count positive numbers

int[] arr = { - 1, 5, 8, - 3, 10 }; int count = 0; foreach(int n in arr) { if(n > 0) count++; } Console.WriteLine(count);

4. Reverse array

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

Array.Reverse(arr); foreach(int n in arr) Console.WriteLine(n);

5. Search an element

int[] arr = {10,20,30,40}; int search = 30; if(arr.Contains(search)) Console.WriteLine("Found"); else Console.WriteLine("Not Found");

LIST

Lists are dynamic arrays.

List names = new List();

1. Add 5 names and display them

List names = new List(); names.Add("Ali"); names.Add("Ahmed"); names.Add("Sara"); foreach(string name in names) Console.WriteLine(name);

2. Remove a name

names.Remove("Ahmed");

3. Count total elements

Console.WriteLine(names.Count);

foreach(string name in students.Values) { Console.WriteLine(name); }

4. Display Only Keys

foreach(int key in students.Keys) { Console.WriteLine(key); }

5. Remove Record

students.Remove(2);

HASHTABLE

Non-generic collection.

using System.Collections; Hashtable ht = new Hashtable();

1. Store Student Data

Hashtable ht = new Hashtable(); ht.Add(1,"Ali"); ht.Add(2,"Ahmed"); ht.Add(3,"Sara"); foreach(DictionaryEntry item in ht) { Console.WriteLine(item.Key + " " + item.Value); }

2. Search Key

if(ht.ContainsKey(2)) { Console.WriteLine(ht[2]); }

3. Remove Item

ht.Remove(2);

4. Count Elements

Console.WriteLine(ht.Count);

5. Clear Hashtable

ht.Clear();

DELEGATES

A delegate stores reference to a method.

Think:

Method Address โ†’ Delegate โ†’ Call Method

Basic Delegate

delegate void MyDelegate();

Example 1

using System; delegate void MyDelegate(); class Program { static void Hello() { Console.WriteLine("Hello"); } static void Main() {

Action With Parameters

Action print = name => Console.WriteLine(name); print("Ali");

Output:

Ali

FUNC DELEGATE

Returns value.

Last datatype = Return Type

Func add = (a,b) => a+b; Console.WriteLine(add(10,20));

Output:

Func Example 2

Func length = s => s.Length; Console.WriteLine(length("Ahmed"));

Output:

PREDICATE DELEGATE

Always returns bool.

Predicate isEven = x => x % 2 == 0;

Console.WriteLine(isEven(10));

Output:

True

LAMBDA EXPRESSIONS

Shortcut for anonymous methods.

Syntax:

parameter => expression

Example:

x => x*x

Equivalent to:

int Square(int x) { return x*x; }

Lambda Example 1

Func square = x => x*x; Console.WriteLine(square(5));

Output:

Lambda Example 2

Action greet = name => Console.WriteLine("Hello " + name); greet("Ali");

RollNo:101, Name:"Ahmed", Marks: ); Console.WriteLine(student.RollNo); Console.WriteLine(student.Name); Console.WriteLine(student.Marks);

Super Important Viva Questions

Difference between Array and List

Array List

Fixed size Dynamic size

Faster Flexible

int[] List

Difference between Dictionary and Hashtable

Dictionary Hashtable

Generic Non-generic

Type Safe Not Type Safe

Faster Slower

Introduced in .NET 2.0 Older

Difference between Action and Func

Action Func

No return value Returns value

0 - 16 parameters Last type is return type

Difference between Func and Predicate

Func Predicate

Any return type Always bool

Flexible Used for conditions

Difference between Tuple and Class

Tuple Class

Temporary data Permanent object

Lightweight More structured

Quick coding Better design