typeforwarding attribute

June 24, 2007

It is an attribute which allows you to move a type from one assembly into another and in such a way that there is no need to recompile the clients that consume the original assembly.
For example there is a class P in assembly A (class library), and it is used by  a user in Assembly C (Console app).  Now we want to move the class P to assembly B (class library). Cut the definition of class P from Assembly A and paste it in B. Add B as references to A and add the typeforwarding attribute in Assembly A.

[assembly: System.Runtime.CompilerServices. TypeForwardedToAttribute(typeof(B.P))]

Rebuild A and B.
I don’t have to recompile Assembly C

Interface

June 24, 2007

Interface are contracts, it defines common set of members that all classes that implement the interface must provide.

Interfaces can be made up of methods, properties (getter/ setter), events, indexers. Interface does not allow fields.

Interface can inherit other interfaces
Interfaces can be inherited by struct
interfaces support multiple inheritance
interface methods are explicitly abstract ad public

Implicit and Explicit interfaces

An interface cannot be instantiated directly.

public is not a valid accessibility for an explicit implemented interface

Generics

June 24, 2007

Advantage of using generics

  •  reduced run time errors
  • improved performance

Generic class constraints

  • interface
  • Base class
  • Constructor
  • Reference or value type

Generic types and methods are not suitable in all circumstances:

  • The entry point method cannot be a member of an generic type.
  • Unmanaged types cannot be generic.
  • Constructors cannot be generic.
  • Operator member functions cannot be generic methods.
  • Properties cannot be generic.
  • Indexers cannot be generic.
  • Attributes cannot be generic

Generic types include classes, structures, and interfaces

struct

June 24, 2007

points to remember about structs

  • Within a struct declaration, fields cannot be initialized unless they are declared as const or static
  • struct does not have a default constructor
  • struct is a value type
  • struct constructor is automatically created by the compiler initializing the fields with the default values
  • structs inherit from another struct or class, and it cannot be the base of a class
  • When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.
  • struct cannot be a base class for  another class
  • struct can implement an interface

Using value types

June 24, 2007

There are three general value types

1. Built in types
2. User defined types
3. Enumeration

All the above value types are defined as a struct.
Since, they are not reference variables, we cannot assign null as the value.
If you want a value type can be assigned a null value then define the variable as
System.Nullable<T> where T is the type of the variable

For example System.Nullable<bool> flag = null;
so the flag variable can now be assigned either true, false or a null value.

The shortcut for the above statement is
bool? flag = null;
bool? b1,b2;
b1 = b2 = null;

If a variable is declared as nullable (Nullable is also a struct) it gets an additional property HasValue, which can be used to check if values is null or not
if(b1.HasValue)
System.Console.WriteLine(sizeof(System.Boolean)); return 1 byte.

TODO:
struct test
{
int i;
}
System.Console.WriteLine(sizeof(test));
This gives CTE.
How can my struct return the sizeof value

Why not
int? i = 10;
if (i==null)
System.Console.WriteLine(i);
else
System.Console.WriteLine(“no value”);

c# enums

June 24, 2007
  • enum is a value type, i.e. they are created on the stack
  • Default enum type is int.
    for example
    enum Test(yes,no);
    will create a int enum list
  • The value assigned to a instance of the enum is not limited to the named contants
    for example
    enum Test:int {t=9,y=8};
    Test t = (Test)10;
    System.Console.Writeln(t);
    will not throw any error and will print 10
  • enum Test:int{t=9};
    System.Console.Writeln(Test.t);
    What will be the output of the above snippet?
    will it be “9” or “t”
  • enum Test:int { t=19, y=18 };
    int i = Test.t;
    Console.Write(i);
    What will be the output of the above program?
  • Difference between enum and a constant
    enum is strongly types than contant
    For example

    the function
    int PrintNoOfDays(int month)
    can be called as
    const int Jan = 1;
    const int Feb = 2; ….
    PrintNoOfDays(Jan);

    or the function can be declared as
     can be called as
    enum Month {Jan,Feb,….)
    int PrintNoOfDays(Month m)

    PrintNoOfDays(Month.Jan)

    The second example is strongly typed

  • Enum under the covers
    When compiling an enum it is converted to struct enum Month {Jan,Feb,….)
    the above enum of months would be converted to
    struct Month : System.Enum
    {
    public const int Jan = 1;
    public const int Feb = 2;

    }
  • The following code will not give any error (there is a comma after the last enum list constant)public enum Test
    {
    a = 1,
    b = 0,

    }

  • What will be the output of
    System.Console.WriteLine(sizeof(Test));
  • Hello world!

    June 23, 2007

    Welcome to my blog. Here I will be writing about by experience while learning new things