Tolal:418 Click:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
C# Interview Questions And Answers
Page 1
Ques: 1 what is partial class in .net 2.0
Ans:
Single Class can be separated into multiple physical files with same logical name.
Ans:
Partial Class:-Using the partial keyword indicates that other parts of the class, struct, or interface can be defined within the namespace
Below is the example of a partial class.
Listing 1: Entire class definition in one file (file1.cs)
public class Node
{
public bool Delete()
{
}
public bool Create()
{
}
}
Listing 2: Class split across multiple files
(file1.cs)
public partial class Node
{
public bool Delete()
{
}
}
(file2.cs)
public partial class Node
{
public bool Create()
{
}
}
Ques: 2 I want to delete an dll with a newer version, but nothing it throws an UnauthorizedAccessException!
please help!!!
Assembly a = Assembly.LoadFile(source);
Assembly b = Assembly.LoadFile(des
Ans:
a
Ques: 3
Ans:
Hi there, i have been programming for a few weeks now, i am still learning new things as i go on.
i would like to understand a few more things about c#
if anyone can answer my questions i would be more then appreciated.
- What is the rule i must consider before placing a semi-colon?
- What is the best data type to use to hold a value of 1200.0? and is there any other data types that could be used and why should i choose the other.
- What is the defence between = and ==? And do i need to use both?
- What is the defrence between a while and a do-while loop?
- What is a class? And why is it so important to be used?
Ques: 4 What is server code and client code in C#?
Ans:
Code which execute on client are called client side code Otherwise Server side code.
Ans:
Server code is run on server side and client code is run client side.
Ans:
Server side code will execute at server end all the business logic will execute at server end where as client side code will execute at client side at browser end.
Ques: 5 Why we using in header file using system and more header file is in C# what is difference between and all header file ?
Ques: 6 Give Real life examples of polymorphisms, encapsulation and inheritance?
Ans:
real lif example of inheritance is parent child relationship
Ques: 7 What is partial class?what is its advantage.
Ans:
Partial Class:
when a class need to be implemented at multiple locations with same class name then those classes need to be declared as "partial".
Example:
public main()
{
class Test
{
-------
-------
}
class Test //error
{
-------
-------
}
}
public main()
{
partial class Test
{
-------
-------
}
partial class Test //correct
{
-------
-------
}
}
Ques: 8 What is the name of c#.net compiler?
Ans:
csc
Ans:
.cs Comliler
Ans:
Name of c# .net compiler is CSC.
CSC stands foe C SHARP COMPILER.
Ans:
Just In time (JIT) Compiler....!!
Ans:
JIT Just In Time Compiler act as interface between IDE to OS after Common run time compilation is done
Ques: 9 What is an object?
Ans:
Object:-Object is the basic run time entity.
The object type is based on System.Object in the .NET Framework. You can assign values of any type to variables of type object.
All data types, predefined and user-defined, inherit from the System.Object class. The object data type is the type to and from which objects are boxed.
Exapmle:-
class a
{}
public static void Main()
{
a ob= new a();// create object ob of class a;
}
Ques: 10 What is a class?
Ans:
Class:-
Classes are declared using the keyword class. The declaration takes the form:
[attributes] [modifiers] class identifier [:base-list] { class-body }[;]
where:
attributes (Optional)
:-Additional declarative information.
modifiers (Optional):-
The allowed modifiers are new, abstract, sealed, and the four access modifiers.
identifier:-
The class name.
base-list (Optional)
:-A list that contains the one base class and any implemented interfaces, all separated by commas.
class-body
:-Declarations of the class members.
Example:-
Public class a
{
// some members and parameter
}
Ques: 11 what abstraction,encapsulation,inheritance? Define with example.
Ans:
Abstraction:-Abstraction is the process of hiding the details and exposing only the essential features of a particular concept or object.
Abstraction is another good feature of OOPS. Abstraction means to show only the necessary details to the client of the object
Example:-A simple example is using a base class "Animal", with a virtual function "Walk". In the case two-legged versus four-legged animals, both of them walk, but the actual mechanics are different. The "Walk" method abstracts the actual mechanics behind the walking that each "Animal" does.
2nd example:-A class called Animal.
It has properties like ears,colour, eyes but they are not defined.
It has methods like Running(), Eating(), etc. but the method does not have any body, just the definition.
Encapsulation:-Encapsulation is the term given to the process of hiding all the details of an object that do not contribute to its essential characteristics.its wrapping of data and members in a single unit.Encapsulation is a process of hiding all the internal details of an object from the outside world .
1:- Encapsulation is the ability to hide its data and methods from outside the world and only expose data and methods that are required .
2.Encapsulation gives us maintainability, flexibility and extensibility to our code.
3.Encapsulation provides a way to protect data from accidental corruption .
4.Encapsulation gives you the ability to validate the values before the object user change or obtain the value .
5.Encapsulation allows us to create a "black box" and protects an objects internal state from corruption by its clients.
6.Encapsulation is the technique or process of making the fields in a class private and providing access to the fields using public methods.
Example:-cars and owners...
all the functions of cars are encapsulated with the owners..
No one else can access it...
Inheritance:-In object-oriented programming (OOP), Inheritance is a way to compartmentalize and reuse code by creating collections of attributes and behaviors called objects which can be based on previously created objects. In classical inheritance where objects are defined by classes.
classes can inherit other classes. The new classes, known as Sub-classes (or derived classes), inherit attributes and behavior of the pre-existing classes, which are referred to as Super-classes.
the process of deriving one class from parent class called .
Example:-INHERITANCE MEANS A CHILD CLASS USE THE ALL THE ELEMENTS IN THE PARENTS CLASS.
DAD
|
CHILED
Seccond Example:-
Kingfisher jet
^
|
Airplane
^
|
Flying Things
Ques: 12 What is mean "Death of Diamod"?
Ans:
Death Diamond:-In object-oriented programming languages with multiple inheritance and knowledge organization, the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C?
For example a class Button may inherit from both classes Rectangle (for appearance) and Clickable (for functionality/input handling), and classes Rectangle and Clickable both inherit from the Object class. Now if the equals method is called for a Button object and there is no such method in the Button class but there is an overridden equals method in both Rectangle and Clickable, which method should be eventually called? ihis is called "Death of Diamond".
Ex:
Class a
{
}
class b: a
{}
class c: a
{}
class d:b,c
{}
It is called the "diamond problem" because of the example of the class inheritance in this situation. In this example, class A is at the top, both B and C separately inherit to A, and D inherits both......
Ques: 13 what is the main difference between delegate and an event in c#?
Ans:
Delegate:-A delegate is basically a reference to a method. A delegate can be passed like any other variable. This allows the method to be called anonymously, without calling the method directly.
delegate declaration defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static or an instance method. Delegates are roughly similar to function pointers in C++; however, delegates are type-safe and secure.
The delegate declaration takes the form:
[attributes] [modifiers] delegate result-type identifier ([formal-parameters])
Event:-An event in one program can be made available to other programs that target the .NET runtime.
An event is a member that enables an object or class to provide notifications. Clients can attach executable code for events by supplying event handlers. Events are declared using event-declarations:
event-declaration:
event-field-declaration
event-property-declaration
event-modifier:
new
public
protected
internal
private
static
Example:-
public delegate void EventHandler(object sender, Event e);
public class Button: Control
{
public event EventHandler Click;
protected void OnClick(Event e) {
if (Click != null) Click(this, e);
}
public void Reset() {
Click = null;
}
}
Ques: 14 How to use Hash Table,ArrayList in c# ?Explain with example.
Ans:
ArrayList:-Arraylist is a collection of objects(may be of different types).
Arraylist is very much similar to array but it can take values of different datatypes.
If you want to find something in a arraylist you have to go through each value in arraylist, theres no faster way out.ArrayList's size can be changed dynamically.
Items are added to the ArrayList with the Add() method.
example:-HOW TO ADD DATA IN ARRAYLIST:
using System.Collections;
class Program
{
static void Main()
{
//
// Create an ArrayList and add three elements.
//
ArrayList list = new ArrayList();
list.Add("One");
list.Add("Two");
list.Add("Three");
}
}
EXAMPLE-Adding one ArrayList to second one
There are different ways to add one ArrayList to another, but the best way is using AddRange. Internally, AddRange uses the Array.Copy or CopyTo methods, which have better performance than some loops.
=== Program that uses Add and AddRange ===
using System;
using System.Collections;
class Program
{
static void Main()
{
//
// Create an ArrayList with two values.
//
ArrayList list = new ArrayList();
list.Add(15);
list.Add(17);
//
// Second ArrayList.
//
ArrayList list2 = new ArrayList();
list2.Add(110);
list2.Add(113);
//
// Add second ArrayList to first.
//
list.AddRange(list2);
//
// Display the values.
//
foreach (int i in list)
{
Console.WriteLine(i);
}
}
}
=== Output of the program ===
15
17
110
113
The ArrayList class provides the Count property, which is a virtual property. When you use Count, no counting is actually done; instead, a cached field value is returned
HASH TABLE:-Hashtable is also collection which takes a key corresponding to each values.
If you want to find something in a hashtable you dont have to go through each value in hashtable, instead search for key values and is faster.The Hashtable lets you quickly get an object out of the collection by using it's key.
The Hashtable object contains items in key/value pairs. The keys are used as indexes. We can search value by using their corresponding key.
The data type of Hashtable is object and the default size of a Hashtable is 16.
Items are added to the Hashtable with the ADD()method.
Example-How to add data in hash table:--
Add method of Hashtable is used to add items to the hashtable. The method has index and value parameters. The following code adds three items to hashtable.
hshTable .Add("A1", "Kamal");
hshTable .Add("A2", "Aditya");
hshTable .Add("A3", "Ashish");
Retrieving an Item Value from Hashtable
The following code returns the value of "Author1" key:
string name = hshTable["A1"].ToString();
Removing Items from a Hashtable
The Remove method removes an item from a Hashtable. The following code removes item with index "A1" from the hashtable:
hshTable.Remove("A1");
Looking through all Items of a Hashtable
The following code loops through all items of a hashtable and reads the values.
// Loop through all items of a Hashtable
IDictionaryEnumerator en = hshTable.GetEnumerator();
while (en.MoveNext())
{
string str = en.Value.ToString();
}
Ques: 15 What is the difference between shadow and override ?
Ans:
Difference between shadow and override:-
Shadowing hide the inherrited method using new keyword and CLR choose the target mthod between the parent and child to call using the object's runtime type.Overriding hide the inherrited method using override keyword and the parent should be virtual. overrding always choose the object's compile-time type.
Differences:-
1-PorPose:-
SHADOW-Protecting against a subsequent base class modification introducing a member you have already defined in your derived class.
OVERRIDE-Achieving polymorphism by defining a different implementation of a procedure or property with the same calling sequence
2-Redefined element:-
SHADOW:-Any declared element type.
OVERRIDE:-Only a procedure (Function or Sub) or property
3-Accessibility:-
SHADOW:-Any accessibility
OVERRIDE:-Cannot expand the accessibility of overridden element (for example cannot override Protected with Public)
4-Readability and writability
SHADOW:-Any combination
OVERRIDE:-Cannot change readability or writability of overridden property
5-Keyword usage
SHADOW:-Shadows recommended in derived class; Shadows assumed if neither Shadows nor Overrides specified.
OVERRIDE:-Overridable required in base class; Overrides required in derived class
Ques: 16 What is the top .NET class that everything is derived from?
Ans:
System.Object
Ques: 17 When we can inherit a protected class-level variable?
Ans:
Classes in the same namespace.
Ques: 18 Does C# support multiple inheritance?
Ans:
No directly,but we can use interface for multiple inheritence
Ans:
No directly,but we can use interface for multiple inheritence
Ques: 19 How do you inherit from a class in C#?
Ans:
Place a colon and then the name of the base class.
exmple:-
class a
{}
class b: a //inherit class a
{}
Ques: 20 What is an abstract class?
Ans:
Abstract class:-Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes.
Abstract classes have the following features:
1-An abstract class cannot be instantiated.
2-An abstract class may contain abstract methods and accessors.
3-It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited.
4-A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
Abstract class have complete and non complete member and methods.
Example:- abstract class a
{
int a;
public abstract void print();
}
Goto Page:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
C# Objective
C# Objective Questions And Answers
C# Interview Questions And Answers
C# Interview Questions And Answers
R4R,C# Objective, C# Subjective, C# Interview Questions And Answers,C#,C# Interview,C# Questions ,C# Answers