NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

In Program.cs file write the below code:
1)

namespace ConsoleApp1
{
public class Program
{
public static void Main()
{
Rectangle obj = new Rectangle();
obj.SetWidth(5);
obj.SetHeight(10);
Console.WriteLine("Are of the Rectangle is {0}", obj.GetArea());
}
}

class Shape
{
public int width;
public int height;
public void SetWidth(int w)
{
width = w;
}
public void SetHeight(int h)
{
height = h;
}
}
class Rectangle: Shape
{
public int GetArea()
{
return width * height;
}
}
}







2)


namespace ConsoleApp1
{
public class Program
{
public static void Main()
{
Employee emp = new Employee();
emp.GetDeptName();
emp.GetDeptLoc();
emp.GetFirstName();
emp.GetLastName();
}
}

class Department
{
public void GetDeptName()
{
Console.WriteLine("This is Department name");
}
public void GetDeptLoc()
{
Console.WriteLine("This is Department location");
}
}
class Employee: Department
{
public void GetFirstName()
{
Console.WriteLine("This is First name");
}
public void GetLastName()
{
Console.WriteLine("This is Last name");
}
}
}



3)

namespace ConsoleApp1
{
public class Program
{
public static void Main()
{
Sedan obj = new Sedan();
obj.GetVehicleName();
obj.GetCarName();
obj.GetSedanName();
}
}

class Vehicle
{
public void GetVehicleName()
{
Console.WriteLine("This is Vehicle name");
}
}
class Car: Vehicle
{
public void GetCarName()
{
Console.WriteLine("This is the Car name");
}
}
class Sedan : Car
{
public void GetSedanName()
{
Console.WriteLine("This is the sedan name");
}
}
}




4)

namespace ConsoleApp1
{
public class Program
{
public static void Main()
{
Sprite sp = new Sprite();
CocaCola co = new CocaCola();
Pepsi pe = new Pepsi();
sp.GetcoldDrink();
sp.GetSprite();
co.GetcoldDrink();
co.GetCocaCola();
pe.GetcoldDrink();
pe.GetPepsi();


}
}

class ColdDrink
{
public void GetcoldDrink()
{
Console.WriteLine("This is a Cold drink name");
}
}
class Sprite : ColdDrink
{
public void GetSprite()
{
Console.WriteLine("This is a Sprite");
}
}
class CocaCola : ColdDrink
{
public void GetCocaCola()
{
Console.WriteLine("This is a CocaCola");
}
}

class Pepsi : ColdDrink
{
public void GetPepsi()
{
Console.WriteLine("This is a Pepsi");
}
}
}




5)


namespace ConsoleApp1
{
public class Program
{
public static void Main()
{
Animal a1 = new Dog();
a1.Sing();
a1.Greet();
}
}
public class Animal
{
public Animal()
{
Console.WriteLine("Animal Constructor");

}
public void Greet()
{
Console.WriteLine("Animal says Hello");
}
public virtual void Sing()
{
Console.WriteLine("Animal song");
}
}

public class Dog : Animal
{
public Dog()
{
Console.WriteLine("Dog constructor");
}
public override void Sing()
{
Console.WriteLine("Dog song");
}
}

}






6)

namespace ConsoleApp1
{
public class Program
{
public static void Main()
{
Department d1 = new Employee();
d1.GetDeptName();
}
}
public class Department
{
public Department()
{
Console.WriteLine("Dept Constructor");
}
public virtual void GetDeptName()
{
Console.WriteLine("Dept name is DET");
}
}

public class Employee : Department
{
public Employee()
{
Console.WriteLine("Employee constructor");
}
public override void GetDeptName()
{
Console.WriteLine("I am in DET Department");
}
}

}



7) Reverse of a String

namespace ConsoleApp1
{
public class Reverse
{
public static void Main()
{
string str = " ";
string rev = " ";
int length=0;
Console.WriteLine("Enter a String");
str = Console.ReadLine();
length = str.Length - 1;
while (length >= 0) {
rev = rev + str[length];
length--;
}
Console.WriteLine("Reversed String is {0}", rev);
Console.ReadLine();

}
}

}



