Basic Programs useful for interviews
class Factorial
{ private static int temp = 1;
public static int CalFactorial(int n)
{ temp = temp * n;
return temp;
}
static void Main(string[] args)
{
Console.WriteLine("Enter A Number:");
int fact = Convert.ToInt32(Console.ReadLine());
int x = 1;
for (int i = 1; i <= fact; i++)
{
x = Factorial.CalFactorial(i);
}
System.Console.WriteLine(x);
Console.ReadLine();
}
}
WPF
What is Hardware acceleration ? what is the use of it?
What is Xaml?
What is Declative programming?
What is Dependency Property?
When should I use WPF instead of Windows Forms in C#?
1) Use it when you require various media type applications(to incorporate a video,documents or 3d content or animated transition between a sequence of images)
2)If you need a skinned user interface.
3)if you need to bind to xml data
4)to dynamically load portions of use interface from a webservice.
5) to create web like navigation style desktop applications.
What are the advantages of WPF over windows applications?
1)Databinding in WPF is superior to what Windows Forms offers.
2)UI and C# business logic can be cleanly separated in WPF .
3)Data/control templates – a much cleaner way than anything Windows Forms can offer.
4)Styles – cool and simple. Its so easy to style all your buttons in an application to have the same look and feel.
5)UI virtualization
6)3D support.
7)I can add a User Experience engineer to my team, and with no C# knowledge he can work magic in Expression Blend and give the front-office trading application a makeover that is guaranteed to win over the business users.
Why WPF superior to Windowsforms ?
1) Declarative Programming .
2)Triggers
3)Consistent styles
4) Data Driven UI
5)Flexible Control Model
6)Adaptible UI layout
7)Rich Application text
8)Drawing object model
9)Advanced graphics
10)lookless controls
Arrays
Arrays in C# work similarly to how arrays work in most other popular languages There are, however, a few differences as listed below1. When declaring an array in C#, the square brackets ([]) must come after the type, not the identifier. Placing the brackets after the identifier is not legal syntax in C#.
int[] IntegerArray; // not int IntegerArray[];2. Another difference is that the size of the array is not part of its type as it is in the C language. This allows you to declare an array and assign any array of int objects to it, regardless of the array's length.int[] IntegerArray; // declare IntegerArray as an int array of any sizeIntegerArray = new int[10]; // IntegerArray is a 10 element arrayIntegerArray = new int[50]; // now IntegerArray is a 50 element arrayWhat are the 3 different types of arrays that we have in C#?1. Single Dimensional Arrays2. Multi Dimensional Arrays also called as rectangular arrays3. Array Of Arrays also called as jagged arraysAre arrays in C# value types or reference types?Reference types.
What is the base class for all arrays in C#?
System.Array
How do you sort an array in C#?
The Sort static method of the Array class can be used to sort array items.
Give an example to print the numbers in the array in descending order?
using System;
namespace ConsoleApplication
{class Program{static void Main()
{int[] Numbers = { 2, 5, 3, 1, 4 };//Print the numbers in the array without sortingConsole.WriteLine("Printing the numbers in the array without sorting");
foreach (int i in Numbers){Console.WriteLine(i);}//Sort and then print the numbers in the arrayConsole.WriteLine("Printing the numbers in the array after sorting");Array.Sort(Numbers);
foreach (int i in Numbers){Console.WriteLine(i);}//Print the numbers in the array in desceding orderConsole.WriteLine("Printing the numbers in the array in desceding order");Array.Reverse(Numbers);
foreach (int i in Numbers){Console.WriteLine(i);}}}}
What property of an array object can be used to get the total number of elements in an array?
Length property of array object gives you the total number of elements in an array. An example is shown below.
using System;
namespace ConsoleApplication
{
class Program
{
static void Main()
{int[] Numbers = { 2, 5, 3, 1, 4 };
Console.WriteLine("Total number of elements = " +Numbers.Length);
}}}
Give an example to show how to copy one array into another array?
We can use CopyTo() method to copy one array into another array. An example is shown below.
using System
;namespace ConsoleApplication
{
class Program
{
static void Main(){int[] Numbers = { 2, 5, 3, 1, 4 };
int[] CopyOfNumbers=new int[5];
Numbers.CopyTo(CopyOfNumbers,0);
foreach (int i in CopyOfNumbers){Console.WriteLine(i);
}
}
}
}
3) Which of these statements correctly declares a two-dimensional array in C#?
int[,] myArray;
int[][] myArray;
int[2] myArray;
System.Array[2] myArray;
Can you store multiple data types in System.Array?No.
What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?
The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element's object, resulting in a different, yet identacle object.
How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.
C# : OOPs
Objects are programming constructs that have data, behavior, and identity. Object data is contained in the fields, properties, and events of the object, and object behaviors are defined by the methods and interfaces of the object.
Objects have identity — two objects with the same set of data are not necessarily the same object.
Objects in C# are defined through classes and structs — these form the single blueprint from which all objects of that type operate.
Objects have the following properties:
· Everything you use in C# is an object, including Windows Forms and controls.
· Objects are instantiated; that is, they are created from templates defined by classes and structs.
· Objects use Properties to obtain and change the information they contain.
· Objects often have methods and events that allow them to perform actions.
· All C# objects inherit from the Object.
What is Class?
Classes are symbolic representations of objects; classes describe the properties, fields, methods, and events that form objects. Classes control user access to object items through encapsulation.
a class defines the data and behavior of the data type. Programmers can then create objects that are instances of this class.
Classes have the following properties:
· a class can inherit implementation from only one base class.
· A class can implement more than one interface.
· Class definitions can be split between different source files.
· Static classes are sealed classes that contain only static methods.
What is the difference between class and object?
Classes describe the structure of objects, while objects are usable instances of classes. Each instance is an exact yet distinct copy of its class. Because an object is an instance of a class, the act of creating an object is called instantiation.
Usually, changing the data in one object does not change the data in any other object.
How will you create objects?
A class defines a type of object, but it is not an object itself. An object is a concrete entity based on a class, and is sometimes referred to as an instance of a class.
Objects can be created by using the new keyword followed by the name of the class that the object will be based on, like this:
Customer object1 = new Customer();
When an instance of a class is created, a reference to the object is passed back to the programmer.
What is encapsulation?
Encapsulation is the ability to contain and control access to a group of associated items. Classes provide one of the most common ways to encapsulate items. Encapsulation allows you to control how the data and procedures are used. You can use access modifiers, such as Private or Protected, to prevent outside procedures from executing class methods or reading and modifying data in properties and fields. You should declare internal details of a class as Private to prevent them from being used outside of your class; this technique is called data hiding.One basic rule of encapsulation is that class data should be modified or retrieved only via Property procedures or methods. Hiding the implementation details of your classes prevents classes from being used in undesired ways, and lets you to later modify such items without risk of compatibility problems.
What is Inheritance?
You can extend the functionality of an existing class by creating a new class that derives from the existing class. The derived class inherits the properties of the base class, and you can add or override methods and properties as required.
In C#, both inheritance and interface implementation are defined by the : operator.
The base class should always be leftmost in the class declaration.
C# does not support multiple inheritance meaning that classes cannot inherit from more than one class. You can,
however, use interfaces for that purpose
e.g:
public class CoOrds
{
Private int x,y;
Public coOrds()
{
x=0;
y =0;
}
Public int X
{
get {return x;}
set { x =value;}
}
Public int Y
{ get { return y;}
Set { y=value ;}
}
}
You derive a new class, called ColorCoOrds, from the CoOrds class, as follows:
ColorCoOrds : CoOrds
{
}
ColorCoOrds then inherits all the fields and methods of the base class, to which you can add new ones to provide extra features in the derived class according to our needs.
The constructor of the derived class implicitly calls the constructor for the base class. In inheritance, all base class constructors are called before the derived class's constructors in the order that the classes appear in the class hierarchy.
How will you typecast to a baseclass?
you cannot use a reference to a base class to access the members and methods of a derived class even if the base class reference may contain a valid reference to an object of the derived type.
You can reference a derived class with a reference to the derived type implicitly:
ColorCoOrds color1 = new ColorCoOrds();
CoOrds coords1 = color1;
In this code, the base class reference, coords1, contains a copy of the color1 reference.
What is the use of base keyword?
You can access base class members in a subclass even when those base members are overridden in the superclass using the base keyword.
For instance, you can create a derived class which contains a method with the same signature as in the base class. If you prefaced that method with the new keyword, you indicate that this is an all-new method belonging to the derived class. You could still provide a method for accessing the original method in the base class with the base keyword.
For instance, say your base CoOrds class had a method called Invert(), which swaps the x and y coordinates. You could provide a substitute for this method in your derived ColorCoOrds class with code like this:
public new void Invert()
{
int temp = X;
X = Y;
Y = temp;
screenColor = System.Drawing.Color.Gray;
}
As you can see, this method swaps x and y, and then sets the coordinates' color to gray. You could provide access to the base implementation for this method by creating another method in ColorCoOrds, such as this one:
public void BaseInvert()
{
base.Invert();
}
You then invoke the base method on a ColorCoOrds object by calling the BaseInvert() method.
ColorCoOrds color1 = new ColorCoOrds();
color1.BaseInvert();
Remember that you would get the same effect if you assigned a reference to the base class to an instance of ColorCoOrds, and then accessed its methods:
CoOrds coords1 = color1;
coords1.Invert();
In case of Base class constructor and derived class constructor which one executed first?
Base class objects are always constructed before any deriving class. Thus the constructor for the base class is executed before the constructor of the derived class. If the base class has more than one constructor, the derived class can decide the constructor to be called.
e.g: public class CoOrds
{
private int x, y;
public CoOrds()
{
x = 0;
y = 0;
}
public CoOrds(int x, int y)
{
this.x = x;
this.y = y;
}
}
You could then change the ColorCoOrds class to use a particular one of the available constructors using the base keyword:
public class ColorCoOrds : CoOrds
{
public System.Drawing.Color color;
public ColorCoOrds() : base ()
{
color = System.Drawing.Color.Red;
}
public ColorCoOrds(int x, int y) : base (x, y)
{
color = System.Drawing.Color.Red;
}
}
What is Method Overriding?
A derived class may override the method of a base class by providing a new implementation for the declared method.
methods must be explicitly marked as virtual using the virtual modifier. Property accessors, as well as methods, can be overridden in much the same way.
What is virtual method?
A method that is to be overridden in a derived class is declared with the virtual modifier. In a derived class, the overridden method is declared using the override modifier.
The override modifier denotes a method or a property of a derived class that replaces one with the same name and signature in the base class. The base method, which is to be overridden, must be declared as virtual, abstract, or override: it is not possible to override a non-virtual or static method in this way. Both the overridden and the overriding method or property must have the same access-level modifiers.
What is polymorphism?
ability of a derived class to redefine, or override, methods that it inherits from a base class.
Explain About Access modifiers?
Access modifiers are keywords used to specify the declared accessibility of a member or a type. This section introduces the four access modifiers:
· public
· protected
· internal
· private
The following five accessibility levels can be specified using the access modifiers:
public: Access is not restricted.
protected: Access is limited to the containing class or types derived from the containing class.
Internal: Access is limited to the current assembly.
protected internal: Access is limited to the current assembly or types derived from the containing class.
private: Access is limited to the containing type.
Difference between const and readonly?
The readonly keyword differs from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, although a const field is a compile-time constant, the readonly field can be used for run-time constants, as in this line: public static readonly uint l1 = (uint)DateTime.Now.Ticks;
What is Property?
Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.
· Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code.
· A get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels. For more information
· The value keyword is used to define the value being assigned by the set indexer.
· Properties that do not implement a set method are read only.
· For simple properties that require no custom accessor code, consider the option of using auto-implemented properties.
What is Indexer?
Indexers are a syntactic convenience that enable you to create a class, struct, or interface that client applications can access just as an array. Indexers are most frequently implemented in types whose primary purpose is to encapsulate an internal collection or array.
What is Abstract class?
The abstract keyword enables you to create classes and class members solely for the purpose of inheritance—to define features of derived, non-abstract classes.
An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
What is sealed class?
A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes are primarily used to prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.
A class member, method, field, property, or event, on a derived class that is overriding a virtual member of the base class can declare that member as sealed.
This negates the virtual aspect of the member for any further derived class. This is accomplished by putting the sealed keyword before the override keyword in the class member declaration. For example:
public class D : C
{
public sealed override void DoWork() { }
}
What is interface?
Interfaces describe a group of related functionalities that can belong to any class or struct. Interfaces can consist of methods, properties, events, indexers, or any combination of those four member types. An interface cannot contain fields. Interfaces members are automatically public.
· A class or struct can inherit more than one interface.
· When a class or struct inherits an interface, it inherits only the method names and signatures, because the interface itself contains no implementations.
· To implement an interface member, the corresponding member on the class must be public, non-static, and have the same name and signature as the interface member. Properties and indexers on a class can define extra accessors for a property or indexer defined on an interface.
· Interfaces and interface members are abstract; interfaces do not provide a default implementation.
Threading
Threads are the basic unit to which an operating system allocates processor time, and more than one thread can be executing code inside that process. Each thread maintains exception handlers, a scheduling priority, and a set of structures the system uses to save the thread context until it is scheduled. The thread context includes all the information the thread needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process.
When to use multiple threads?
To increase responsiveness to the user and decrease the data processing time of your application. If you are doing intensive input/output work, you can also use I/O completion ports to increase your application's responsiveness.
In what kind of scenarios a single application domain could use multiple threads?
Without modification, the same application would dramatically increase user satisfaction when run on a computer with more than one processor. Your single application domain could use multiple threads to accomplish the following tasks:
Communicate over a network, to a Web server, and to a database.
· Perform operations that take a large amount of time.
· Distinguish tasks of varying priority. For example, a high-priority thread manages time-critical tasks, and a low-priority thread performs other tasks.
· Allow the user interface to remain responsive, while allocating time to background tasks.
What is the use of volatile keyword?
The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times.
The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access.
What kind of types of fields volatile support?
The volatile keyword can be applied to fields of these types:
· Reference types.
· Pointer types (in an unsafe context). Note that although the pointer itself can be volatile, the object that it points to cannot. In other words, you cannot declare a "pointer to volatile."
· Integral types such as sbyte, byte, short, ushort, int, uint, char, float, and bool.
· An enum type with an integral base type.
· Generic type parameters known to be reference types.
· IntPtr and UIntPtr.
Can I declare a local variable as volatile?
Local variables cannot be declared volatile.