Archive for the ‘c# 2.0’ Category

Playing with IL code

July 31, 2007

This is an interesting post on how to tweak the IL code. And here is an example on how to modify the IL code for a speacial case of tail recursion, to remove the stack overflow error.


Defining variable

July 5, 2007

Can you declare variables on demand? I mean to say whenever in my code, I need a variable- I declare the variable, will this improve the performance of my application?

The answer is no, the CLR defines all the variables of a function at the starting of the funtion,  so no performance benifits etc., this can be checked by reading the IL of the code using ILDASM.

more command

June 24, 2007

I have created the following code snippet to print the Application.cfm file content, paginated on the system console (similar to the more command)

TextReader tr = new StreamReader(@”c:\Application.cfm”);
while (tr.Peek() != -1)
if (System.Console.CursorTop >= System.Console.WindowHeight – 4)
System.Console.WriteLine(“Press any key to continue…”);

chap 1

June 24, 2007

I have finished reading the first chapter, and following are my notes

  • using directive directly inside the namespace is invalid
  • using directive statements are not commulative
  • a class not nested in any namespace is in the global namespace
  • namespaces can be nested
  • Main is entry point a C# program and Main can be in a class or a struct
  • Main method can be private
  • Main method cannot be overloaded
  • If there are multiple classes with Main method then you can set startup in the project properties
  • the default exit code for an application is zero,The exit code of a process is stored in the Process Environment Block (PEB) and readable through the GetExitCodeProcess API.
  • ? to declare a Nullable <T> type
  • Read here to understand why Nullable type struct was introduced
  • ?? to check for null value
  • C# switch does not allow cascading between switch case statements, it is only allowed if case does not have any statements defined
  • switch(k) where k can be int, char, enum or string type or any object, value or ref which can be converted to these types
  • The foreach statement confirms that the enumerator or elements of the collection are disposable objects. If so, the foreach statement calls the Dispose method on the applicable objects. Disposable objects implement the IDisposable interface.

Learning C#

June 24, 2007

I have picked the book “Programming Visual c# 2005” to be read from cover to cover.


June 24, 2007

What is boxing and unboxing?

To convert a value type to reference type


Best practice:
int i = 10;
Console.Writeline(“i = ” + i + ” ” + i);
Writeline invokes String Concat(object arg1, object arg2) method to generate the string to be passed on to Writeline method.
Now here when passing the value of i boxing happens. If there is a reptead use of a variable in the Writeline statement it can explicitly boxed and then passed on to the Writeline function.

Which is faster?
int i =10;
System.Int32 i =10;
Ans:Both will generate the same IL code

generic class

June 24, 2007

Will the following code compile ?

class testgeneric<type>
type _i;

public type I
get { return _i; }
set { _i = value; }

class testgeneric
int _i;

public int I
get { return _i; }
set { _i = value; }


Ans: Yes

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


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


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