Introduction to ASP.
NET & C# (Practical)
Program-1. Write a Program, Create a simple console application in C# ,
learning of consecrating basic building blocks of a console application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hello World");
Console.ReadKey();
}
}
}
OUTPUT: Hello World
Program-2. Write a program to table lists difference between array and arraylist in c#.
Arrays: An array is a group of like-typed variables that are referred to by a common
name. Example:
// C# program to demonstrate the Arrays
using System;
class GFG {
// Main Method
public static void Main(string[] args)
{ // creating array
int[] arr = new int[4];
// initializing array
arr[0] = 47;
arr[1] = 77;
arr[2] = 87;
arr[3] = 97;
// traversing array
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i]);
}
}
}
Output:
47
77
87
97
ArrayList: ArrayList represents an ordered collection of an object that can be indexed
individually. It is basically an alternative to an array. It also allows dynamic memory
allocation, adding, searching and sorting items in the list. Example:
// C# program to illustrate the ArrayList
using System;
using System.Collections;
class GFG {
// Main Method
public static void Main(string[] args)
{
// Create a list of strings
ArrayList al = new ArrayList();
al.Add("Ajay");
al.Add("Ankit");
al.Add(10);
al.Add(10.10);
// Iterate list element using foreach loop
foreach(var names in al)
{
Console.WriteLine(names);
}
}
}
Output:
Ajay
Ankit
10
10.1
Difference Between Array and ArrayList
Feature Array ArrayList
This has fixed size and can’t increase or Size can be increase or decrease
Memory
decrease dynamically. dynamically.
ArrayList belongs
Namespace Arrays belong to System.Array namespace
to System.Collection namespace.
In Arrays, we can store only one datatype In ArrayList we can store different
Data Type
either int, string, char etc. datatype variables.
Feature Array ArrayList
Operation Insertion and deletion operation in
Insertion and deletion operation is fast.
Speed ArrayList is slower than an Array.
Arrays are strongly typed which means it
Typed can store only specific type of items or ArrayList are not strongly typed.
elements.
null Array cannot accept null. ArrayList can accepts null.
Program-3. Write a program to combine two arrays without duplicate value in
C #, using the Union() method.
using System;
using System.Collections.Generic;
using System.Linq;
class GFG{
public static void Main()
{
// Declare first array with integer numbers
int[] array1 = { 22, 33, 21, 34, 56, 32 };
// Declare second array with integer numbers
int[] array2 = { 24, 33, 21, 34, 22 };
// Displaying array 1
Console.WriteLine("Array 1: ");
foreach (int x1 in array1)
{
Console.WriteLine(x1);
}
// Displaying array 2
Console.WriteLine("Array 2: ");
foreach (int x2 in array2)
{
Console.WriteLine(x2);
}
// Combine the unique elements and convert into array
var final = array1.Union(array2).ToArray();
// Display the elements in the final array
Console.WriteLine("New array:");
Array.ForEach(final, i => Console.WriteLine(i));
}
}
Output:
Array 1:
22
33
21
34
56
32
Array 2:
24
33
21
34
22
New array:
22
33
21
34
56
32
24
Program-4. Write a program to remove duplicate values from an array in C# in
order to get distinct values.
// C# program to remove duplicate elements from the array
using System;
using System.Linq;
class GFG{
public static void Main()
{
// Declare an array of integer type
int[] data = { 10, 20, 230, 34, 56, 10, 12, 34, 56, 56 };
Console.WriteLine("Array before removing duplicate values: ");
Array.ForEach(data, i => Console.WriteLine(i));
// Use distinct() function
// To create an array that contain distinct values
int[] unique = data.Distinct().ToArray();
// Display the final result
Console.WriteLine("Array after removing duplicate values: ");
Array.ForEach(unique, j => Console.WriteLine(j));
}
}
Output:
Array before removing duplicate values:
10
20
230
34
56
10
12
34
56
56
Array after removing duplicate values:
10
20
230
34
56
12
Program-5. Write a program to count the total number of elements or some
specific elements in the array using an extension method Count() method.
using System;
using System.Linq;
class GFG{
static public void Main()
{
// Creating a count variable
var total = 0;
// creating array of numbers
int[] nums = { 9, 6, 5, 2, 1, 5, 8, 4,
6, 2, 3, 4, 8, 7, 5, 6 };
// Counting the number of elements
total = nums.Count();
// Displaying the count
Console.WriteLine(total);
}
}
Output
16