(1) Example
of Boxing and Un-boxing?
static void Main(string[] args)
{
int N = 15;
// boxing
object obj = N;
// unboxing
int i = (int)obj;
// Display result
Console.WriteLine("Value of
object is : " + obj);
Console.WriteLine("Value of i
is : " + i);
Console.ReadLine();
}
(2) Lambda
Expression Example?
class StudentList
{
public int StudentId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
static void Main(string[] args)
{
IList<StudentList>
studentList = new List<StudentList>()
{
new StudentList(){StudentId =1,
Name="Anil", Age=20},
new StudentList() { StudentId = 2,
Name = "Ajay", Age = 30 },
new StudentList() { StudentId = 3,
Name = "Boss", Age = 40 },
new StudentList() { StudentId = 4,
Name = "Cag", Age = 50 },
};
var Names = studentList.Select(x
=> x.Name);
foreach (var Name in Names)
{
Console.WriteLine(Name);
}
Console.Read();
}
}
(3) Example
of Delegate?
class Program
{
delegate int Calculator(int n);
static int number = 20;
public static int add(int n)
{
number = number + n;
return number;
}
public static int mul(int n)
{
number = number * n;
return number;
}
public static int sub(int n)
{
number = number - n;
return number;
}
public static int getNumber()
{
return number;
}
public static void Main(string[]
args)
{
Calculator c1 = new
Calculator(add);
Calculator c2 = new
Calculator(mul);
Calculator c3 = new Calculator(sub);
c1(20);
Console.WriteLine("c1
delegate number is: " + getNumber());
c2(3);
Console.WriteLine("c2
delegate number is: " + getNumber());
c3(15);
Console.WriteLine("c3
delegate number is: " + getNumber());
Console.ReadLine();
}
}
(4) Example
of Multilevel Inheritance?
class Program
{
public class Employee
{
public int salary = 40000;
}
public class Programmer : Employee
{
public int bonus = 10000;
}
public class developer:Programmer
{
public int Hrd = 1200;
}
static void Main(string[] args)
{
developer p1 = new
developer();
Console.WriteLine("Salary:
" + p1.salary);
Console.WriteLine("bonus:
" + p1.bonus);
Console.WriteLine("Hrd:"
+ p1.Hrd);
Console.ReadLine();
}
}
(5) First
Duplicate value is a given an array { 3, 6, 9, 7, 1, 9, 5, 7 }?
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 3, 6, 9, 7,
1, 9, 5, 7 };
PrintFirstDuplicateValue(arr);
Console.ReadKey();
}
private static void
PrintFirstDuplicateValue(int[] arr)
{
for(int i=0;i<arr.Length-1;i++)
{
for(int
j=i+1;j<arr.Length;j++)
{
if (arr[i] == arr[j])
{
Console.WriteLine("First Duplicate Value is:" + arr[i]);
return;
}
}
}
}
}
(6) Palindrome
or Not you can take 6 words?
static void
Main(string[] args)
{
string[] Strng = {
"ANIL", "VASU", "MADAM", "ANJI",
"KRISH",
"MALAYALAM"};
foreach(var str in Strng)
{
string revstr = "";
for(int
i=str.Length-1;i>=0;i--)
{
revstr += str[i].ToString();
}
if(revstr==str)
{
Console.WriteLine("Palindrome is:{0}",str,revstr);
}
else
{
Console.WriteLine("Not
a palindrome:{0}",str,revstr);
}
}
Console.ReadLine();
}
(7) CallByRef and CallByOut Example?
class RefOut
{
static void Main(string[] args)
{
int val = 4;//Given to the reference and out
Refoutclass obj = new Refoutclass();//Instance of class Refoutclass
obj.Add(ref val); //Called Method1
Console.WriteLine("Reference Value is:{0}",val);
obj.Multiplic(out int val1); //Called Method2
Console.WriteLine("Out Value is:{0}",val1);
Console.ReadLine();
}
}
class Refoutclass
{
public void Add(ref int a) //Calling Method1
{
a += a;
}
public void Multiplic(out int b) //Calling Method2
{
//out- to print the multiple values.
int s = 6; //initializing the value.
b = s;
b *= b; //Method or Logic Execution
}
}
{
static void Main(string[] args)
{
int val = 4;//Given to the reference and out
Refoutclass obj = new Refoutclass();//Instance of class Refoutclass
obj.Add(ref val); //Called Method1
Console.WriteLine("Reference Value is:{0}",val);
obj.Multiplic(out int val1); //Called Method2
Console.WriteLine("Out Value is:{0}",val1);
Console.ReadLine();
}
}
class Refoutclass
{
public void Add(ref int a) //Calling Method1
{
a += a;
}
public void Multiplic(out int b) //Calling Method2
{
//out- to print the multiple values.
int s = 6; //initializing the value.
b = s;
b *= b; //Method or Logic Execution
}
}
}
(8) Remove Duplicate Characters
of a strings (“Google”, “Assistant”)?
class Program
{
static
void Main()
{
string
value1 = StringChar("Csharpstar");
string
value2 = StringChar("Assistant ");
string
value3 = StringChar("Google ");
Console.WriteLine(value1);
Console.WriteLine(value2);
Console.WriteLine(value3);
Console.ReadLine();
}
private
static string StringChar(string Char)
{
string
result = "";
foreach
(var value in Char)
if
(result.IndexOf(value) == -1)
result
+= value;
return
result;
}
}
Remove Duplicate Characters of a strings?
string[] str = { "Assistant", "Csharpstar", "Google" };
foreach(var item in str)
{
var word = new HashSet<char>(item);
foreach(var remove in word)
{
Console.Write(remove);
}
Console.WriteLine();
}
Console.ReadLine();
Remove Duplicate Characters of a strings?
string[] str = { "Assistant", "Csharpstar", "Google" };
foreach(var item in str)
{
var word = new HashSet<char>(item);
foreach(var remove in word)
{
Console.Write(remove);
}
Console.WriteLine();
}
Console.ReadLine();
(9) Static Example?
class MyOffice
{
static
string officename;
static
string Address;
static
MyOffice()
{
officename
= "xyz";
Address
= "123";
}
static
void Main(string[] args)
{
Console.WriteLine(MyOffice.officename);
Console.WriteLine(MyOffice.Address);
Console.ReadKey();
}
}
(10) StreamWriter and StreamReader
example?
class
Program
{
static
void Main(string[] args)
{
//Reader Example
FileStream f = new FileStream("C:\\Desktop\\C# Concept Word\\out.txt", FileMode.OpenOrCreate);
//Reader Example
FileStream f = new FileStream("C:\\Desktop\\C# Concept Word\\out.txt", FileMode.OpenOrCreate);
StreamReader s = new StreamReader(f);
string line = "";
while((line=s.ReadLine())!=null)
{
Console.WriteLine(line);
}
s.Close();
f.Close();
string line = "";
while((line=s.ReadLine())!=null)
{
Console.WriteLine(line);
}
s.Close();
f.Close();
Console.ReadLine();
}
}
public
static void WriteToFile()
{
FileStream
f = new FileStream("C:\\Users\\Desktop\\out.txt", FileMode.Create);
StreamWriter s = new StreamWriter(f);
s.WriteLine("Hello Anil");
s.WriteLine("How r u");
s.Close();
f.Close();
Console.WriteLine("File created successfully...");
StreamWriter s = new StreamWriter(f);
s.WriteLine("Hello Anil");
s.WriteLine("How r u");
s.Close();
f.Close();
Console.WriteLine("File created successfully...");
Console.ReadLine();
}
}
}
(11) Update indexes of an array int
based?
static void Main(string[] args)
{
int[]
arr = new int[] { 3, 4, 5 };
Console.WriteLine("Original
array");
for
(int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]
+ "");
}
arr[1]
= 5;
arr[2]
= 6;
Console.WriteLine("Updated");
for
(int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]
+ "");
}
Console.ReadLine();
Update indexes of an
array String based?
string[]
str = { "Ali", "Ajay", "Boss" };
Console.WriteLine("Original
array IS");
for(int
i=0;i<str.Length;i++)
{
Console.WriteLine(str[i] + "");
}
str[1]
="Krish";
str[2]
= "Vasu";
Console.WriteLine("Updated
Array IS");
for(int
j=0;j<str.Length;j++)
{
Console.WriteLine(str[j]+"");
}
Console.ReadLine();
}
No comments:
Post a Comment