8)
namespace ConsoleApp1
{
public class Reverse
{
public static void Main()
{
Console.WriteLine("Enter Your first name");
String str1 = Console.ReadLine();
Console.WriteLine("Enter Your last name");
String str2 = Console.ReadLine();
Console.WriteLine(str2 + " " + str1);
}
}

}


9)
namespace ConsoleApp1
{
// Method Overloading
public class Program
{
public static void Main()
{
TestData dataClass = new TestData();
int add1 = dataClass.Add(45, 34, 67);
int add2 = dataClass.Add(23, 34);
int sub1 = dataClass.Sub(500, 200, 100);
int sub2 = dataClass.Sub(500, 200);
int mul1 = dataClass.Mul(10, 20, 20);
int mul2 = dataClass.Mul(10, 10);
int div = dataClass.Div(10, 2);
}
}

public class TestData
{
public int Add(int a, int b, int c)
{
return a + b + c;
}
public int Add(int a, int b)
{
return a + b;
}
public int Sub(int a, int b, int c)
{
return a - b - c;
}
public int Sub(int a, int b)
{
return a - b;
}
public int Mul(int a, int b, int c)
{
return a * b * c;
}
public int Mul(int a, int b)
{
return a * b;
}
public int Div(int a, int b)
{
return a / b;
}
}

}




10)

namespace ConsoleApp1
{
public interface IEmployee
{
public void GetName();
}
public class Employee : IEmployee
{
public void GetName()
{
Console.WriteLine("My name is Saptarshi Roy");
}
}
}


public class Program
{
public static void Main()
{
Employee employee = new Employee();
employee.GetName();
}
}





11)


namespace ConsoleApp1
{

abstract class AbsClass
{
public int AddTwoNumbers(int num1, int num2)
{
return num1 + num2;
}
public abstract int MultiplyTwoNumbers(int num1, int num2);
}

class Absderived : AbsClass
{
public override int MultiplyTwoNumbers(int num1, int num2)
{
return num1 * num2;

}
static void Main(String[] args)
{
Absderived calculate = new Absderived();
int added = calculate.AddTwoNumbers(10, 20);
int multiplied = calculate.MultiplyTwoNumbers(10, 20);
Console.Write("Added : {0}, Multiplied : {1}", added, multiplied);

}
}
}



12)

namespace ConsoleApp1
{

abstract class Payment
{
public void ComputeLoan()
{
Console.WriteLine("This is the Compute Loan method");
}
public abstract void ComputePrivateLoan();
public abstract void ComputeGovtLoan();
}

class PaymentDerived : Payment
{
public override void ComputePrivateLoan()
{
Console.WriteLine("This is the Compute Private Loan method");

}
public override void ComputeGovtLoan()
{
Console.WriteLine("This is the Compute Govt Loan method");

}
static void Main(String[] args)
{
Payment calculate = new PaymentDerived();
calculate.ComputePrivateLoan();
calculate.ComputeGovtLoan();


}
}
}



13)
namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
int a = 0, nofword = 1, b = 0;
string str;
Console.WriteLine("Enter a String");
str = Console.ReadLine();
while(a <= str.Length-1)
{
if (str[a] ==' ')
{
nofword++;
b++;
}
a++;
}
Console.WriteLine("Number of Word in the sentence is {0}", nofword);
//Console.WriteLine("Number of letters in the sentence are {0}", str.Count(char.IsLetter));
Console.WriteLine("Number of letters in the sentence are {0}", a-b);
}
}
}



14)
namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
int j = 5;
switch(j)
{
case 5:
Console.WriteLine(5);
break;
case 10:
Console.WriteLine(10);
break;
case 15:
Console.WriteLine(15);
break;
default:
Console.WriteLine(100);
break;
}
}
}
}



