Monday, May 5, 2008

DOTNET FAQS-8

oops concepts-2



(A) What are similarities between Class and structure ?
Following are the similarities between classes and structures :-
√ Both can have constructors, methods, properties, fields, constants,
enumerations, events, and event handlers.
√ Structures and classes can implement interface.
√ Both of them can have constructors with and without parameter.
√ Both can have delegates and events.
(A) What is the difference between Class and structure’s ?
Following are the key differences between them :-
√ Structure are value types and classes are reference types. So structures use
stack and classes use heap.
√ Structures members can not be declared as protected, but class members can
be. You can not do inheritance in structures.
√ Structures do not require constructors while classes require.
√ Objects created from classes are terminated using Garbage collector. Structures
are not destroyed using GC.
(B) What does virtual keyword mean ?
They are that method and property can be overridden.
(B) What are shared (VB.NET)/Static(C#) variables?
214
Static/Shared classes are used when a class provides functionality which is not specific to
any instance. In short if you want an object to be shared between multiple instances you
will use a static/Shared class.
Following are features of Static/Shared classes :-
√ They can not be instantiated. By default a object is created on the first method
call to that object.
√ Static/Shared classes can not be inherited.
√ Static/Shared classes can have only static members.
√ Static/Shared classes can have only static constructor.
Note :- In CD there is a folder “WindowsShared” which has a sample code for shared
variables.Below is a snippet. It has a “AddCount” function which increments a static
“intCount” variable. In form there are two buttons which creates a new object and displays
the count of the static variable. Even though the object is created and destroyed, the variable
values does not change. It retains its old value.
Public Class ClsShared
Shared intCount As Integer
Public Function AddCount() As Integer
intCount = intCount + 1
Return intCount
End Function
End Class
Public Class FrmSharedClasses
Inherits System.Windows.Forms.Form
Private Sub CmdInstance1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles CmdInstance1.Click
Dim pobjClsShared As New ClsShared()
MessageBox.Show(“The count at this moment is” &
pobjClsShared.AddCount.ToString())
End Sub
Private Sub CmdInstance2_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles CmdInstance2.Click
Dim pobjClsShared As New ClsShared()
MessageBox.Show(“The count at this moment is” &
pobjClsShared.AddCount.ToString())
215
End Sub
End Class
Figure :- 6.7 Shared/Static In Action
(B) What is Dispose method in .NET ?
.NET provides “Finalize” method in which we can clean up our resources. But relying on
this is not always good so the best is to implement “Idisposable” interface and implement
the “Dispose” method where you can put your clean up routines.
(B) What is the use of “OverRides” and “Overridable”
keywords ?
Overridable is used in parent class to indicate that a method can be overridden. Overrides
is used in the child class to indicate that you are overriding a method
(A) Where are all .NET Collection classes located ?
216
System.Collection namespace has all the collection classes available in .NET.
(A) What is ArrayList ?
Array is whose size can increase and decrease dynamically. Array list can hold item of
different types. As Array list can increase and decrease size dynamically you do not have
to use the REDIM keyword. You can access any item in array using the INDEX value of
the array position.
(A) What’s a HashTable ?
Twist :- What’s difference between HashTable and ArrayList ?
You can access array using INDEX value of array, but how many times you know the
real value of index. Hashtable provides way of accessing the index using a user identified
KEY value, thus removing the INDEX problem.
(A) What are queues and stacks ?
Queue is for first-in, first-out (FIFO) structures. Stack is for last-in, first-out (LIFO)
structures.
(B) What is ENUM ?
It’s used to define constants.
(A) What is nested Classes ?
Nested classes are classes within classes. In sample below “ClsNested” class has a
“ChildNested” class nested inside it.
Public Class ClsNested
Public Class ChildNested
Public Sub ShowMessage()
MessageBox.Show(“Hi this is nested class”)
End Sub
End Class
End Class
This is the way we can instantiate the nested class and make the method call.
Dim pobjChildNested As New ClsNested.ChildNested()
pobjChildNested.ShowMessage()
217
Note:-In CD the above sample is provided in “WindowsNestedClasses”.
(B)What is Operator Overloading in .NET?
It provides a way to define and use operators such as +, -, and / for user-defined classes
or structs. It allows us to define/redefine the way operators work with our classes and
structs. This allows programmers to make their custom types look and feel like simple
types such as int and string.
VB.NET till now does not support operator overloading. Operator overloading is done
by using the “Operator” keyword.
Note:- Operator overloading is supported in VB.NET 2005
(I) In below sample code if we create a object of class2
which constructor will fire first ?
Public Class Class1
Sub New()
End Sub
End Class
Public Class class2
Inherits Class1
Sub New()
End Sub
End Class
* I leave this to the readers......
(B)What is the significance of Finalize method in .NET?
.NET Garbage collector does almost all clean up activity for your objects. But unmanaged
resources (ex: - Windows API created objects, File, Database connection objects, COM
objects etc) is outside the scope of .NET framework we have to explicitly clean our
resources. For these types of objects .NET framework provides Object. Finalize method
218
which can be overridden and clean up code for unmanaged resources can be put in this
section.
(A)Why is it preferred to not use finalize for clean up?
Problem with finalize is that garbage collection has to make two rounds in order to remove
objects which have finalize methods.
Below figure will make things clear regarding the two rounds of garbage collection rounds
performed for the objects having finalized methods.
In this scenario there are three objects Object1, Object2 and Object3. Object2 has the
finalize method overridden and remaining objects do not have the finalize method
overridden.
Now when garbage collector runs for the first time it searches for objects whose memory
has to free. He can see three objects but only cleans the memory for Object1 and Object3.
Object2 it pushes to the finalization queue.
Now garbage collector runs for the second time. He see’s there are no objects to be
released and then checks for the finalization queue and at this moment it clears object2
from the memory.
So if you notice that object2 was released from memory in the second round and not first.
That’s why the best practice is not to write clean up Non.NET resources in Finalize
method rather use the DISPOSE.
219
Figure :- 6.8 Garbage collection in actions
(I)How can we suppress a finalize method?
GC.SuppressFinalize ()
(B)What is the use of DISPOSE method?
Dispose method belongs to IDisposable interface. We had seen in the previous section
how bad it can be to override the finalize method for writing the cleaning of unmanaged
resources. So if any object wants to release its unmanaged code best is to implement
220
IDisposable and override the Dispose method of IDisposable interface. Now once your
class has exposed the Dispose method it’s the responsibility of the client to call the
Dispose method to do the cleanup.
(A)How do I force the Dispose method to be called
automatically, as clients can forget to call Dispose method?
Note :- I admire this question.
Call the Dispose method in Finalize method and in Dispose method suppress the finalize
method using GC.SuppressFinalize. Below is the sample code of the pattern. This is the
best way we do clean our unallocated resources and yes not to forget we do not get the hit
of running the Garbage collector twice.
Note:- It will suppress the finalize method thus avoiding the two trip.
Public Class ClsTesting
Implements IDisposable
Public Overloads Sub Dispose()Implements IDisposable.Dispose
' write ytour clean up code here
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Dispose()
End Sub
End Class
(I)In what instances you will declare a constructor to be
private?
When we create a private constructor, we can not create object of the class directly from
a client. So you will use private constructors when you do not want instances of the class
to be created by any external client. Example UTILITY functions in project will have no
221
instance and be used with out creating instance, as creating instances of the class would
be waste of memory.
(I)Can we have different access modifiers on get/set
methods of a property ?
No we can not have different modifiers same property. The access modifier on a property
applies to both its get and set accessors.
(I)If we write a goto or a return statement in try and catch
block will the finally block execute ?
The code in then finally always run even if there are statements like goto or a return
statements.
(A)What is Indexer ?
An indexer is a member that enables an object to be indexed in the same way as an array.
(A)Can we have static indexer in C# ?
No.
(A)In a program there are multiple catch blocks so can it
happen that two catch blocks are executed ?
No, once the proper catch section is executed the control goes finally to block. So there
will not be any scenarios in which multiple catch blocks will be executed.
(A) What is the difference between System.String and
System.StringBuilder classes?
System.String is immutable; System.StringBuilder can have mutable string where a variety
of operations can be performed.

No comments: