Whats the difference betweeen Structure, Class and Enumeration
Structures and Enumerations are Value-Types. This means, the data that they contain is stored as a stack on the memory. Classes are Reference-Types, means they are stored as a heap on the memory.
Structures are implicitly derived from a class called System.ValueType. The purpose of System.ValueType is to override the virtual methods defined by System.Object. So when the runtime encounters a type derived from System.ValueType, then stack allocation is achieved. When we allocate a structure type, we may also use the new keyword. We may even make a constructor of a structure, but, remember, A No-argument constructor for a structure is not possible. The structure's constructor should always have a parameter.
So if we define the following structure
struct MyStruct
{
public int y,z;
}
and we create a structure type
MyStruct st = new MyStruct();
In case of a class, no-argument constructors are possible. Class is defined using the class keyword.
A struct cannot have an instance field, whereas a class can.
class A
{
int x = 5; //No error
...
}
struct
{
int x = 5; //Syntax Error
}
A class can inherit from one class (Multiple inheritance not possible). A Structure cannot inherit from a structure.
Enum is the keyword used to define an enumeration. An enumeration is a distinct type consisting of a set of named constants called the enumerator list. Every enumeration has an underlying type. The default type is "int". Note: char cant be the underlying data type for enum. First value in enum has value 0, each consequent item is increased by 1.
enum colors {red, green, blue, yellow};
Here, red is 0, green is 1, blue is 2 and so on.
An explicit casting is required to convert an enum value to its underlying type
int x = (int)colors.yellow;
Explain constructor and destructor with an example using C#.NET.
Explain constructor and destructor with an example using C#.NET.
A constructor is a member function that performs the task of initializing the objects with the default values to be assigned after creation.
A destructor is a function that is run to release the resources held by an object when it is no longer needed by the application.
In C#.NET we can create constructor and destructor in the following manner:
-----------------CONSTRUCTOR---------
class C
{
private int x;
private int y;
public C (int i, int j)
{
x = i;
y = j;
}
public void display ()
{
Console.WriteLine(x + "i+" + y);
}
}
-----------------DESTRUCTOR---------
class D
{
public D()
{
// constructor
}
~D()
{
// Destructor
}
}
C# Interview Questions and Answers
1) The C# keyword ?int? maps to which .NET type?
-
System.Int16
-
System.Int32
-
System.Int64
-
System.Int128
2) Which of these string definitions will prevent escaping on backslashes in C#?
-
string s = #?n Test string?;
-
string s = ??n Test string?;
-
string s = @?n Test string?;
-
string s = ?n Test string?;
3) Which of these statements correctly declares a two-dimensional array in C#?
-
int[,] myArray;
-
int[][] myArray;
-
int[2] myArray;
-
System.Array[2] myArray;
4) If a method is marked as protected internal who can access it?
-
Classes that are both in the same assembly and derived from the declaring class.
-
Only methods that are in the same class as the method in question.
-
Internal methods can be only be called using reflection.
-
Classes within the same assembly, and classes derived from the declaring class.
5) What is boxing?
a) Encapsulating an object in a value type.
b) Encapsulating a copy of an object in a value type.
c) Encapsulating a value type in an object.
d) Encapsulating a copy of a value type in an object.
6) What compiler switch creates an xml file from the xml comments in the files in an assembly?
-
/text
-
/doc
-
/xml
-
/help
7) What is a satellite Assembly?
-
A peripheral assembly designed to monitor permissions requests from an application.
-
Any DLL file used by an EXE file.
-
An assembly containing localized resources for another assembly.
-
An assembly designed to alter the appearance or ?skin? of an application.
What is a delegate?
-
A strongly typed function pointer.
-
A light weight thread or process that can call a single method.
-
A reference to an object in a different process.
-
An inter-process message channel.
9) How does assembly versioning in .NET prevent DLL Hell?
-
The runtime checks to see that only one version of an assembly is on the machine at any one time.
-
.NET allows assemblies to specify the name AND the version of any assemblies they need to run.
-
The compiler offers compile time checking for backward compatibility.
-
It doesn?t.
10) Which ?Gang of Four? design pattern is shown below?
public class A {
private A instance;
private A() {
}
public
static A Instance {get
{
if ( A == null )
A = new A();
return instance;
}
}
}
-
Factory
-
Abstract Factory
-
Singleton
-
Builder