Monday, January 19, 2009

What are Types in .NET ?

First thing that need to understand and i.e. Type is something which decides the type of object you have created. All the primitive Types in .NET have fixed sizes and it will be the same size on any system.

Adding more words to Types ,In .NET the object you are going to create,what will be its scope,what will be its limitation,what will be its size and how they can be interoperable is decided by Type.

Simply there are are two distinct groups of data types, Value Type and Reference Type. As per OOps value types are those where state dominates while for reference types behaviour dominates.

For primitive types I will say any data type directly supported by compiler.
e.g. System.Int32 a = new System.Int32(5);

For value type there are certain remarkable points , that are
*The type acts like a primitive type.
*The type doesn't need to inherit from any other type.
*The type will not have any other types derived from it.
-
*Sherlock Holmes

Wednesday, January 7, 2009

Value Types and Reference Types Vs Passing Value by Reference And Value

Before I will go for different aspects of value and referece type it is necessary to highlight a point which become crucial for the understanding.

Very softly it sounds that Passing values by Reference or Value are similar to reference type and value type concepts but I'm sorry its not.So let's go through the basics first, Values types and reference types could be diffrentiated down like this..

Value types :
All numeric data types int,double
Boolean, Char, and Date
All structures, even if their members are reference types
Enums, since the type included inside them is always Byte, Short, Integer,Long

Reference types :
String
All arrays, even if elements contained are value types
Class types
And Delegates

Now come to CLASS Types - every object created is a Reference type.It means that if you pass it ByRef or ByVal, any changes you make to it are going to be reflected and to be
made to the object itself. I know it seems a sweeping statement, but for all reasons and purposes, it's fairly true.


So Passing a Reference type ByValue passes a copy of a pointer(memory location) to the value.While passing a value type it simply acts like it's actually passing its own data.

If you have a Reference type composed exclusively of Value Types then it will behave just like a reference type but it's members will behave like Value types.

If you pass a Structure for instance, Even if it is completely composed of Reference types, it will behave as a Value type, because after all, that's what it is.

Now what will happen if you try to copy or clone a Reference Type. Well,the short answer is that it will behave just like you'd expect a reference type to behave.

Finally I will say Reference and Value types are very very different in .NET than anything prior to it. If you don't get under the actuals and not try to understand they way they work, you are going to have it as a pain..
-
*Indigo Ocean Dreams: 4 Children's Stories Designed to Decrease Stress, Anger and Anxiety while Increasing Self-Esteem and Self-Awareness

Sunday, January 4, 2009

Difference between virtual and abstract - Similar working nature but having different reason to be there

Well, Virtual methods allow subclasses to provide their own implementation of that method using the override keyword if they are wished while Abstract methods in a class contain no method body, and are implicitly virtual.Neither abstract or virtual can be declared private, since it will crush the purpose, and subclasses must override them using the same method signature.If a class has any abstact methods, then it must report itself as abstract I mean it has to, so that no instances of it can be created.
If a subclass needs to override a member (i.e. you can't specify a meaningful body in the base class, as it can't be defined), then it should be declared abstract and it will act as base-class. But if you just want to give the subclass the ability to override a method then it should be virtual.Any abstract member forces the entire class to be abstract, it means you can't create actual instances of the base class.
You must override when you deriving from abstract class. You can override methods (if for some reason you need to do something else) when you deriving from virtual.
In some cases you cannot provide an implementation for some methods, but method has to be there because it is part of the class interface. You want the programmers using the class to provide this implementation and you don't want to let them instantiate objects of this class because without this method it won't simply work. In this case you declare the method and the class as abstract. Abstract classes cannot be instantiated because they are not complete.
In other cases you can provide a default implementation of a method which is fair enough to make the control work even if not re-defined or changed, but you want to give the programmers using your class as a base class to provide their better implementation. In this case you declare the method as virtual thus, giving the programmers options whether to use the default or provide their own implementation and twist the results.
-
*Sharp Electronics PW-E550 Electronic Dictionary

Differences between Value And Reference Type

Starting start from very basic that Types are template that describe a set of data encapsulation and functionality.

Basically there are two kinds of types, they are

* Reference Type (Classes)
* Value Type (Structure)
The primary difference is the way variable data is accessed.
To understand it let’s go through stack and heap. All the data associated with value type are allocated on stack while a variable on reference type exists in two memory locations. The actual object data is stored on heap and a variable containing pointer (address or reference) to that object is allocated on stack.

* If an object is left without having any reference, it is subject to garbage collection. It is the
CLR which manages allocation of heap memory for objects and controls their recuperation.

Examples of value and reference types:
* Value Type Integer, Bool, Char, Structure, Enum etc.
* Reference Type Classes, Delegates, Array Types etc.

-
*1X4 USB Switchview Secure Niap Certification Approved

Saturday, January 3, 2009

Exploring Static Classes and Static Class Members

To start with this topic I will say static class is mostly same as a non-static class, but having one major difference that a static class cannot be instantiated. In other words, I will say you cannot use the new keyword to create a variable of the static class type. Because there is no instance variable which needs to be initialized, you simply access the members of a static class by using the class name itself.

Since the type information for a static class is loaded by the .NET Framework CLR when the program that references the class is loaded. The program cannot specify exactly or will not guaranteed when the class is loaded. However, it is guaranteed that it will be loaded and to have its fields initialized and its static constructor will be called before the class is referenced for the first time in your program. A static constructor is only called or invoked one time, and a static class remains in memory througout or for the lifetime of the application domain in which executed program resides.


Let's in a few words draw the basic features of a static class:
*It contains only static members.
*It cannot be instantiated. (Object can not be created)
*It is also sealed. (Can not be inherited)
*It cannot contain instance constructors.(So that members will not be initialized)

Creating a static class is basically the same as creating a class that contains only static members and a private constructor, As a private constructor prevents the class from being instantiated. The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added and it will guarantee that instances of this class cannot be created.

Static classes are sealed i.e they cannot be inherited. They cannot inherit from any class except Object. Static classes can contain a static constructor. Non-static classes should also define a static constructor if the class contains static members that require non-trivial initialization.

A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.


It is more emblematic to declare a non-static class with some static members, than to declare an entire class as static. Basically there are two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances.

Static methods can be overloaded but not overridden, As they belong to the class, and not to any instance of the class. Although a field cannot be declared as static const, a const field is essentially static in its behavior. It belongs to the type, not to instances of the type. Therefore, const fields can be accessed by using the same ClassName.MemberName notation that is used for static fields. No object or instance is required.

As C# does not support static local variables (variables that are declared in method scope or have local scope). So it will be fair to declare static class members by using the static keyword before the return type of the member, as shown in the following example:


public class Bike{
public static int NoOfTyres = 2;
public static int SizeofFuelTank
{
get
{
return 13;
}
}
public static void Run() { }
public static event EventType RunOutOfFuel;
//Declare other non-static members..
}