What are the advantages and disadvantages of using
collection classes present in
System.Collections namespace
We will understand the advantages and disadvantages of using
collection classes present in System.Collections namespace, using ArrayList
collection class. The same advantages and disadvantages apply to all other
collection classes like Stack, Queue and Hashtable classes.
Advantages of using ArrayList:
1.
ArrayList can grow in size dynamcally. In the
example below, the Numbers ArrayList initial size is set 2. But we have added 3
elements. This proves that ArrayList, and the rest of the collection classes
like Stack, Queue and Hashtable can grow in size dynamically. If Numbers, was
an integer array, then we would have run into Index Out of Range compiler
error.
2.
3.
2. ArrayList provide several convinient methods
to add and remove elements to the collection. You can use the Add(), Remove()
etc which are very handy to add and remove elements respectively. Similarly the
Stack, Queue and Hashtable classes have thier respective methods, to add or
remove the elements.
4.
5.
6.
Disadvantages of using ArrayList:
7.
ArrayList and all other collection classes like
stack, queue and hashtable which are present in System.Collection namespace
operate on object and hence are loosely typed. The loosely typed nature of
these collections make them vulnerable to runtime errors. Click here for an
example on how the loosely typed nature of an ArrayList can cause runtime
erros.
8.
9.
Loosley typed collections can also cause
performance overhead, because boxing and unboxing happens. In the example
below, Numbers is an arraylist. We are stroing 10 and 20 which are integers and
value types. Since, arraylist operate on object type, and object type is a
10.
11.
reference type, the value 10 is boxed and
converted into a reference type. The same is the case with integer 20. If we
store 100 integers in the arraylist. All the 100 intgers are boxed, meaning
converted into reference types and then stored in the collection.
12.
13.
When we try to retrieve the elements out of the
collection, we covert the object type back to integer type, unboxing happens.
So this unnecessary boxing and unboxing happens behind the scenes everytime we
add and remove value types to the collection classes present in System.Collections
namespace. This can severly affect the performance, especially if your
collections are large. To solve this problem, we have generics introduced in
dotnet. Click here for the advantages and disadvantages of using generics.
14.
What are the advantages and disadvantages
of using arrays
Advantages of using arrays:
1. Arrays are strongly typed, meaning you
can only have one type of elements in the array. The strongly typed nature of
arrays gives us 2 advantages. One, the performance will be much better because
boxing and unboxing will not happen. Second, run time errors can be prevented
because of type mis matches. Type mis matches and runtime errors are most
commonly seen with collection classes like ArrayList, Queue, Stack etc, that
are present in System.Collections namespace.
In the example below, Numbers is an integer
array. When we try to store a string in the integer array, a compiler error is
reported stating cannot implicitly convert string to integer. This is why we
call arrays are strongly typed.
In the example below, Numbers is an
ArrayList. Collections of type arraylist are loosely typed. This means any type
of elements can be added to the collection. ArrayList operate on object type,
which makes them loosely typed. No compiler error is reported, but when we run
the application, a runtime error is reported as shown. In software development,
it is always better to catch errors at compile time rather than at runtime.
Disadvantages of using arrays:
1. Arrays are fixed in size and cannot grow
over time, where ArrayList in System.Collections namespace can grow
dynamically.
2. Arrays are zero index based, and hence a
little difficult to work with. The only way to store or retrieve elements from
arrays, is to use integral index. Arrays donot provide convinient methods like
Add(), Remove() etc provided by collection classes found in System.Collections
or System.Collections.Generics namespaces, which are very easy to work with.
----------------
C# Interview Questions related to
Interfaces.
Explain what is an Interface in C#?
An Interface in C# is created using the
interface keyword. An example is shown below.
using System;
namespace Interfaces
{
interface IBankCustomer
{
void DepositMoney();
void WithdrawMoney();
}
public class Demo : IBankCustomer
{
public void DepositMoney()
{
Console.WriteLine("Deposit
Money");
}
public void WithdrawMoney()
{
Console.WriteLine("Withdraw
Money");
}
public static void Main()
{
Demo DemoObject = new Demo();
DemoObject.DepositMoney();
DemoObject.WithdrawMoney();
}
}
}
In our example we created IBankCustomer
interface. The interface declares 2 methods.
1. void DepositMoney();
2. void WithdrawMoney();
Notice that method declarations does not
have access modifiers like public, private, etc. By default all interface
members are public. It is a compile time error to use access modifiers on
interface member declarations. Also notice that the interface methods have only
declarations and not implementation. It is a compile time error to provide
implementation for any interface member. In our example as the Demo class is
inherited from the IBankCustomer interface, the Demo class has to provide the
implementation for both the methods (WithdrawMoney() and DepositMoney()) that
is inherited from the interface. If the class fails to provide implementation
for any of the inherited interface member, a compile time error will be
generated. Interfaces can consist of methods, properties, events, indexers, or
any combination of those four member types. When a class or a struct inherits
an interface, the class or struct must provide implementation for all of the
members declared in the interface. The interface itself provides no
functionality that a class or struct can inherit in the way that base class functionality
can be inherited. However, if a base class implements an interface, the derived
class inherits that implementation.
Can an Interface contain fields?
No, an Interface cannot contain fields.
What is the difference between class
inheritance and interface inheritance?
Classes and structs can inherit from
interfaces just like how classes can inherit a base class or struct. However
there are 2 differences.
1. A class or a struct can inherit from
more than one interface at the same time where as A class or a struct cannot
inherit from more than one class at the same time. An example depicting the
same is shown below.
using System;
namespace Interfaces
{
interface Interface1
{
void Interface1Method();
}
interface Interface2
{
void Interface2Method();
}
class BaseClass1
{
public void BaseClass1Method()
{
Console.WriteLine("BaseClass1
Method");
}
}
class BaseClass2
{
public void BaseClass2Method()
{
Console.WriteLine("BaseClass2
Method");
}
}
//Error : A class cannot inherit from more
than one class at the same time
//class DerivedClass : BaseClass1,
BaseClass2
//{
//}
//A class can inherit from more than one
interface at the same time
public class Demo : Interface1, Interface2
{
public void Interface1Method()
{
Console.WriteLine("Interface1
Method");
}
public void Interface2Method()
{
Console.WriteLine("Interface2
Method");
}
public static void Main()
{
Demo DemoObject = new Demo();
DemoObject.Interface1Method();
DemoObject.Interface2Method();
}
}
}
2. When a class or struct inherits an
interface, it inherits only the method names and signatures, because the
interface itself contains no implementations.
Can an interface inherit from another
interface?
Yes, an interface can inherit from another
interface. It is possible for a class to inherit an interface multiple times,
through base classes or interfaces it inherits. In this case, the class can
only implement the interface one time, if it is declared as part of the new
class. If the inherited interface is not declared as part of the new class, its
implementation is provided by the base class that declared it. It is possible
for a base class to implement interface members using virtual members; in that
case, the class inheriting the interface can change the interface behavior by
overriding the virtual members.
Can you create an instance of an interface?
No, you cannot create an instance of an
interface.
If a class inherits an interface, what are
the 2 options available for that class?
Option 1: Provide Implementation for all
the members inheirted from the interface.
namespace Interfaces
{
interface Interface1
{
void Interface1Method();
}
class BaseClass1 : Interface1
{
public void Interface1Method()
{
Console.WriteLine("Interface1
Method");
}
public void BaseClass1Method()
{
Console.WriteLine("BaseClass1
Method");
}
}
}
Option 2: If the class does not wish to
provide Implementation for all the members inheirted from the interface, then
the class has to be marked as abstract.
namespace Interfaces
{
interface Interface1
{
void Interface1Method();
}
abstract class BaseClass1 : Interface1
{
abstract public void Interface1Method();
public void BaseClass1Method()
{
Console.WriteLine("BaseClass1
Method");
}
}
}
A class inherits from 2 interfaces and both
the interfaces have the same method name as shown below. How should the class
implement the drive method for both Car and Bus interface?
namespace Interfaces
{
interface Car
{
void Drive();
}
interface Bus
{
void Drive();
}
class Demo : Car,Bus
{
//How to implement the Drive() Method
inherited from Bus and Car
}
}
To implement the Drive() method use the
fully qualified name as shown in the example below. To call the respective
interface drive method type cast the demo object to the respective interface
and then call the drive method.
using System;
namespace Interfaces
{
interface Car
{
void Drive();
}
interface Bus
{
void Drive();
}
class Demo : Car,Bus
{
void Car.Drive()
{
Console.WriteLine("Drive Car");
}
void Bus.Drive()
{
Console.WriteLine("Drive Bus");
}
static void Main()
{
Demo DemoObject = new Demo();
((Car)DemoObject).Drive();
((Bus)DemoObject).Drive();
}
}
}
What do you mean by "Explicitly
Implemeting an Interface". Give an example?
If a class is implementing the inherited
interface member by prefixing the name of the interface, then the class is
"Explicitly Implemeting an Interface member". The disadvantage of
Explicitly Implemeting an Interface member is that, the class object has to be
type casted to the interface type to invoke the interface member. An example is
shown below.
using System;
namespace Interfaces
{
interface Car
{
void Drive();
}
class Demo : Car
{
// Explicit implementation of an interface
member
void Car.Drive()
{
Console.WriteLine("Drive Car");
}
static void Main()
{
Demo DemoObject = new Demo();
//DemoObject.Drive();
// Error: Cannot call explicitly
implemented interface method
// using the class object.
// Type cast the demo object to interface
type Car
((Car)DemoObject).Drive();
}
}
}
No comments:
Post a Comment