15)
namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
int i = 20;
if (i == 10)
Console.WriteLine("i is 10");
else if (i == 15)
Console.WriteLine("i is 15");
else if (i == 20)
Console.WriteLine("i is 20");
else
Console.WriteLine("i is not present");

}
}
}



16)
namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
int x = 21;
do
{
Console.WriteLine("Do While Loop");
}
while (x < 20);

}
}
}


17) Array List

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
ArrayList My_array = new ArrayList();
My_array.Add('S');
My_array.Add('A');
My_array.Add('P');
My_array.Add('T');
My_array.Add('A');
My_array.Add('R');
My_array.Add('S');
My_array.Add('H');
My_array.Add('I');

//Console.WriteLine("initial number of elements :" + My_array.Count);


//My_array.Remove('P');
//Console.WriteLine("After Remove() method the" + "number of elements: " + My_array.Count);


//My_array.RemoveAt(4);
//Console.WriteLine("After Remove() method the" + "number of elements: " + My_array.Count);

My_array.RemoveRange(1, 3);
Console.WriteLine("After RemoveRange() method the" + "number of elements: " + My_array.Count);


//My_array.Clear();
//Console.WriteLine("After Count() method the" + "number of elements: " + My_array.Count);
}
}
}



18)
namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
var arlist = new ArrayList()
{
1,
"Bill",
300,
4.5f
};

int firstElement = (int)arlist[0];
string secondElement = (string)arlist[1];
Console.WriteLine("First Element: {0}, Second Element: {1}", firstElement, secondElement);
}
}
}


19)

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
List<int> primenumbers = new List<int>();
primenumbers.Add(1);
primenumbers.Add(3);
primenumbers.Add(5);
primenumbers.Add(7);

List<int> numbers = new List<int>() { 1, 2, 5, 7, 8, 10 };
//Console.WriteLine(numbers[0]);
//Console.WriteLine(numbers[1]);
//Console.WriteLine(numbers[2]);
//Console.WriteLine(numbers[3]);

//using for loop
//for (int i = 0; i < numbers.Count; i++)
// Console.WriteLine(numbers[i]);

//using for each loop
//foreach (var number in numbers)
// Console.WriteLine(number);

numbers.ForEach(num => Console.Write(num + ","));
}
}
}

20)

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
var numbers = new List<int>() { 10, 20, 30, 40, 10 };
numbers.Remove(10); // removes the first 10 from a list
numbers.RemoveAt(2); // removes the 3rd element (index starts from 0)
foreach (var el in numbers)
Console.WriteLine(el);
}
}
}


21) Remove not necessary elements from the array

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
string[] arr = { "0", "1", "false", "2", "null", "3" };
for(int i=0;i<arr.Length;i++)
{
try
{
int val;
val = int.Parse(arr[i]);
Console.WriteLine(" " + val + " ");
}
catch
{

}
}
}
}
}


22) Reverse of an array without using reverse function

namespace ConsoleApp1
{

public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter the size of array");
int size = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[size];
int[] arr1 = new int[size];
int j = size;
Console.WriteLine("Enter values in the Array: ");
for(int i=0;i<arr.Length;i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < arr.Length; i++)
{
arr[j - 1] = arr1[i];
j--;
}
Console.WriteLine("The Final array is: ");
for (int k = 0; k < arr.Length; k++)
{
Console.WriteLine(arr1[k]+" ");
}
Console.ReadLine();

}
}
}



23) To find a specific element in the array

namespace ConsoleApp1
{

public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter the size of array");
int size = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[size];
Console.WriteLine("Enter values in the Array: ");
for(int i=0;i<arr.Length;i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Enter the value to be searched in the array: ");
int n = Convert.ToInt32(Console.ReadLine());
int pos=-1;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == n)
{
pos = i;
break;
}
}
if(pos==-1)
{
Console.WriteLine("Element is not present in the array");
}
else
{
Console.WriteLine("Element is present in the array");
}
}
}
}


24) To Check Whether the last Character of a Word is A or not

