Docsity

Revision Programs for C#, Assignments of Programming Languages

Toggle sidebar
bg1
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace RevisionProgram
{
// Property + Indexer
class Student
{
private string[] subjects = new string[3];
public int Id { get; set; }
public string Name { get; set; }
// Indexer
public string this[int index]
{
get { return subjects[index]; }
set { subjects[index] = value; }
}
}
class Program
{
// Delegate
delegate void MyDelegate(string msg);
static void ShowMessage(string msg)
{
Console.WriteLine(msg);
}
static void Main()
{
Console.WriteLine("===== ARRAYS =====");
int[] numbers = { 10, 20, 30, 40, 50 };
Console.WriteLine("Original Array:");
foreach (int n in numbers)
Console.Write(n + " ");
Console.WriteLine();
Array.Clear(numbers, 1, 2);
pf3
pf4
pf5

Partial preview of the text

Download Revision Programs for C# and more Assignments Programming Languages in PDF only on Docsity!

using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace RevisionProgram { // Property + Indexer class Student { private string[] subjects = new string[3]; public int Id { get; set; } public string Name { get; set; } // Indexer public string this[int index] { get { return subjects[index]; } set { subjects[index] = value; } } } class Program { // Delegate delegate void MyDelegate(string msg); static void ShowMessage(string msg) { Console.WriteLine(msg); } static void Main() { Console.WriteLine("===== ARRAYS ====="); int[] numbers = { 10, 20, 30, 40, 50 }; Console.WriteLine("Original Array:"); foreach (int n in numbers) Console.Write(n + " "); Console.WriteLine(); Array.Clear(numbers, 1, 2);

Console.WriteLine("After Array.Clear:"); foreach (int n in numbers) Console.Write(n + " "); Console.WriteLine("\n"); //------------------------------------------------ Console.WriteLine("===== LIST ====="); List cities = new List { "Lahore", "Karachi", "Peshawar", "Quetta", "Multan" }; cities.Add("Islamabad"); foreach (string city in cities) Console.WriteLine(city); Console.WriteLine(); //------------------------------------------------ Console.WriteLine("===== DICTIONARY ====="); Dictionary> provinces = new Dictionary> { { "Punjab", new List{ "Lahore","Multan","Gujrat"} }, { "Sindh", new List{ "Karachi","Hyderabad"} }, { "KPK", new List{ "Peshawar","Mardan"} } }; provinces.ToList().ForEach(p => { Console.WriteLine(p.Key); Console.WriteLine(string.Join(", ", p.Value)); Console.WriteLine(); });

Name: "Ali", GPA: 3. ); Console.WriteLine(studentInfo.RollNo); Console.WriteLine(studentInfo.Name); Console.WriteLine(studentInfo.GPA); Console.WriteLine(); //------------------------------------------------ Console.WriteLine("===== DELEGATE ====="); MyDelegate d = ShowMessage; d("Hello from Delegate"); Console.WriteLine(); //------------------------------------------------ Console.WriteLine("===== ACTION ====="); Action print = msg => Console.WriteLine(msg); print("Hello from Action"); Console.WriteLine(); //------------------------------------------------ Console.WriteLine("===== FUNC ====="); Func add = (a, b) => a + b; Console.WriteLine("Sum = " + add(10, 20)); Console.WriteLine(); //------------------------------------------------ Console.WriteLine("===== PREDICATE =====");

Predicate isEven = x => x % 2 == 0; Console.WriteLine(isEven(20)); Console.WriteLine(); //------------------------------------------------ Console.WriteLine("===== LAMBDA EXPRESSIONS ====="); Func square = x => x * x; Console.WriteLine(square(5)); Console.WriteLine(); //------------------------------------------------ Console.WriteLine("===== LINQ ====="); string[] cityArray = { "Lahore", "Karachi", "Peshawar", "Quetta", "Multan", "Gujrat" }; Console.WriteLine("Cities containing 'e':"); var result = cityArray.Where(c => c.Contains('e')); foreach (var city in result) { Console.WriteLine(city); } Console.WriteLine(); Console.WriteLine("Cities sorted:");