Skip to content

Instantly share code, notes, and snippets.

@theahmadzai
Last active June 15, 2020 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theahmadzai/1d936dfbd49241adc8412f358df9e299 to your computer and use it in GitHub Desktop.
Save theahmadzai/1d936dfbd49241adc8412f358df9e299 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace TestApp {
class Program {
static Dictionary<string, string> users = new Dictionary<string, string>();
static void Main(string[] args) {
Input20Users();
ShowAllUsers();
SearchAndDelete();
SearchAndDisplay();
}
static void Input20Users() {
for(byte i = 0; i<4; i++) {
Console.Write("Enter mobile #: ");
string mob = Console.ReadLine();
Console.Write("Enter name: ");
string name = Console.ReadLine();
if(users.ContainsKey(mob)) {
Console.WriteLine("This mobile no already exists.");
i--;
continue;
}
users.Add(mob, name);
}
}
static void ShowAllUsers() {
foreach(var user in users) {
Console.WriteLine("{0}:{1} ", user.Key, user.Value);
}
}
static void SearchAndDelete() {
Console.Write("Enter mobile no to delete: ");
string mob = Console.ReadLine();
if(users.ContainsKey(mob)) {
users.Remove(mob);
Console.WriteLine("Record {0} deleted.", mob);
return;
}
Console.WriteLine("Record {0} Not found.", mob);
}
static void SearchAndDisplay() {
Console.Write("Enter name to display: ");
string name = Console.ReadLine();
foreach(var user in users) {
if(user.Value == name) {
Console.WriteLine("Mobile #: {0}, Name: {1}", user.Key, user.Value);
return;
}
}
Console.WriteLine("Record {0} not found.", name);
}
}
}
using System;
using System.Collections;
namespace TestApp {
class MyArrayList : ArrayList {
public override int Add(object value) {
base.Insert(0, value);
return 0;
}
public override void Remove(object obj) {
while(base.Contains(obj)) {
base.Remove(obj);
}
}
public override void Insert(int index, object value) {
if(index > base.Count || index < 0) index = 0;
base.Insert(index, value);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestApp {
class Program {
static void Main(string[] args) {
List<int> vals = new List<int>() {
1, 2, 3, 2, 4, 6, 5, 7, 9, 8, 8
};
var descList = vals.OrderByDescending(v => v);
foreach(int v in descList) {
Console.WriteLine(v);
}
var evenList = vals.Where(v => v % 2 == 0);
foreach(int v in evenList) {
Console.WriteLine(v);
}
Console.WriteLine("Min: {0}", vals.Min());
Console.WriteLine("Max: {0}", vals.Max());
Console.WriteLine("Avg: {0}", vals.Average());
Console.WriteLine("Sum: {0}", vals.Sum());
Console.WriteLine("Count: {0}", vals.Count());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestApp {
class Country {
public string Name;
public string Capital;
public long Population;
public override string ToString() {
return string.Format("Name: {0} \nCapital: {1} \nPopulation: {2} \n", Name, Capital, Population);
}
}
class Program {
static void Main(string[] args) {
List<Country> countries = new List<Country>() {
new Country(){ Name = "Pakistan", Capital = "Islamabad", Population=220000000},
new Country(){ Name = "India", Capital = "New Delhi", Population=1000000000},
new Country(){ Name = "Afghanistan", Capital = "Kabul", Population=35000000},
new Country(){ Name = "Iran", Capital = "Tehran", Population=81000000}
};
var q1 = countries.Where(v => v.Population >= 100 && v.Population <= 200);
foreach(Country country in q1) {
Console.Write(country);
}
var q2 = countries.Where(v => v.Name.ToLower().StartsWith("p"));
foreach(Country country in q2) {
Console.Write(country);
}
var q3 = countries.OrderBy(v => v.Name);
foreach(Country country in q3) {
Console.Write(country);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestApp {
class Student {
public string Name;
public char Gender;
public float Cgpa;
public string Discipline;
public DateTime Dob;
public override string ToString() {
return string.Format("Name: {0} \nGender: {1} \nCgpa: {2} \nDiscipline: {3} \nDob: {4} \n", Name, Gender, Cgpa, Discipline, Dob);
}
}
class Program {
static void Main(string[] args) {
List<Student> students = new List<Student>() {
new Student(){ Name="Javed", Gender='M', Cgpa=3.3f, Discipline="BSCS", Dob=new DateTime()},
new Student(){ Name="Ali", Gender='M', Cgpa=3.0f, Discipline="BSIT", Dob=new DateTime()},
new Student(){ Name="Zara", Gender='F', Cgpa=3.4f, Discipline="MSCS", Dob=new DateTime()},
new Student(){ Name="Hamza", Gender='M', Cgpa=2.3f, Discipline="BSCS", Dob=new DateTime()}
};
var q1 = students.Select(v => new { v.Name, v.Gender });
foreach(var s in q1) {
Console.WriteLine("Name: {0}, Gender: {1}", s.Name, s.Gender);
}
var q2 = students.OrderBy(v => v.Discipline).ThenByDescending(v => v.Cgpa);
foreach(var s in q2) {
Console.Write(s);
}
var q3 = students.Where(v => v.Cgpa > 3.0f).OrderBy(v => v.Name);
foreach(var s in q3) {
Console.Write(s);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestApp {
class Employee {
public string Name;
public string Gender;
public int DeptId;
public int Salary;
}
class Department {
public int Id;
public string Name;
public string City;
}
class Program {
static void Main(string[] args) {
var employees = new List<Employee>();
var departments = new List<Department>();
var q1 = employees.Join(
departments,
e => e.DeptId,
d => d.Id,
(e, d) => new { e, d }
);
foreach(var o in q1) {
Console.Write(o);
}
var q2 = employees
.Join(departments,
e => e.DeptId,
d => d.Id,
(e, d) => new { e, d })
.Where(v => v.d.City.ToLower() == "lahore")
.Select(v => new { v.e.Name, v.e.Gender, v.e.Salary, DeptName = v.d.Name });
foreach(var o in q2) {
Console.Write(string.Format("{0},{1},{2},{3}", o.Name, o.Gender, o.Salary, o.DeptName));
}
var q3 = employees.OrderBy(v => v.Salary);
foreach(var o in q3) {
Console.Write(o);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment