Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, 24 May 2012

Value types and Reference types

Value types and Reference types

Here are the key points:
■ The value of a reference type expression (a variable for example) is a reference,
not an object.
■ References are like URLs—they are small pieces of data that let you access the
real information.
■ The value of a value type expression is the actual data.
■ There are times when value types are more efficient than reference types, and
vice versa.
■ Reference type objects are always on the heap, but value type values can be on
either the stack or the heap, depending on context.
■ When a reference type is used as a method parameter, by default the parameter
is passed by value—but the value itself is a reference.
■ Value type values are boxed when reference type behavior is needed; unboxing
is the reverse process.


Post By : Dipen Shah
Blog By : Dipen Shah

Boxing and Unboxing - c#

What is concept of Boxing and Unboxing ?
Boxing and unboxing act like bridges between value type and reference types. When we convert
value type to a reference type it’s termed as boxing. Unboxing is just vice-versa. When an object
box is cast back to its original value type, the value is copied out of the box and into the
appropriate storage location.

Example: of boxing and unboxing where integer data type are converted in to object
and then vice versa.
int i = 1;
object obj = i; // boxing
int j = (int) obj; // unboxing


Post By : Urvish Patel
Blog By : Dipen Shah

Tuesday, 22 May 2012

What is shadowing?

When two elements in a program have same name, one of them can hide and shadow the other
one. So in such cases the element, which shadowed the main element, is referenced.
Below is a sample code, there are two classes “Parent” and “ShadowedParent”. In
“Parent”, there is a variable “x” which is a integer. “ShadowedParent” overrides
“Parent” and shadows the “x” variable to a string.

Example:

Public Class Parent
Public x As Integer
End Class
Public Class ShadowedParent
Inherits Parent
Public Shadows x As String
End Class

Difference between Shadowing and Overriding:
• Overriding redefines only the implementation while shadowing redefines the whole
element.
• In overriding derived classes can refer the parent class element by using “ME” keyword,
but in shadowing you can access it by “MYBASE”.

Post By : Urvish Patel
Blog By : Dipen Shah
Learn New...