namespace ConsoleApp1
{

public class Program
{
public static int lastCharacter(string s)
{
int n = s.Length;
if (s[n - 1] == 'A' || s[n - 1] == 'a')
return 1;
else
return 0;
}
public static void Main()
{
string s = "Joshna";
int check = lastCharacter(s);
if (check == 1)
Console.WriteLine("Equal");
else
Console.WriteLine("Not Equal");
}
}
}



25) Wordlength Converter

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
string[] arr1 = { "hello", "world", "of" };
int[] arr2 = new int[arr1.Length];
for(int i=0;i<arr1.Length;i++)
{
arr2[i] = arr1[i].Length;
}
foreach(int i in arr2)
{
Console.WriteLine(i);
}

}

}
}


26) Sorted List

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
// Creating a sortedlist
// Using SortedList class
SortedList my_slist1 = new SortedList();

//Adding key/value pairs in
// SortedList using Add() method
my_slist1.Add(1.02, "This");
my_slist1.Add(1.07, "Is");
my_slist1.Add(1.04, "SortedList");
my_slist1.Add(1.01, "Understanding");

foreach(DictionaryEntry pair in my_slist1)
{
Console.WriteLine("{0} and {1}", pair.Key, pair.Value);
}
Console.WriteLine();
}

}
}


27) Here the key is a string but it will sort as well

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
// Creating another sortedlist
// using Object Initializer Syntax
SortedList my_slist2 = new SortedList()
{
{"b.09",234 },
{"b.11",395 },
{"b.01",405 },
{"b.67",100 } };

foreach(DictionaryEntry pair in my_slist2)
{
Console.WriteLine("{0} and {1}", pair.Key, pair.Value);
}
}

}
}


28) Using Remove and RemoveAt in Sortedlist

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
// Creating a sortedlist
// Using SortedList class
SortedList my_slist1 = new SortedList();

//Adding key/value pairs in
// SortedList using Add() method
my_slist1.Add(1.02, "This");
my_slist1.Add(1.07, "Is");
my_slist1.Add(1.04, "SortedList");
my_slist1.Add(1.01, "Understanding");

foreach (DictionaryEntry pair in my_slist1)
{
Console.WriteLine("{0} and {1}", pair.Key, pair.Value);
}
Console.WriteLine();

//Remove & RemoveAt
my_slist1.Remove(1.07);
my_slist1.RemoveAt(2);

foreach (DictionaryEntry pair in my_slist1)
{
Console.WriteLine("{0} and {1}", pair.Key, pair.Value);
}

}

}
}


29) ContainsKey() and ContainsValue() method in Sortedlist

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
// Creating a sortedlist
// Using SortedList class
SortedList my_slist1 = new SortedList();

//Adding key/value pairs in
// SortedList using Add() method
my_slist1.Add(1.02, "This");
my_slist1.Add(1.07, "Is");
my_slist1.Add(1.04, "SortedList");
my_slist1.Add(1.01, "Understanding");

foreach (DictionaryEntry pair in my_slist1)
{
Console.WriteLine("{0} and {1}", pair.Key, pair.Value);
}
Console.WriteLine();

// Using ContainsKey() method to check
// the specified key is present or not

if(my_slist1.ContainsKey(1.02)==true)
{
Console.WriteLine("Key is found...!!");
}
else
{
Console.WriteLine("Key is found...!!");
}

// Using ContainsValue() method o check
//the specified value is present or not
if(my_slist1.ContainsValue("Is")==true)
{
Console.WriteLine("Value is found...!!");
}
else
{
Console.WriteLine("value is not found....!!");
}

}

}
}


30) Creating a Dictionary

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
// Creating a Dictionary
// Using Dictionary<TKey, TValue> class
Dictionary<int, string> My_dict1 = new Dictionary<int, string>();

// Adding key/value pairs in
// the Dictionary
// Using Add() method
My_dict1.Add(1123,"Welcome");
My_dict1.Add(1124, "to");
My_dict1.Add(1125, "Dictionary");

