What
is C#?
C# is a simple, modern, object-oriented, and type-safe programming
language used with XML-based Web services on the .NET platform and designed for
improving productivity in the development of Web applications.
Which is compiled by .Net framework for generating intermediate language
(IL).
C# combines the high productivity of Rapid Application Development (RAD)
languages.
List
some of the advantages of C#?
1. Easy to learn
2. Object
oriented
3. Component oriented
4. Part of .NET framework
What
are IDE’s provided by Microsoft for C# development?
Below are the IDE’s used for C# development –
· Visual Studio Express (VSE)
· Visual Studio (VS)
· Visual Web Developer
Explain
the features of C#?
1. Constructors and Destructors
2. Properties
3. Passing Parameters
4. Arrays
5. Main
6. XML Documentation and
7. Indexers
What
are the namespaces used in C#.NET?
Using System;
Using System.Collections.Generic;
Using System.Windows.Forms;
What
are the characteristics of C#?
There are several characteristics of C# are:
Simple, Type safe, Flexible, Object oriented, Compatible, Consistent,
Interoperable, Modern
What
are the types of errors?
1. Syntax error
2. Logic error
3. Runtime error
Explain
the types of comments in C#?
· Single Line Comment Eg : //
· Multiline Comments Eg: /* */
· XML Comments Eg : ///
List
out some of the exceptions in C#?
· NullReferenceException
· ArgumentNullException
· DivideByZeroException
· IndexOutOfRangeException
· InvalidOperationException
· StackOverflowException etc.
Which
are the loop types available in C#?
Below are the loop
types in C# -
For
While
Do-While
What
are the different types of statements supported in C#?
C# supports several different kinds of statements are
Block statements,
Declaration
statements,
Expression
statements,
Selection
statements,
Iteration
statements,
Jump statements,
Try catch statements,
Checked and
unchecked,
Lock statement.
What
is boxing and Un-boxing?
Whenever a value type member is assigned to the reference type member
it’s called as boxing.
Whenever a reference type member is assigned to the value type member
it’s called as Un-boxing.
class Test
{
static void Main()
{
int i = 1;
object o = i; // boxing
int j = (int) o; // unboxing
}
}
Explain “static” keyword in C#?
“Static” keyword
can be used for declaring a static member. If the class is made static then all
the members of the classes are also made static. If the variable is made static
then it will have a single instance and the value change is updated in this
instance.
What
is an Interface?
Interface is a contract that defines the signature of the functionality.
It looks like a class but has no implementation. It has only empty definition
of methods, functions, events and indexer.
Define
Constructors?
A constructor is a member function with the same name as its class. The
constructor is invoked whenever an object of its associated class is created.
It is called constructor because it constructs the values of data members of
the class.
· Constructor is
used to initialize an object (instance) of a class.
· Constructor is a
like a method without any return type.
· Constructor has
same name as class name.
· Constructor
follows the access scope (Can be private, protected, public, Internal and
external).
· Constructor can
be overloaded.
· Constructors
generally following types:
· Default
Constructor
· Parameterized
constructor
· Private
Constructor
· Static
Constructor
· Copy Constructor
Define
destructors?
A destructor is called for a class object when that object passes out of
scope or is explicitly deleted. A destructor as the name implies is used to
destroy the objects that have been created by a constructors. Like a
constructor, the destructor is a member function whose name is the same as the
class name but is preceded by a tilde.
What
are methods?
A method is a member that implements a computation or action that can be
performed by an object or class. Static methods are accessed through the class.
Instance methods are accessed through instances of the class.
What
are fields?
A field is a variable that is associated with a class or with an
instance of a class.
What
are events?
An event is a member that enables a class or object to provide
notifications. An event is declared like a field except that the declaration
includes an event keyword, and the type must be a delegate type.
What
is the use of using statement in C#?
The using statement is used to obtain a resource, execute a statement,
and then dispose of that resource.
Why
to use “using” in C#?
“Using” statement
calls – “dispose” method internally, whenever any exception occurred in any
method call and in “Using” statement objects are read only and cannot be
re-assignable or modifiable.
Can “this” be used
within a static method?
No ‘This’ cannot be used in a static method. As only static
variables/methods can be used in a static method.
What
is the difference between “constant” and “read only” variables in C#?
“Const” keyword is
used for making an entity constant. We cannot modify the value later in the
code. Value assigning is mandatory to constant variables.
Eg: const string_
name=”Test”;
“Read only”
variable value can be changed during runtime and value to read only variables
can be assigned in the constructor or at the time of declaration.
(or)
Constants: The value can’t be changed.
Read-only: The value will be initialized only once from the constructor
of the class.
Static: Value can be initialized once.
What is the difference
between public, static Void, Main and String[] args?
Public: The keyword public is an access modifier that tells the C#
compiler that the Main method is accessible by anyone.
Static: Declares that the Main method is a global one and can be called
without creating an instance of the class.
(Or)
Classes/methods/variables are accessible throughout the application
without creating instance.
Void: The keyword void is a type modifier that states
that the Main method does not return any value.
Main: is the method name. It is the entry point for any C# program. Whenever we run the C# program, Main () method is invoked first before any other method. It represents startup of the program.
String [] args: is used for command line arguments in C#. While running the C# program, we can pass values. These values are known as arguments which we can use in the program.
Explain Generics in C#?
Generics are C# is used to make the code reusable and which intern
decreases the code redundancy and increases the performance and type safety.
Namespace – “System.Collections.Generic”.
(OR)
Generics help to separate logic and data type to increase re-usability.
In other words you can create a class whose data type can be defined on
run-time.
What
is a Managed and Un-managed code?
Managed Code: - Code that executes
under CLR execution environment is called as Managed code.
Unmanaged Code: - Code that get executes outside
CLR boundary.
Is
C# code is unmanaged or managed code?
C# code is managed code because the compiler – CLR will compile the code
to Intermediate Language.
What
is Stack and Heap?
Stack memory stores data types like int, double, Boolean, etc.,
Heap stores data types like string and objects.
Ex: - int i=1; // This is stored in stack.
int y=i; // This is stored in
stack.
object o=null; // This is stored in
heap.
What
is Serialization and De-serialization?
The process of converting an object into a stream of bytes is called
Serialization.
De-serialization is the reverse process of creating an object from a
stream of bytes.
Serialization / De-serialization is mostly used to transport objects.
What
are the modifiers in C#?
Abstract, Sealed, Virtual, Const, Event, Extern, Override, Readonly,
Static, New.
What
are the types of access modifiers in C#?
Access modifiers in C# are -- public, protect, private, internal,
protect internal, private protected.
1. Public – All members have access in all
classes and projects.
2. Private - It can be accessed by any
code within the containing class only.
3. Protected - When attribute and methods are defined as
protected, All members in current class and in derived classes can access the
variables. It can be accessed by any method in the inherited classes and any
method within the same class. The protected access modifier cannot be applied
to classes and interfaces. Methods and fields in a interface
can't be declared protected.
4. Internal – Only members
in current project have access to the elements.
5. Protected Internal – All members in
current project and all members in derived class can access the variables.
What
is the value type and reference type and differences?
(1) Value types are stored on the stack and when a value of a variable
is assigned to another variable.
Examples: bool, byte, chat, decimal, double, enum, float, int, long,
sbyte, short, strut, uint, ulong, ushort.
(2) Reference types are stored on the heap, and when an assignment
between two reference variables occurs.
Examples: class, delegate, interface, object, string.
What
is sealed Class in C#?
Sealed class is used to prevent the class from being inherited from
other classes.
What
is the difference between “Out” and “Ref” parameters in C#?
“Out” parameter can be passed to a method and it need not be initialized
where as “ref” parameter has to be initialized before it is used.
Ref Parameter: -
If you want to pass a variable as ref parameter you need to initialize
it before you pass it as ref parameter to method. Ref keyword will pass
parameter as a reference this means when the value of parameter is changed in
called method it get reflected in calling method also.
Out Parameter: -
If you want to pass a variable as out parameter you don’t need to
initialize it before you pass it as out parameter to method. Out keyword also
will pass parameter as a reference but here out parameter must be initialized
in called method before it return value to calling method.
What
is the basic concept of object-oriented programming?
It is necessary to understand some of the concepts used extensively in
object-oriented programming.
· Objects
· Classes
· Data abstraction and encapsulation
· Inheritance
· Polymorphism
· Dynamic Binding
· Message passing.
What is object?
An object is an instance of a class. An object is created by using operator new. A class that creates an object in memory will contain the information about the values and behaviours (or methods) of that specific object.
What is the difference between Object and Instance?
An instance of a user-defined type is called an object. We can instantiate many objects from one class. An object is an instance of a class.
What
is Class?
A class is a collection of method and variables.
A class defines certain properties, fields, events, method etc.
A class enables you to create your own custom types by grouping together
variables of other types, methods and events.
A class can be defined by using the class keyword.
What
is a Namespace?
A namespace is an abstract container or environment created to hold a
logical grouping of unique identifiers (i.e names).
Namespaces allow to group entities like classes, objects and
functions under a name.
What are Abstraction, Encapsulation, Inheritance and Polymorphism?
Abstraction:
Abstraction is a process of hiding the unwanted data and giving only
relevant data.
(or) Abstraction is a process of hiding the implementation details and
displaying the essential features.
Abstraction solves the problem in the design level.
Encapsulation:
Encapsulation
is a process of binding the data members and member functions into a single
unit.
Encapsulation solves the problem in the implementation level.
Inheritance:
Inheritance
is a process of deriving / creating a new class from already an existing class.
Main
purpose of inheritance is code Re-usability and providing additional
functionality / enhancement.
Polymorphism:
It is a property of object to act differently under different conditions.
Types of Polymorphism: -
1) Static Polymorphism / Compile time
Polymorphism / Early Binding.
It is implemented by using method overloading. In compile time itself we
come to know if there are mismatches.
2) Dynamic Polymorphism / Run time Polymorphism / Late
Binding.
It is implemented by using override and virtual keyword.
What
is Abstraction and Encapsulation?
Abstraction is design process while Encapsulation happens during phase
which is achieved by using access modifiers.
Abstraction is done in design phase while Encapsulation is implemented
in execution phase using access modifiers.
What
is abstract class?
Abstract class is a half-defined parent class. The full implementation
of abstract class is defined by the child classes.
To define an abstract class we need to use the abstract keyword.
What
is the use of abstract keyword?
The modifier abstract is a keyword used with a class, to indicate that
this class cannot itself have direct instances or objects, and it
is intended to be only a 'base' class to other classes.
Define
polymorphism?
Polymorphism means one name, multiple forms. It allows us to have more
than one function with the same name in a program. It allows us to have
overloading of operators so that an operation can exhibit different behaviors
in different instances.
What
is the difference between compile time polymorphism and run time polymorphism?
Compile time Polymorphism: - Compile time Polymorphism
also known as method overloading.
Method overloading means having two or more methods with the same name
but with different signatures.
Run time Polymorphism: - Run time Polymorphism also
known as method overriding.
Method overriding means having two or more methods with the same name,
same signature but with different implementation.
(OR)
we have a parent class with virtual functions which are overridden in
the child classes.
What
is the difference between method overriding and method overloading?
Method overloading is creating a method with the same name
within the same class having different input signatures.
Method overloading is the example of Compile time polymorphism which
is done at compile time.
Method overloading can be achieved by using following things:
· By changing the number of parameters used.
· By changing the order of parameters.
· By using different data types for the parameters.
Different types of overloading in C# are
· Constructor overloading
· Function overloading
· Operator overloading
What
is Constructor Overloading in C# .net?
In Constructor overloading, n number of constructors can be created for
the same class. But the signatures of each constructor should vary. For example
public class Employee
{
public Employee()
{ }
public Employee(String Name)
{ }
}
What
is Function Overloading in C# .net?
In Function overloading, n number of functions can be created for the
same class. But the signatures of each function should vary. For example
public class Employee
{
public void Employee()
{ }
public void Employee(String Name)
{ }
}
What
is Operator Overloading in C# .net?
We had seen function overloading in the previous example. For operator
Overloading, we will have a look at the example given below. We had defined a
class rectangle with two operator overloading methods.
class Rectangle
{
private int Height;
private int Width;
public Rectangle(int w,int h)
{
Width=w;
Height=h;
}
public static bool operator >(Rectangle a,Rectangle b)
{
return a.Height > b.Height ;
}
public static bool operator <(Rectangle a,Rectangle b)
{
return a.Height < b.Height ;
}
}
Example: -
public class Methodoveloading
{
public int add(int a, int b) //two int type Parameters method
{
return a + b;
}
public int add(int a, int b,int c) //three int type Parameters with same method same as above
{
return a + b+c;
}
public float add(float a, float b,float c,float d) //four float type Parameters with same method same as above two method
{
return a + b+c+d;
}
}
Method overriding: we change the method definition in the
derived class that changes the method behavior. (OR) we have a parent class
with virtual functions which are overridden in the child classes.
Method overriding is the example of run time polymorphism, how it is the
part of run time polymorphism i will explain in detail.
Some Key Points of Method overriding
Method overriding is only possible in derived class not within the same
class where the method is declared.
Only those methods are overrides in the derived class which is declared
in the base class with the help of virtual keyword or abstract keyword.
Example: -
public class Account
{
public virtual int balance()
{
return 10;
}
}
public class Amount:Account
{
public override int balance()
{
return 500;
}
}
Output of the
above Program is
10 and 500
What
are the different categories of inheritance?
Inheritance in Object Oriented Programming is of four types:
1 Single inheritance: Contains one base class and one derived class.
2 Hierarchical inheritance: Contains one base class and multiple derived
classes of the same base class.
3 Multilevel inheritance: Contains a class derived from a derived
class.
4 Multiple inheritances: Contains several base classes and a
derived class.
Does
C# support multiple inheritance?
No, it’s impossible which accepts multi-level inheritance.
Can
you inherit multiple interfaces?
Yes. Multiple interfaces may be inherited in C#.
What
is difference between Class and Interface?
Class: is logical representation of object. It is collection of data and
related sub procedures with definition.
Interface: is also a class contains methods which are not having any
definition. Class does not support multiple inheritances. But interface can
support.
What
is difference between Abstract class and Interface?
Abstract Class |
Interface |
Abstract classes are used when we want to share common functionality
in parent-child relationship. |
Interfaces are used to define contract, enforce standardization,
decoupling and dynamic polymorphism. |
Some methods in abstract classes can have implementation. |
All methods, function, properties in interfaces have to empty
compulsorily. |
We can declare variables. |
We cannot declare variables. |
Abstract classes are inherited. |
Interfaces are implemented. |
An abstract class can have fields. |
Interfaces cannot have fields. |
Abstract class members can have access modifiers. |
Interface members cannot have access modifiers. |
What is the difference
between DLL and EXE files?
DLL (Dynamic Link Library) |
EXE |
It Doesn’t contain Main method. |
It will contain Main method. |
Cannot run individually |
Can run individually. |
Used as a supportive file for other applications. |
Never used as a supportive file |
OS doesn’t create separate process for any DLL rather DLL
will run in the same process created for an Exe. |
OS creates a separate process for each Exe it’s executes. |
A Program/application without Main method creates a DLL after
compilation. |
A Program/application with Main method creates the after compilition |
What
is Value Type and Reference Type?
Value Type |
Reference Type |
The data type which can store the data directly into their memory
locations are known as Value type. |
The data type which can store the data directly into their memory
locations rather refer to other memory location where data is stored are
known as reference type. |
Memory is allocated at Compiler time. |
Memory is allocated at Run time. |
Memory allocation is made with in the stack i.e in continuous memory
location. |
Memory allocation is made with in the heap i.e in random memory
location. |
CLR doesn’t provide automatic memory management. |
CLR provide automatic memory management. |
Occupies less memory. |
Occupies more memory. |
If data is not initialized, then stores the default value in to the
variable. Ex:- int a; a=10 |
If data is not initialized, then stores the default value in to the
reference. Ex:- String.s=”Satya” |
What
is the difference between Class and Structure?
Class |
Structure |
Classes are reference types. |
Structures are value types. |
Classes use in heap. |
Structures use in stack. |
Class members can be declared as protected. |
Structures members cannot be declared as protected. |
Classes require Constructors and destructors. |
Structures do not require Constructors and destructors. |
Objects created from classes are terminated using Garbage Collector
(GC). |
Structures are not destroyed using Garbage Collector (GC). |
Classes support Inheritance. |
Structures do not support Inheritance |
What
is the difference between String and String Builder?
String |
String Builder |
It’s an immutable (Immutable means when you will
create a string type object, then you cannot modify it in memory. If you will
modify it, then it will create a new instance of the object in memory.) |
It’s mutable (It means you can modify the String
Builder object without creating new instances in memory.) |
Performance wise string is slow because every
time it will create new instance |
Performance wise string builder is high because
it will use same instance of object to perform any action |
In string we don’t have append keyword |
In String Builder, we can use append keyword |
String belongs to System namespace |
String
builder belongs to System.Text namespace. |
What is an Array and
Array List?
The following table
lists the difference between Array and Array List in C#.
Array |
Array List |
Array is strongly typed. This means that an array can store only
specific type of items\elements. |
Array List can store any type of items\elements. |
Array stores fixed number of elements. Size of an Array must be
specified at the time of initialization. |
Array List grows automatically, and you don't need to specify size. |
No need to cast elements of an array while retrieving because it is
strongly type and stores specific type of items only. |
Items of Array List need to be cast to appropriate data type while
retrieving. |
Use static helper class Array to perform different tasks on the array. |
Array List itself includes various utility methods for various tasks. |
What is an abstract base class?
An abstract class is a class that is designed to be specifically used as
a base class. An abstract class contains at least one pure virtual function.
Why
is the virtual keyword used in code?
The Virtual keyword is used in code to define methods and the properties
that can be overridden in derived classes.
What
are the Local and Global resources?
Local Resources |
Global Resources |
Page-Specific |
Can be read from any page within the website. |
Can be used only in the specific page. |
Define resource file once and share it across the website |
.resx file |
.resx file |
Same convention to name the file as per the language and culture. |
Same convention to name the file as per the language and culture. |
Stored in the App_LocalResources folder |
Stored in the App_GlobalResources folder. |
App_LocalResources folder can be present either in root folder or in
subfolders |
App_GlobalResources folder must be present in the root of the
application. |
Can be used implicitly or explicitly in the markup |
Must be used explicitly |
Does not generate any object. |
Generates an object for the Global Resource file to be used in the
code to access the resource values. |
What are
Console.ReadLine, Console.Read and Console.ReadKey?
1. Console.ReadLine(): A static method which accepts
the String and return the string as well.
ReadLine (returns a string): reads only
single line from the standard input stream.
Example: - It can be used to ask the user enter their name or age.
2. Console.Read(): A static method which accepts the String but
returns an Integer.
Read (returns an int): reads only
one single character from the standard input stream. Similar to ReadKey except
that it returns an integer.
3. Console.ReadKey(): A static method which accepts
the Character and return ASCII value of that character.
ReadKey (returns a character): reads only
one single character from the standard input stream.
What
are Write and WriteLine?
The Write () method outputs one or more values to the screen
without a new line character. TheWriteLine() always appends a
new line character to the end of the string. This means any
subsequent output will start on a new line.
Explain
Static Members in C#?
If an attribute's value had to be same across all the instances of the
same class, the static keyword is used.
To access a private or public attribute or method in a class, at first
an object of the class should be created. Then by using the object instance of
that class, attributes or methods can be accessed. To access a static variable,
we don't want to create an instance of the class containing the static
variable. We can directly refer that static variable as shown below.
double var = Employee.MinSalary ;
Define
Property in C#?
Properties are a type of class member that are exposed to the outside
world as a pair of Methods. For example, for the static field Minsalary, we
will create a property as shown below.
private double
minimumSalary;
public static
double MinSalary
{
get
{
return
minimumSalary;
}
set
{
minimumSalary = value;
}
}
So when we execute
the following lines code
double minSal =
Employee.MinSalary;
get Method will get
triggered and value in minimumSalary field will be returned. When we execute,
Employee. MinSalary
= 3000;
set Method will get
triggered and value will be stored in minimumSalary field.
What
is Data Encapsulation?
Data Encapsulation is defined as the process of hiding the important
fields from the end user. In the above example, we had used getters and setters
to set value for MinSalary. The idea behind this is that, private field
“minimumSalary” is an important part of our classes. So if we give a third
party code to have complete control over the field without any validation, it
can adversely affect the functionality. This is in line with the OOPS Concept
that an external user should know about an object does. How it does it, should
be decided by the program. So if a user set a negative value for
MinSalary, we can put a validation in the set method to avoid negative values
as shown below
set
{
if(value > 0)
{
minSalary = value;
}
}
Explain
Inheritance in C#?
In object-oriented programming (OOP), inheritance is a way to reuse code
of existing objects. In inheritance, there will be two classes - base class and
derived classes. A class can inherit attributes and methods from existing class
called base class or parent class. The class which inherits from a base class
is called derived classes or child class. For more clarity on this topic, let
us have a look at 2 classes shown below. Here Class Car is Base Class and Class
Ford is derived class.
class Car
{
public Car()
{
Console.WriteLine("Base Class Car");
}
public void DriveType()
{
Console.WriteLine("Right Hand Drive");
}
}
class Ford : Car
{
public Ford()
{
Console.WriteLine("Derived Class Ford");
}
public void Price()
{
Console.WriteLine("Ford Price : 100K $");
}
}
When we execute following lines of code,
Ford CarFord = new Ford();
CarFord.DriveType();
CarFord.Price();
Output Generated is as given below.
Base Class Car
Derived Class Ford
Right Hand Drive
Ford Price : 100K $
What this means is that, all the methods and attributes of Base Class
car are available in Derived Class Ford. When an object of class Ford is created,
constructors of the Base and Derived class get invoked. Even though there is no
method called DriveType() in Class Ford, we are able to invoke the method
because of inheriting Base Class methods to derived class.
Can
Multiple Inheritances implemented in C#?
In C#, derived classes can inherit from one base class only. If you want
to inherit from multiple base classes, use interface.
Explain
the use of Virtual Keyword in C#?
When we want to give permission to a derived class to override a method
in base class, Virtual keyword is used. For example. Let’s us look at the
classes Car and Ford as shown below.
class Car
{
public Car()
{
Console.WriteLine("Base Class Car");
}
public
virtual void DriveType()
{
Console.WriteLine("Right Hand Drive");
}
}
class Ford : Car
{
public Ford()
{
Console.WriteLine("Derived Class Ford");
}
public void
Price()
{
Console.WriteLine("Ford Price : 100K $");
}
public
override void DriveType()
{
Console.WriteLine("Right Hand ");
}
}
When following
lines of code get executed
Car CarFord = new
Car();
CarFord.DriveType();
CarFord = new
Ford();
CarFord.DriveType();
Output is as given
below.
·Base Class Car
·Right Hand Drive
·Base Class Car
·Derived Class Ford
·Right Hand
No comments:
Post a Comment