C#
What is C#?
- C# (pronounced “C sharp”).
- C# is a new programming language designed by Microsoft to work with the .NET Framework.
- C# is a simple, modern, object oriented, and type-safe programming Language derived from C and C++.
- C# .NET enables developers to build solutions for the broadest range of clients, including Web; Microsoft Windows Forms based applications, and smart devices.
What's the top .NET class that everything is derived from?
Using namespace System.Object.
What is the difference between String and string?
No difference.
What type of code (client or server) is found in a Code-Behind class?
Server
No difference.
What type of code (client or server) is found in a Code-Behind class?
Server
What’s the .NET collection class that allows an element to
be accessed using a unique key?
HashTable.What is mean by variable?
A variable is the name given to a memory location holding a particular type of data.
Example:- int i;
Bitwise & Logical Operator in CSharp?
Bitwise
|
Logical
|
AND ( &)
|
AND (
&&)
|
OR (|)
|
OR (||)
|
XOR ()
|
|
NOT (!)
|
What
is Type Safe Language?
|
Type
Safe:-
Value of particular data type can only be stored in their respective data type. E.g. You cannot store integer value in Boolean data type. |
Boolean
data type
|
The bool keyword is an CSharp Runtime type : System.Boolean.
It is
used to declare variables to store the Boolean values, true and false.
For
Example:-
CSharp declaration for example: bool flag; CSharp Initialization for example : flag = true; Note:- Is CSharp default initialization value of Boolean is false |
What is Local Variables?
|
Local Variable :- variable defines in a method is
called Local Variable
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
int age;
}
}
}
|
What is concept of Boxing
& Unboxing?
|
Boxing is used to convert Value Type to Reference Type.
E.g. int a=1;
Object obj=a;
Unboxing is used to convert Reference type to Value Type.
E.g. . int a=1;
Object obj=a;
int b=(int)obj;
|
What
is Value Type & Reference Type?
|
|
Value Type
|
Reference Type
|
A
Value Type holds its actual value in memory allocated on the Stack
|
Value is allocated in Heap
|
Not null.
|
Assigned to null.
|
Not GC.
|
|
Value type include:-
All intrinsic data type
(e.g System.SByte, System.Byte
System.Int16 ,System.Int32
System.Int64 ,System.Single System.Double ,System.Decimal System.Char ,System.Boolean System.DateTime etc)
Struct, Enumeration.
|
Reference Type include:-
Class,
object,
string,
Array,
Delegate,
Interface
|
What
is Copy & Clone ?
|
|
Copy
|
Clone
|
Copy method creates Deep Copy.
|
Clone method creates Shallow Copy.
|
Deep copy means copy structure(Index) as well as data
|
Shallow copy means copy only structure. Does not copy any
data
Example:-copy the struct. Of the dataset, including all data Table schemas, relations ,& constraints.
|
What's the difference between the
System.Array.CopyTo() and System.Array.Clone()?
|
System.Array.CopyTo() is a deep copy of the array,
System.Array.Clone()
is shallow copy of the array.
|
How can you sort the elements of
the array in descending order?
|
By calling Sort() and then Reverse() methods.
|
What
is String & String Builder?
|
|||||||||
String
|
String Builder
|
||||||||
System.Text;
|
|||||||||
String is nothing but Immutable.
|
String Builder is nothing but mutable.
|
||||||||
Immutable
mean’s During runtime create new
string & deleted old string .(every
time create new string)
|
Mutable means During runtime create new string & automatic size increase.
|
||||||||
String class is static in nature.
|
String Builder class is Dynamic in nature.
|
||||||||
Example:-
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
ConsoleApplication8
{
class Program
{
static void Main(string[]
args)
{
string
str = "a";
str += "b";
str += "c";
str += "d";
Console.WriteLine(str);
Console.ReadLine();
}
}
}
Output:-
Note:-means
During runtime create new
string & deleted old string.
|
Example:-
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
ConsoleApplication8
{
class Program
{
static void Main(string[]
args)
{
//string str = "a";
//str
+= "b";
//str
+= "c";
//str
+= "d";
StringBuilder sb = new
StringBuilder();
sb = Append("a");
sb = Append("b");
sb = Append("c");
sb = Append("d");
string res = sb.ToString();
Console.WriteLine("capacity="+sb.MaxCapacity);
Console.WriteLine(res);
Console.ReadLine();
}
}
}
Output:-
Note:-means
During runtime create new string &
automatic size increase.
|
||||||||
How to reverser string without using (Reverse) key word?
OR
How to reverser string with using StringBuilder class?
|
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static
void Main(string[]
args)
{
//By using without any keyword(reverse)
to reverser string
// string input = "HELLO NISHI";
// string output = "";
// for (int i = input.Length - 1; i >= 0;
i--)
// {
// output += input[i];
// }
// Console.WriteLine("Reverse String
Is {0}", output);
// Console.Read();
//By using stringBuilder to
reverser string
string
Name = "NISHI";
char[]
characters = Name.ToCharArray();
StringBuilder
sb = new StringBuilder();
for
(int i = Name.Length - 1; i >= 0; --i)
{
sb.Append(characters[i]);
}
Console.Write(sb.ToString());
Console.Read();
}
}
}
|
Program : Swapping 2 numbers Without temp
variable
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static
void Main(string[]
args)
{
Console.WriteLine("/****** Program to swap 2 numbers without using temporary variable *******/");
int
a = 10, b = 20;
Console.WriteLine(" Values before swapping a={0} , b={1}",
a, b);
a = a + b;
b = a - b;
a = a - b;
Console.WriteLine(" Values after swapping a={0} , b={1}",
a, b);
Console.ReadKey();
}
}
}
OutPut
Values before swapping a=10 ,
b=20
Values after swapping a=20 ,
b=10
Or
a = a * b;
b = a / b;
a = a / b;
we get same output
|
Program : Swapping 2 numbers With temp variable |
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static
void Main(string[]
args)
{
Console.WriteLine("/****** Program to swap 2 numbers using temporary
variable *******/");
int
a = 10, b = 20;
Console.WriteLine(" Values before swapping a={0} , b={1}",
a, b);
int
tempVar = 0; //temporary variable
tempVar = a;
a = b;
b = tempVar;
Console.WriteLine(" Values after swapping a={0} , b={1}",
a, b);
Console.ReadKey();
}
}
}
//OutPut
//Values before
swapping a=10 , b=20
//Values after
swapping a=20 , b=10
|
C# Find repeated STRING
in an array
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace FindDuplicatStringAndCount
{
class Program
{
static
void Main(string[]
args)
{
string[] arr = { "twinkle",
"twinkle", "little", "star",
"How", "I",
"know", "who",
"are", "you",
"twinkle", "twinkle", "little",
"star" };
var
query = from f in
arr
group f by f into da
select da;
int maximum = arr.Count();
foreach (IGrouping<string, string>
intagroup in query)
{
Console.WriteLine("Key={0},Repeated
{1} Times", intagroup.Key, intagroup.Count());
}
Console.ReadKey();
}
}
}
////OUTPUT
//Key=twinkle,Repeated
4 Times
//Key=little,Repeated
2 Times
//Key=star,Repeated
2 Times
//Key=How,Repeated
1 Times
//Key=I,Repeated
1 Times
//Key=know,Repeated
1 Times
//Key=who,Repeated
1 Times
//Key=are,Repeated
1 Times
//Key=you,Repeated
1 Times
|
Q. What is a delegate? Why?Use
of delegate? Example of delegate?
|
|||||
Delegate
:-System.MultiCastDelegate
“Delegate is function pointer which used to invoke method at runtime”.
OR
“Delegate
means is a way of telling csharp which method to call when an event is
triggered (occurred).”
Access
Modifiers:-
can be Public
& Internal
1.
Callback
method(method calling another method)
2 . Event.
3.
Threading.
4.
Sorting.
1.
Unicast
Delegate (Single Cast Delegate).
2.
Multicast
Delegate.
Step-I(Declaring a Delegate)
Step-II(creating Delegate Object)
Step-III(Invoking Delegate object)
Generally example:-There are 50 employee in ABC
company, & market one of new technologies come, & company want to
learn this technologies to his all employee .But all 50 employee goes (particular
place) & learn this technologies, it is very costly, less performance , required
more time etc.
So delegate means
just send one represented person,
he learn new technologies, come back & he teach to all remaining
employee. Save time, cost as well as more performance.
OR
Technically
example: - If you
click a button on a form, the program would call a specific method. It is
this pointer which is a delegate.
|
Q. What are the types of delegate?
|
|||
Type of
Delegate:-
1.
Unicast
Delegate (Single Cast Delegate).
2.
Multicast
Delegate.
“The delegate that refer to a single method is called single Cast
Delegate”
“The
delegate that refer to a more than one
method is called Multicast Delegate”
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
public delegate void MyDel( ); //step-1 Declaring a Delegate
public class MulticastDemo
{
public static void Method1()
{
Console.WriteLine("Hello
Delegate!!Method 1");
}
public static void Method2()
{
Console.WriteLine("Multicast
Delegate demo!!Method 2");
}
public static void Method3()
{
Console.WriteLine("Calling
method3");
}
}
class Program
{
static
void Main(string[]
args)
{
MyDel md = new MyDel(MulticastDemo.Method1);//step-2 creating
Delegate Object
md + = new MyDel(MulticastDemo.Method2);
md + = new MyDel(MulticastDemo.Method3);
md.Invoke();//step-3 Invoking Delegate object
//md();
Console.ReadLine();
}
}
}
Pluse(+)
Operator :-Used
for add method.(see in above example)
Manus(-)
Operator :-Used
for Remove method.
|
How
to use C# exceptions statements
|
||
“An exception is a runtime error that arises because of some
unexpected situation occurs & interrupts execution of program.”
Example:-
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace exceptionexample
{
class Program
{
static void Main(string[] args)
{
int a = 10, b = 0, result;
result = a / b;//error occure during Runtime divided by zero
Console.WriteLine(result);
Console.ReadLine();
}
}
}
Above program is
logically correct but at Runtime error occurs. But customer don’t know what
is error occurs during runtime .So that we used exception.
Why exception Hand used
Ø
To avoid termination of program.
Ø
Client to identify the error.
C# Exception handling uses the try, catch,
and finally keywords to attempt actions that may not succeed, to handle
failures, and to clean up resources afterwards.
try
{
//your code here
}
Catch (exception type)
{
//your code here
}
Finally block
The
code in the finally block will execute even if there is no Exceptions.That
means if you write a finally block , the code should execute after the
execution of try block or catch block(first
execute try, second execute catch, then finally).
try
{
//your code here
}
Catch (exception type)
{
//if the exception occurred
//your code here
}
finally
{
//your code here
}
From
the following CSharp code , you can understand how to use try..catch statements. Here we are going to divide a
number by zero.
When you execute this C# code the above source code , the program will throw aDividedByZero Exception and after that the control wil go to finally clause.
In C# exception are usually of the two
types as following
1.
System-Defined Exception
2.
User-Defined Exception
System-Defined Exception
The
exception defined the .NET (BCL) are as following.
System.
DivideByZeroException
System.
FormatException
System.
IndexOutOfRangeException.
System.
OverflowException.
System.
NullReferenceException.
System.
StackOverflowException.
System.
ArgumentException.
|
Lambda Expression(C# Programming)
|
A
lambda expression is
an anonymous function that can contain statements and expressions, and can be
used to create delegates or expression
tree types.
All
lambda expressions use the lambda operator =>, which is read as "goes
to".
Syntax:-
Delegate_name obj=x(L.H.S) => X*X(R.H.S)
L.H.S specified InPut parameter.
R.H.S specified OutPut parameter(holds the expression
or statement block).
For Example
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
//step-1 Declaring a Delegate
public delegate
int MyDel(int a);
class Program
{
static void Main(string[] args)
{
//step-II lambda expression
MyDel md = x => x * x;//lambda expression is read "x goes
to x times x.
Console.WriteLine(md(9));
Console.ReadLine();
}
}
}
Output:-81
|
LINQ Query
Expressions (C# Programming)
|
|||
using System.Linq;
There are Four categories
1.
LINQ TO OBJECT:- For handling a LINQ query against a
collection of objects
-Filtering operators(OfType())
-Sorting operators
-Join operators
-Projection operators
-Grouping Operator
-Quantification(All, Any
& Contains)
-Set Operators(Intersect
, Union & Distinct)
-Aggregate
Operator(max(), min(), sum() , count() )
2.
LINQ to DataSets - For handling LINQ queries against
ADO.NET DataSets.
3. LINQ to SQL - For handling LINQ queries against
Microsoft SQL Server.
4.
LINQ
to XML - For
handling an XPATH query against XML documents
Why use LINQ
A query is an expression that retrieves data from a
data source.
Different languages
have been developed over time for the various types of data sources, for example SQL for relational
databases and XQuery for XML. Therefore,
developers have had to learn a new query language for each type of data
source or data format that they must support, so that we used LINQ.
Example
All LINQ query
operations consist of three distinct actions:
Action-I:
- Obtain the data source.
Action-II: - Create the query.
Start with from where & select .
Action-III:
- Execute the query in a foreach statement.
The following example
shows how the three parts of a query operation are expressed in source code.
|
What is difference between =(equal to) & ==(double equal to)
Ans:-
Ans:-
String s1 = new String("Hello");
String s2 = new String("Hello");
boolean b1 = ( s1 == s2 ) ; // false: s1 and s2 point to different objects
boolean b2 = ( s1.equals(s2) ) ; // true: s1 and s2 both represent the same
// piece of text - "Hello"
Thank you