Thursday, September 4, 2008

.net C#.net and VB.net

ACCESS MODIFIERS
 * Classes and structs that are declared directly within a namespace (in other words, they are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.
 * Nested classes and structs may also be declared as private. Private nested types are not accessible from the containing type.

Derived classes cannot have greater accessibility than their base types. In other words, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.
Class members (including nested classes and structs) can be declared with any of the five types of access. Struct
 * members cannot be declared as protected because structs do not support inheritance.

The accessibility of a member can never be greater than the accessibility of its containing type. For example, 
 * a public method declared in an internal type has only internal accessibility.

When a member of a class or struct is a property, field, method, event, or delegate, and that member either is a 
 * type or has a type as a parameter or return value, the accessibility of the member cannot be greater than the 
 * type. For example, you cannot have a public method M that returns a class C unless C is also public. Likewise,
 * you cannot have a protected property of type A if A is declared as private.

User-defined operators must always be declared as public

Interfaces declared directly with a namespace can be declared as public or internal and like classes and structs,
 * interfaces default to internal access. Interface members are always public because the purpose of an interface 
 * is to enable other types to access a class or struct. No access modifiers can be applied to interface members.

Enumeration members are always public, and no access modifiers can be applied.

By default, delegates have internal access.

Any types declared within a namespace or at the top level of a compilation unit (for example, not within 
 * a namespace, class, or struct) are internal by default, but can be made public.
*/

/*
A private constructor is a special instance constructor. It is commonly used in classes that contain static members 
 * only. If a class has one or more private constructors and no public constructors, then other classes
 * (except nested classes) are not allowed to create instances of this class.
 * if you are designing your own type that contains only static members then you should mark the type as Sealed 
 * and define a private parameterless contructor that is never called.
*/

/*
C# doesnt allow a value type to define a parameterless constructor.
 * keep in mind that although C# doesnt allow value types with pareameterless costructors, the CLR does.
 * 
 *     struct point
    {
                public int x=5,y; // cannot have instance field initializers in structs. so x=5 is wrong..
 *                               //becoz there are no default constructors. so int x is correct but int x=5 is wrong...
                public classA a; //an object can be a part of a value type
 * 
 *                public point() // wrong...parameterless constructor is not allowed
 *              public point(int x)
 *              {
 *                  //in a constructor u have to initialize each field else compilation error
 *              this.x=x;
 *              this.y=x;
 *              this.a=new classA();
 *              }
 * }
 * 
 * system.int32 a = new system.int32() and int a = 0 has same code in IL. that mean when we wrote 0 then implicit constructor was called.
 * Any data type that compliler directly supports are called primitive types.   
we do not have instance field initializers in structs....so no objects in value types can be instantiated
*/

/* 
 *a new operator has no complementary delete oerator that is there is no way to explicitly free the memory allocated for an object. 
 * MSCorLib.dll assembly contains the definitions of all the core .NET Framework class Library types, such as Object, int32, String and so on.
 * all the value types of class are allocated in the heap and not on stack, only the value types of the functions are allocated on stack
 * in c#, types declared using struct are value types adn types declared using class are refernce types. 
 * Value types are derived from system.valuetype which is again derived from system.object.system.valuetype overrides
 * the equal method so that it returns true if the values fo the two object's fields match.
 * it also overrides the gethashcode method so that it produces a hash code value using an algorithm that takes into 
 * account the values in the objects instance fields.
 * the reason why a type must define both equals and gethashcode is that the implementation of the 
 * system.collections.hashtable type requires that any two objects that are equal must have same hash code value. 
 * 
 * 
 * someval v1; //someval is a value type.
 * this line produces IL that allocates the instance on the thread stack and zeroes the fields.the only problem is 
 * that C# thinks the instance initialized if u use the new operator or a direct value. so if u try to use it will
 * throw complile time error.
 * 
 * 
 *A READONLY variable can only be assigned in a constructor or variable initializer(repeations are allowed) but
 * cant be declared in a function or constructor.
 * 
 * The compiler saves the CONSTANT value in the module metadata. You can define constant only for types that your
 * compiler considers as primitive types.
 * 
 * A lot of types(System.String) overload the equality (==) and inequality operators.
 * 
 * 
 * An EVENT in C# is a way for a class to provide notifications to clients of that class when some interesting
 * thing happens to an object.
 * DELEGATE is a class that derive from System.MulticastDelegate and includes following  members:
 * EnumConnectionCallback constructor
 * Invoke
 * BeginInvoke
 * EndInvoke
 * 
 * 
 * XML DUCUMENTATION - One of the nice side-effects of setting this configuration value is that documentation 
 * warnings are then enabled:
All overlaoded OPERATOR methods must be defined as public and static.
* should have a return type
* unary operator - single argument, binary operator - 2 arguments.
*
* DELEGATES are reference types that encapsulate a method(either static or instance) with a specified signature.
* CALLBACK methods enable you to pass a function pointer to another function that will then call you back.

No comments: