C#.NET Interview
Questions For Exp. Developers
What is the difference between UTF,
ASCII and ANSI code format of encoding?
*ASCII vs ANSI*
We commonly refer to character
encoding as a letter's "ASCII value,"
when we really mean "ANSI
value." A lot of the time that's sufficient,
but in fact the ASCII standard is
pretty much obsolete.
ASCII (American Standard Code for
Information Interchange) is a 7-bit
standard that has been around since
the late 1950s (its current
incarnation dates from 1968). It
defines 128 different characters, which
is more than enough for English:
upper- and lowercase letters,
punctuation, numerals, control codes
(remember control-c?), and
nonprinting codes such as tab,
return, and backspace.
ASCII and ANSI are pretty good as
long as you are western European.
These two mappings are extremely
limited in that they may only code
(i.e. assign a number to) 256
letters, so that there is no space to
include other glyphs from other
languages.
*Unicode *
Unicode fixes the limitations of
ASCII and ANSI, by providing enough
space for over a million different
symbols. Like the above two systems,
each character is given a number, so
that Russian ? is 042F, and the
Korean won symbol ? is 20A9. (Note
that all Unicode numbers are
Hexadecimal, meaning that one counts
by 16’s not 10’s, not a problem as
users really don’t need to know the
mapping numbers anyway.) So,
although not yet totally
comprehensive, Unicode covers most of the
world’s writing systems. Most
importantly, the mapping is consistent, so
that any user anywhere on any
computer has the same encoding as everyone
else, no matter what font is being
used.
So Unicode is a map, a chart of
(what will one day be) all of the
characters, letters, symbols,
punctuation marks, etc. necessary for
writing all of the world’s languages
past and present.
What is the difference between *UTF-8,
UTF-16*?
UTF-8 uses variable byte to store a
Unicode. In different code range, it
has its own code length, varies from
1 byte to 6 bytes. Because it
varies from 8 bits (1 byte), it is
so called "UTF-8". UTF-8 is suitable
for using on Internet, networks or
some kind of applications that needs
to use slow connection.
Unicode (or UCS) Transformation
Format, 16-bit encoding form. The UTF-16
is the Unicode Transformation Format
that serializes a Unicode scalar
value (code point) as a sequence of
two bytes, in either big-endian or
little-endian format. Because it is
grouped by 16-bits (2 bytes), it is
also called "UTF-16",
which is the most commonly used standard.
How to initialize a default value
when you know its data type?
Using default keyword we can
initialize default value for its type like
as shown below:
public int amount = default(System.Int32);
The default key word is normally
used to initialise a member in
a *generic classes*. In generic
classes and methods, one issue that
arises is how to assign a default
value to a parameterized type T when
you do not know the following in
advance:
1. Whether T will be a reference type or a
value type.
2. If T is a value type, whether it will be a
numeric value or a struct.
For example, here’s an example from
MSDN help
publicclass GenericList
{
private class Node
{
//
public Node Next;
public T Data;
}
private Node head;
public T GetNext()
{
*T temp = **default**(T);*
}
}
The type '*T*' is parameterised type
for the template. With the line in
bold the type '*T*' could be a
numeric value (so the variable should be
initialised to '*0*') or a reference
(and so should be initialised to
NULL). Using default here makes sure
the correct initialisation takes place.
How to design a class (or
data type) which sorts its data either ascending or descending by default.
This can be achieved by using
*Array**.BinarySearch.*
*Prerequisite to read this FAQ*
1. Understanding of bitwise complement (also
known as 1’s complement).
The equivalent the bitwise complementoperator in C# is "~"
(*tilde*) and in VB.NET “*XOR*”
In general we use
*Array**.BinarySearch* to find the element index in
given array. But if you understand
about return value
of *Array**.BinarySearch* function,
we can build sorted array too. Even
Microsoft used the same technic in
implementing
*System.Collections.SortedList*
class.
*Below is definition from MSDN about
Array.BinarySearch
*The index of the specifiedvaluein
the specifiedarray, ifvalueis found.
Ifvalueis not found andvalueis less
than one or more elements inarray, a
negative number which is the bitwise
complement of the index of the
first element that is larger
thanvalue. Ifvalueis not found andvalueis
greater than any of the elements
inarray, a negative number which is the
bitwise complement of (the index of
the last element plus 1).**
It means, it returns the index of
found element otherwise it returns the
expected index position in bitwise
complement.
Let’s see same thing in action.
Below sample code (MySortedList class)
acts like integer list and sorts its
elements in ascending order
dynamically whenever new element is
added.
*Sample Code: MySortedList*
class Program
{
static void Main(string[] args)
{
MySortedList list = new MySortedList();
list.Add(10);
list.Add(8);
list.Add(7);
list.Add(7);
list.Add(20);
list.Add(16);
list.Add(1);
foreach (int v in list.Items)
{
Console.WriteLine(v);
}
Console.ReadLine();
}
}
class MySortedList
{
int[] items = new int[0];
public int[] Items
{
get { return items; }
set { items = value; }
}
public void Add(int value)
{
int index = Array.BinarySearch(items,
value);
if (index < 0)
index = ~index;
//Increase Items Array Size by 1
int lastIndex = items.Length;
int capacity = items.Length + 1;
int[] dupArray = new int[capacity];
Array.Copy(items, dupArray,
items.Length);
items = dupArray;
//Adjusting elements to insert element
in its right index
if (index < lastIndex)
{
Array.Copy(items, index, items,
index + 1, lastIndex - index);
}
items[index] = value;
}
}
*Output:*
**
**
What is implementation inheritance
and interface inheritance?
When a class is derived from another
class in such a way that it
will inherit all its members to its corresponding derived class,
then it is implementation inheritance.
* When a class inherits only the signatures of the functions from its
corresponding base class, then it is interface inheritance.
When you are using shadowing if you
want to access the base class
method with derived class objects how can you access it?
method with derived class objects how can you access it?
1st cast the derived class object to
base class type and if you call
method it invokes base class method. Keep in mind it works only when
derived class method is shadowed.
observe the highlighted lines below:
publicclass BaseClass
{
public void Method1()
{
string a = "Base method";
}
}
publicclass DerivedClass : BaseClass
{
public new void Method1()
{
string a = "Derived Method";
}
}
publicclass TestApp
{
public static void main()
{
DerivedClass derivedObj = new DerivedClass();
* **BaseClass**obj2 = (**BaseClass**)derivedObj;
** obj2.Method1(); **// invokes Baseclass method*
}
}
method it invokes base class method. Keep in mind it works only when
derived class method is shadowed.
observe the highlighted lines below:
publicclass BaseClass
{
public void Method1()
{
string a = "Base method";
}
}
publicclass DerivedClass : BaseClass
{
public new void Method1()
{
string a = "Derived Method";
}
}
publicclass TestApp
{
public static void main()
{
DerivedClass derivedObj = new DerivedClass();
* **BaseClass**obj2 = (**BaseClass**)derivedObj;
** obj2.Method1(); **// invokes Baseclass method*
}
}