Jan 29, 2010

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

No comments:

Post a Comment