foreach (KeyValuePair<int,string> ele1 in My_dict1)
{
Console.WriteLine("{0} and {1}", ele1.Key, ele1.Value);
}
Console.WriteLine();

}

}
}

31) ContainsKey() and ContainsValue() method in Dictionary

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
// Creating a Dictionary
// Using Dictionary<TKey, TValue> class
Dictionary<int, string> My_dict1 = new Dictionary<int, string>();

// Adding key/value pairs in
// the Dictionary
// Using Add() method
My_dict1.Add(1123,"Welcome");
My_dict1.Add(1124, "to");
My_dict1.Add(1125, "Dictionary");

foreach (KeyValuePair<int,string> ele1 in My_dict1)
{
Console.WriteLine("{0} and {1}", ele1.Key, ele1.Value);
}
Console.WriteLine();

// Using ContainsKey() method to check
// the specified key is present or not
if(My_dict1.ContainsKey(1123)==true)
{
Console.WriteLine("Key is found.....!!");
}
else
{
Console.WriteLine("Key is not found.....!!");
}

// Using ContainsValue() method to check
// the specified value is present or not
if (My_dict1.ContainsValue("Welcome") == true)
{
Console.WriteLine("value is found.....!!");
}
else
{
Console.WriteLine("Value is not found.....!!");
}

}

}
}


32) Creation of a Stack

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
// Create a Stack
// Using Stack class
Stack my_stack = new Stack();

// Adding elements in the stack
// Using Push method
my_stack.Push("Item1");
my_stack.Push("Item2");
my_stack.Push("Item3");
my_stack.Push("Item4");

Console.WriteLine("Total elements present in" + "my_stack: {0}", my_stack.Count);

my_stack.Pop();
// After pop method
// LIFO - Last in First Out
Console.WriteLine("Total elements present in" + "my_stack: {0}", my_stack.Count);
foreach(string val in my_stack)
{
Console.WriteLine(val);
}

// Remove all the elements
// from the stack
my_stack.Clear();
Console.WriteLine("Total elements present in" + "my_stack: {0}", my_stack.Count);
}

}
}

33) Creation of a Queue, Adding elements and Removing Elements

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
// Create a Queue
// Using Queue class
Queue my_queue = new Queue();

// Adding elements in the queue
// Using Enqueue method
my_queue.Enqueue("Item1");
my_queue.Enqueue("Item2");
my_queue.Enqueue("Item3");
my_queue.Enqueue("Item4");
my_queue.Enqueue("Item5");
Console.WriteLine("Contents of my_queue are: ");
foreach(string val in my_queue)
{
Console.WriteLine(val);
}
Console.WriteLine("Total elements present in" + "my_queue: {0}", my_queue.Count);

my_queue.Dequeue();
// After Dequeue method
Console.WriteLine("Total elements present in" + "my_queue: {0}", my_queue.Count);
Console.WriteLine("After Dequeue The elements in the queue: ");
foreach (string val in my_queue)
{
Console.WriteLine(val);
}
Console.WriteLine();
// Remove all the elements
// from the stack
my_queue.Clear();
Console.WriteLine("Total elements present in" + "my_queue after Dequeue: {0}", my_queue.Count);
}

}
}


34) Multiplication Table of a Number

namespace ConsoleApp1
{

public class Program
{
public static void Main()
{
int n;
Console.WriteLine("Input the number: ");
n = Convert.ToInt32(Console.ReadLine());
for(int i=1;i<=10;i++)
{
Console.WriteLine("{0} * {1} = {2}", n, i, n * i);
}

}

}
}


35)

namespace ConsoleApp1
{

public class Program
{
public static bool test(string str)
{
int counter = 0;
for (int i = 0; i < str.Length;i++)
{
if (str[i]=='z')
{
counter++;
}
}
return counter > 1 && counter <= 4;
}
public static void Main()
{
Console.WriteLine(test("frizz"));
Console.WriteLine(test("zname"));
Console.WriteLine(test("Zazz"));
Console.WriteLine(test("false"));
Console.WriteLine(test("zzzz"));

}

}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.