Archive for June, 2007

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)
{
System.Console.WriteLine(tr.ReadLine());
if (System.Console.CursorTop >= System.Console.WindowHeight – 4)
{
System.Console.WriteLine(“Press any key to continue…”);
System.Console.ReadKey(true);
System.Console.Clear();
}
}
System.Console.ReadKey(true);

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.

boxing

June 24, 2007

What is boxing and unboxing?
http://msdn.microsoft.com/msdnmag/issues/1200/dotnet/

Advantage?
To convert a value type to reference type

Disadvantage?
Performance

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;
or
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

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”);