Jan 29, 2010

Partial Classes in C#

- Usually a class placed in a single file. But to use that class in multiple files we can use partial classes.
- Partial allows classes, structs, interfaces to span across multiple files.
- Ex:
//file1.cs
partial class Class1
{
public void m1()
{
}
}
//file2.cs
partial class Class1
{
public void m2()
{
}
}
- After compile the source like

partial class Class1
{
public void m1()
{
}
public void m2()
{
}
}

Readonly Fields in C# classes

- whose value shouldnot be changed as "Const" but where the value is not known until runtime.
- "Const" fields are internally static, but we need to declare as static for "Readonly" fields.
- Readonly fields are constants and also allow to do some calculations to determine the initial value of the fields, but const fileds are not.
- We can assign values to Readonly fields in constructors only not anywhere else.

All about classes in C#

- Classes contains datamembers and function members.
- Datamembers are fields, constants, events.
- Function members are methods, properties, constructors, finalizers, operators, indexers.

Methods
========
- Methods in C# must have return type atleast void.

Value types and Reference types

- Value type stores its value directly and reference type stores a reference to the value.
- reference types stored on the heap, value types stored on the stack
- reference types are passed by using ref keyword.
- we must initialize the variable before it is passed to a method, either by value type or reference type. By using "OUT" parameters we can pass the variables to the method that has not been initialized.
- If the OUT parameter is not assigned a value within the body of the function then the method won't compile.
- Examples for val, ref, out parameters
public class ExforType
{
pubic void valTypeFunc(string s, int j)
{
s="bird";
}
pubic void refTypeFunc(ref string s, int m)
{
s="bird";
}
pubic void outTypeFunc(out string s, int n)
{
s="bird";
}
public static int main(String args[])
{
int i=0;
string s="cat";
string st="dog";
valTypeFunc(s,i);
Console.WriteLine(s)
refTypeFunc(ref s,i);
Console.WriteLine(s)
outTypeFunc(out st,i);
Console.WriteLine(st)
}
}

output:

cat
bird
bird

Method overloading
==================

- Methods with the same name but different numbers or types of parameters
- It is not sufficient for two methods to differ only in return types and ref,out parameters.


Properties
==========

- Properties are useful to provide security for the variables. If you don't want to modify a variable better use properties.
- Properties are defined by using get and set accessors.
- Example
private string name;
Private string nameProperty
{
get
{
return "Somename"
}
Private set
{
name="somename";
}
}
- Readonly properties - donot have set, writeonly properties - donot have get
- Property have only public get and private, protected set. one of the accessor must follow the access level of the property. In the above example set accessor follows the property access level.

Best Explanaion : http://www.csharphelp.com/2006/01/what-why-properties-in-c-part-1/

Inline methods
- There is no inline methods concept in C#. JIT compiler replaces all function calls by inline code.

Constructors
============

- Constructors are special functions that are called automatically when an object is instantiated.
- They must have the same name as the class and cannot have return type.
- Useful for intializing the values of fields.
- two types of constructors instance constructors(are executed when an object of that class is created) and static constructors(is executed only once and will be invoked before your code makes any reference to the class)

static constructors
===================

- static constructors doesnot have any reference types
- called by the .NET runtime when the class is loaded and not to be called by any code.
- static constructors cannot take any parameters
- there can be only one static constructor for a class.
- static constructors are used if we want to intialize static fields or properties before the class is first used
- static constructors can only access static members of the class
- Example
public class SCTest
{
public static readonly Color choice;
static SCTest()
{

}
}

Static Classes
==============

-A static class may only contain static members.
- Ex:
static class ExClass
{
public static void method1()
{
}
}
ExClass.method1();

Differnce between C# classes and structs

- Classes are reference types stored on the heap, structs are value types stored on the stack or inline(if they are part of another object that is stored on the heap)

- Classes support inheritance, structs don't support inheritance.

- Ex:
Class ExClass
{
public const string billday="Monday";
int custid;
}

struct ExStruct
{
public const string billday="Monday";
int custid;
}

ExClass ec=new ExClass();
ExStruct es=new ExStruct();

- If we need small datastructures, for performance reasons, we will go for structs instead of classes

- In classes, if we are not define any constructor, the compiler provides a default one otherwise not. But in structs, compiler always supplies a default no-parameter constructor.

- All classes derived from System.Object and all structs derived from System.ValueType.