Sie sind auf Seite 1von 41

.

Net C# Data Types


Data is physically stored inside cells of memory. This memory could be physical memory (Hard
disk) or logical memory (RAM). Any cell of memory is represented with a unique address. This
address is more than some combination of numbers or symbols.
C# language provides for practically all the data types. These types can be divided in three
categories: value types, reference types and pointer types.
There are some more basic concepts to be learnt before the discussion of the data types. This
is about variables and constants. A Variable is a named cell of memory used for data storage. A
Variable value can be changed anytime. Every variable must have a type and this type must be
set before it is used. Qualifying a variable with a type is called as declaration of variable. The type
of a variable is the most important aspect and it defines the behavior of variable. All variables can
be divided into seven main categories depending on the context of usage:
1.
2.
3.
4.
5.
6.
7.

Static variables
Variable of instance
Array's elements
Parameters given by reference
Parameters given by value
Returned values
Local variables.

Static Variables will be alive throughout the life of a program. It can be declared using static
modifier.
An Instance variable is a variable declared without static modifier. Usually it is declared inside a
class or structure definition.
Parameter Values can be viewed as input parameters into methods:
public static void Sum(int a, int b)
{
Console.WriteLine("The sum of elements {0} and {1} is {2}",a,b,a + b);
}
This code writes in console values of variables a, b and their summary value. Now if the values
of these parameters are modified inside the method, this change will not be reflected inside the
main function. It is because the compiler creates copies of them when it passes them as value
types. This ensures that their original values are not modified.
Instead if one wants to modify the parameter variables inside the function, C# has something
called Reference variables. Reference variables also have a modifier out which can be used
before their type. Look at the following example:
public static void SumRef(ref int a, ref int b)
{
a = 4;
b = 6;
Console.WriteLine("The sume of elements {0} and {1} is {2}",a,b,a + b);

}
Now this method modifies the value of variables a and b with values 4 and 6. These values are
retained even after the execution of the function gets completed.
If the parameters need to be returned then they can be qualified with out modifier or as returned
parameter in method definition. Here is an example of both of them, in which both of them return
the same value:
public static int SumOut(int a, int b, out int sum1)
{
sum1 = a+b;
Console.WriteLine("The sum1 of elements {0} and {1} is {2}",a,b,a+b);
return sum1;
}
In main function it must be called in the next manner:
int sume ;
sume = SumeOut(2,2, out sume);
Constants in C#:
Constant type of data cannot be changed. To declare a constant the keyword const is used. An
example for the constant declaration is: const double PI = 3.1415;
Values types in C#:
Value type stores real data. When the data are queried by different function a local copy of it
these memory cells are created. It guarantees that changes made to our data in one function
don't change them in some other function. Let see at a simple example:
public class IntClass
{
public int I = 1;
}
Here we have simple class that contains only one public data field of integer type. Now have a
look on its usage in main function:

static void Main(string[] args)


{
// test class
int i = 10;
int j = i;
j = 11;
IntClass ic1 = new IntClass();
IntClass ic2 = ic1;
ic2.I = 100;

Console.WriteLine("value of i is {0} and j is {1}",i,j);


Console.WriteLine();
Console.WriteLine("value of ic1.I is {0} and ic2.I is {1}",ic1.I,ic2.I);
Console.WriteLine();
}
Reference Types in C#:
In the above example, assume that First we have two value type i and j. Also assume that the
second variable is initialized with the value of the first one. It creates new copy in memory and
after it the values of these variables will be next:
i = 10;
j = i;
There are a few more things written in the above example for explaining the Reference Types in
C#. At first, the variable ic1 of IntClass is created using dynamic memory allocation. Then we
initialize the variable ic2 with value of ic1. This makes both the variables ic1 and ic2 referring to
the same address. If we change a value of ic2, it automatically changes the value of ic1.
Now, over to the discussions about the important value types used in C#. The category simple
types contains some predefined or system types that also are commonly used in other
programming languages. It contains integer types: byte, Sbyte, Long, Ulong, Short, Ushort, int,
Uint. These common types differs only range of values and sign.
Next simple type is character type. To declare a variable of this type need use keyword char. It
can take values of characters or numbers as 16-digit symbol of the type Unicode.
The Boolean type has only two values: true, false. But these values cannot be assigned with a
0 or 1 as in C++ language.
Next category of simple types is floating point types. It contains two types float and double.
Float type can get values in range from 1.5*10-45 to 3.4*1038. Double type has range of values
from 5.0*10-324 to 1.7*10308.
A structural value types are struct and enum. Struct is a the same as class but it uses real
values not references. The following code snippet contains the definition for struct:
struct Point3D
{
public float m_x;
public float m_y;
public float m_z;
public float [] GetArray()
{
float [] arr = new float[3];
arr[0] = m_x;
arr[1] = m_y;

arr[2] = m_z;
return arr;
}
}
The above is declaration for a simple structure of real 3D point. As you see a class declaration
looks very similar to the struct except that the class also has a constructor.
Enumerated types can be used to create some set of identifiers that can have values of simple
type. Let us see at example of enum type:
public enum Days
{
Monday,
Tuesday,
Wensday,
Thursday,
Friday,
Saturday,
Sunday
}
In example there are enum that has days of week names. The values of days by default are in
range from 0 to 6.
Common types in C#:
Object in C# language is universal; it means that all supported types are derived from it. It
contains only a couple of methods: GetType() - returns a type of object, ToString() returns string
equivalent of type that called.
Next type is class. It is declared in the same manner as structure type but it has more advanced
features.
Interface is an abstract type. It is used only for declaring type with some abstract members. It
means members without implementations. Please, have a look at piece of code with a declaration
for a simple interface:
interface IRect
{
int Width
{
get;
set;
}
int Height

{
get;
set;
}
int CalculateArea();
}
The members of interface can be methods, properties and indexers.
Next reference type to be dealt is delegate. The main goal of delegate usage is encapsulation
of methods. It most like at pointers to function in C++.
String is common type that is used in almost all programming languages of high level. An
example of string declaration and initialization:
string s = "declaration and init";
The last very used reference type is array. Array it is set of elements that have the same type.
Array contains list of references on memory cells and it can contain any amount of members. In
C# there are three types of arrays: one-dimensional, two-dimensional and jagged array.
So, this covers almost all types used in C#. All these types can be cast to another type using
special rules. An implicit casting can be done with types when values of variables can be
converted without losing of any data. There is special type of implicit casting called boxing. This
enables us to convert any type to the reference type or to the object type. Boxing example:

// boxing
char ch = 'b';
object obj = ch;
Console.WriteLine("Char value is {0}",ch);
Console.WriteLine("Object value is {0}",obj);
Console.WriteLine();
This piece of code prints the same values of integer type variable and object type variable. The
opposite process to the boxing is un-boxing. An example for un-boxing is as follows.

// unboxing
float q = 4.6f;
object ob = q;
Console.WriteLine("Object value is {0}",ob);
float r = (float)ob;
Console.WriteLine("Float value is {0}",r);
So, it is main item of common data type creating and using. All sources are attached. To
compile and run it need to run .NET command line. Just type: csc DataTypes.cs. It creates
DataTypes.exe that can be run as standard executable file.

.Net C# Tutorial Intermediate Language MSIL


Microsoft Intermediate Language (MSIL) is a platform independent language that gets compiled
into platform dependent executable file or dynamic link library. It means .NET compiler can
generate code written using any supported languages and finally convert it to the required
machine code depending on the target machine.
To get some clarity in this language we need to write some code. In this tutorial we?ll use a very
simple piece of source code in C# .Net. Now we need to compile this code using csc command in
Microsoft .NET on the command line. To do this, please type next string: csc ILSample.cs. After it
compiler will create an executable file named ILSample.exe.
After this type the command called ILDasm. It will open application called Intermediate
Language Disassembler. In file menu choose open and find our executable file.This application
opens our assembly showing all structural units viz., classes, methods, data fields and all global
and local application data. Now if you click on the method it shows code in intermediate
language. This code is most like at language with independent set of instructions.

public double GetVolume()


{
double volume = height*width*thickness;
if(volume<0)
return 0;
return volume;
}

You can get the strings of Microsoft Intermediate Language code using ILDasm:

.method public hidebysig instance float64


GetVolume() cil managed
{
// Code size 51 (0x33)
.maxstack 2
.locals init ([0] float64 volume,
[1] float64 CS$00000003$00000000)
IL_0000: ldarg.0
IL_0001: ldfld float64 OOP.Aperture::height
IL_0006: ldarg.0
IL_0007: ldfld float64 OOP.Aperture::width
IL_000c: mul
IL_000d: ldarg.0
IL_000e: ldfld float64 OOP.Aperture::thickness
IL_0013: mul

IL_0014: stloc.0
IL_0015: ldloc.0
IL_0016: ldc.r8 0.0
IL_001f: bge.un.s IL_002d
IL_0021: ldc.r8 0.0
IL_002a: stloc.1
IL_002b: br.s IL_0031
IL_002d: ldloc.0
IL_002e: stloc.1
IL_002f: br.s IL_0031
IL_0031: ldloc.1
IL_0032: ret
} // end of method Aperture::GetVolume

To clearly understand that IL really is immediately language you should write the same code in
VB .NET and look at these two sources in IL. These methods will be almost identical.
The main advantages of IL are:
1. IL isn?t dependent on any language and there is a possibility to create applications with
modules that were written using different .NET compatible languages.
2. Platform independence - IL can be compiled to different platforms or operating systems.

OOP & C#
The skeleton of object - oriented programming is of course the concepts of class. This C#
tutorial on OOPS explains classes and their importance in implementation of object ? oriented
principles.
Any language can be called object ? oriented if it has data and method that use data
encapsulated in items named objects. An object ? oriented programming method has many
advantages, some of them are flexibility and code reusability.
All the programming languages supporting Object oriented Programming will be supporting
these three main concepts:
1. Encapsulation
2. Inheritance
3. Polymorphism
Encapsulation in C#:
Encapsulation is process of keeping data and methods together inside objects. In this way
developer must define some methods of object?s interaction. In C# , encapsulation is realized
through the classes. A Class can contain data structures and methods. Consider the following
class.

public class Aperture


{
public Aperture()
{
}
protected double height;
protected double width;
protected double thickness;
public double GetVolume()
{
double volume = height*width*thickness;
if(volume<0)
return 0;
return volume;
}
}

In this example we encapsulate some data such as height, width, thickness and method
GetVolume. Other methods or objects can interact with this object through methods that have
public access modifier. It must be done using ?.? operator.
Inheritance in C#:
In a few words, Inheritance is the process of creation new classes from already existing
classes. The inheritance feature allows us to reuse some parts of code. So, now we have some
derived class that inherits base class?s members. Consider the following code snippet:

public class Door : Aperture


{
public Door() : base()
{
}
public bool isOutside = true;
}

As you see to inherit one class from another, we need to write base class name after ?:?
symbol. Next thing that was done in code Door () ? constructor also inherits base class
constructor. And at last we add new private field. All members of Aperture class are also in Door
class. We can inherit all the members that has access modifier higher than protected.
Polymorphism in C#:
Polymorphism is possibility to change behavior with objects depending of object?s data type. In
C# polymorphism realizes through the using of keyword virtual and override. Let look on the
example of code:

public virtual void Out()


{
Console.WriteLine("Aperture virtual method called");
}
//This method is defined in Aperture class.
public override void Out()
{
Console.WriteLine("Door virtual method called");
}

Now we need to re-define it in our derived Door class. The usage of virtual methods can be
clarified when we creating an instance of derived class from the base class:
Aperture ap = new Door();
ap.Out();
In such cases, the runtime keeps record of all the virtual function details in a table called
VMT(Virtual Method Table) and then in runtime dynamically picks the correct version of the
function to be used. Here it uses Out() method from derived class of course.

.Net C# Tutorial Namespaces


A Namespace in Microsoft .Net is like containers of objects. They may contain unions, classes,
structures, interfaces, enumerators and delegates. Main goal of using namespace in .Net is for
creating a hierarchical organization of program. In this case a developer does not need to worry
about the naming conflicts of classes, functions, variables etc., inside a project.
In Microsoft .Net, every program is created with a default namespace. This default namespace
is called as global namespace. But the program itself can declare any number of namespaces,
each of them with a unique name. The advantage is that every namespace can contain any
number of classes, functions, variables and also namespaces etc., whose names are unique only
inside the namespace. The members with the same name can be created in some other
namespace without any compiler complaints from Microsoft .Net.

To declare namespace C# .Net has a reserved keyword namespace. If a new project is created
in Visual Studio .NET it automatically adds some global namespaces. These namespaces can be
different in different projects. But each of them should be placed under the base namespace
System. The names space must be added and used through the using operator, if used in a
different project.
Please now have a look at the example of declaring some namespace:

using System;
namespace OutNamespace
{
namespace WorkNamespace
{ /// can be placed some classes, structures etc.
}
}

In this example we create two namespaces. These namespaces have hierarchical structure.
We have some outer one named OutNamespace and the inner one called WorkNamespace. The
inner namespace is declared with a C# .Net class WorkItem.
The next logical discussion after a namespace is classes. A class is the basis of object ?
oriented programming. It encapsulates the data and methods into one itself and manipulates
them through the interaction with that object.

class WorkItem
{
public WorkItem()
{
}
static WorkItem()
{
m_counter = 1;
}
public static int GetCounter()
{
return m_counter;
}
private static int m_counter;
public virtual void Status()
{

}
internal bool IsWorking
{
get
{
return m_isWorking;
}
set
{
m_isWorking = value;
}
}
private bool m_isWorking = true;
}

The above sample contains the .Net namespace with the class WorkItem inside it.
As already discussed, a class is the basis of Object oriented programming. A class must have a
constructor. The constructor method is used for initialization purposes. Each class can have
different modifiers which pertains to the type and functionality of the class. Some such modifiers
are: new, public, protected, internal, private, abstract, and sealed. A class members can also
have these modifiers. In the above example, there is declared special constructor type ? static
constructor. It uses only for class not for its instances. In the same way we can access to the
static members of class.
OutNamespace.WorkNamespace.WorkItem item = new WorkItem();
int i = WorkItem.GetCounter();
In this piece of code there was created some instance of WorkItem but called using its full name
(including all namespace?s names). The second line it is an access to the public static property of
WorkItem. There are many advanced features that can be used in class constructing process.
One of them polymorphism that can be realized though the virtual methods.
.Net Framework basics
When we speak about .Net, we mean by .NET framework. .NET Framework is made up of the
Common Language Runtime (CLR), the Base Class Library (System Classes). This allows us to
build our own services (Web Services or Windows Services) and Web Applications (Web forms
Or Asp .Net), and Windows applications (Windows forms). We can see how this is all put
together.?

Above Picture shows overall picture, demonstrating how the .NET languages follows rules
provided by the Common Language Specifications (CLS). These languages can all be used?
Independently to build application and can all be used with built-in data describers (XML) and
data assessors (ADO .NET and SQL). Every component of the .NET Framework can take
advantage of the large pre- built library of classes called the Framework Class Library (FCL).
Once everything is put together, the code that is created is executed in the Common Language
Runtime. Common Language Runtime is designed to allow any .NET-compliant language to
execute its code. At the time of writing, these languages included VB .Net, C# and C++ .NET, but
any language can become .NET- compliant, if they follow CLS rules. The following sections will
address each of the parts of the architecture.
.Net Common Language Specifications (CLS):
In an object-oriented environment, everything is considered as an object. (This point is
explained in this article and the more advanced features are explained in other articles.) You
create a template for an object (this is called the class file), and this class file is used to create
multiple objects.
TIP: Consider a Rectangle. You may want to create many Rectangle in your lifetime; but each
Rectangle will have certain characteristics and certain functions. For example, each rectangle will
have a specific width and color. So now, suppose your friend also wants to create a Rectangle.
Why reinvent the Rectangle? You can create a common template and share it with others. They
create the Rectangle based on your template. This is the heart of object-oriented programming?
the template is the class file, and the Rectangle is the objects built from that class. Once you
have created an object, your object needs to communicate with many other Objects.
Even if it is created in another .NET language doesn?t matter, because each language follows
the rules of the CLS. The CLS defines the necessary things as common variable types (this is
called the Common Type System CTS ), common visibility like when and where can one see
these variables, common method specifications, and so on. It doesn?t have one rule which tells
how C# composes its objects and another rule tells how VB .Net does the same thing . To steal a
phrase, there is now ?One rule to bind them all.? One thing to note here is that the CLS simply
provides the bare rules. Languages can adhere to their own specification. In this case, the actual
compilers do not need to be as powerful as those that support the full CLS.
The Common Language Runtime (CLR):
The heart of .net Framework is Common Language Runtime (CLR). All .NET-compliant
languages run in a common, managed runtime execution environment. With the CLR, you can

rely on code that is accessed from different languages. This is a huge benefit. One coder can
write one module in C#, and another can access and use it from VB .Net. Automatic object
management, the .NET languages take care of memory issues automatically. These are the few
listed?benefits which you get from CLR.
Microsoft Intermediate Language (MSIL):
So how can many different languages be brought together and executed together??Microsoft
Intermediate Language (MSIL) or, as it?s more commonly known, Intermediate Language (IL). In
its simplest terms, IL is a programming language.?If you wanted to, you could write IL directly,
compile it, and run it. But why would want to write such low level code? Microsoft has provided
with higher-level languages, such as C#, that one can use. Before the code is executed, the MSIL
must be converted into platform-specific code. The CLR includes something called a JIT compiler
in which the compiler order is as follows.
Source Code => Compiler => Assembley =>Class Loader =>Jit Compiler =>Manged Native
Code=>Execution.
The above is the order of compilation and execution of programs. Once a program is written in a
.Net compliant language, the rest all is the responsibility of the frame work.

Methods and properties


Any class in an object-oriented language has method and property members. These are the
places where the actual business logic or functionality is written and executed. This tutorial
explains how to create and use methods and properties in C#.

C# Methods:
Method is object-oriented item of any language. All C# programs are constructed from a number
of classes and almost all the classes will contain methods. A class when instantiated is called an
object. Object-oriented concepts of programming say that the data members of each object
represent its state and methods represent the object behavior.

Method Signature in C#:


Each method is declared as follows:
Return-type methodname ( Parameterslist );
For better understanding of methods let consider following example. We have a class Man. It can
have many fields like that:
public class Man
{
public Man(){}
private int m_old;
private string m_name;
public string WhatIsYourName()
{
Console.WriteLine(m_name);

return m_name;
}
public string HowOldAreYou()
{
Console.WriteLine(m_old.ToString());
return m_old;
}
}
The private members m_old and m_name define some state of objects that can be created as
instances of our class. Also the class Man has two methods, which serve some of our requests.
Method string WhatIsYourName() writes current object?s name to the console and returns it, and
the second one similar to first return age of man and also writes an output to the console.
The return type in the example above returns strings, which is an in-built data type. The
methods can also return any generic C# type or any custom types created by us.
Passing Parameters to Methods in C#:
The input parameters can be passed in two ways.
Value type
Reference type.
If parameters are passed as value types a new copy of it will be created and passed inside the
function. If they are created as reference types, only the address of the parameters will be
passed.
See next example:
public int CalculateBirthYear(int year)
{
int b = year - m_old;
Console.WriteLine("Birth year is {0}",b);
return b;
}
If input parameter pass as reference type it must use keyword ref, in that way we operate with
the same cell in memory. That?s mean it can be changed inside any method. A small example for
a parameter passed by reference is:
public int CalculateBirthYear(ref int year)
{
int b = year - m_old;
Console.WriteLine("Birth year is {0}",b);
return b;
}
Now, the function CalculateBirthYear can even modify the value of year as it is passed by
reference.

Output Parameters in Methods:


The return values in any function will be enough for any one if only one value is needed. But in
case a function is required to return more than one value, then output parameters are the norm.

This is not supported in C++ though it can be achieved by using some programming tricks. In C#
the output parameter is declared with the keyword out before the data type. A typical example is
as follows.
public void CalculateBirthYear(ref int year, out int birthyear)
{
int b = year - m_old;
Console.WriteLine("Birth year is {0}",b);
birthyear = b;
return;
}
Strictly speaking there is no difference between ref and out parameters. The only difference is
that the ref input parameters need an input value and the out parameters don?t.

Variable arguments in C#:


The C# language supports variable arguments through a keyword called params. A typical
example for the declaration of a function with variable argument signature is as follows.
Public void functionName(int a, params int[] varParam);

Method Overloading in C#:


A method is considered to be an overloaded method, if it has two or more signatures for the same
method name. These methods will contain different parameters but the same return types.
A simple example for an overloaded methods are:
Public void functionName(int a, params int[] varParam);
Public void functionName(int a);

Property in C#:
Property ? it is a special method that can return a current object?s state or set it. Simple syntax of
properties can see in the following example:
public int Old
{
get {return m_old;}
set {m_old = value;}
}
public string Name
{
get {return m_name;}
}
Here are two types of properties. A first one can set or get field of class named m_old, and the
second is read only. That?s mean it can only get current object?s state.
The significance of these properties is its usability. These properties need not be called with any
function names like objectname.get or objectname.set etc., But they can be directly assigned the
values or retrieve the values.

C# .Net and Java


This article compares the same program written in the C# and Java languages and then
compares the dissembled code of both languages.
Java Hello Program:

class Hello
{
public static void main(String args[])
{
System.out.println("Hello");
}
}

Disassembled Java Hello Program:

class Hello
{
Hello()
{
// 0 0:aload_0
// 1 1:invokespecial #1 <Method void Object()>
// 2 4:return
}
public static void main(String args[])
{
System.out.println("Hello");
// 0 0:getstatic #2 <Field PrintStream System.out>
// 1 3:ldc1 #3 <String "Hello">
// 2 5:invokevirtual #4 <Method void PrintStream.println(String)>
// 3 8:return
}
}

Explanation of Java program:

To understand this you must have some knowledge of computer internals concepts for eg.
Opcodes, instruction templates etc. I assume that you already know them.
As usual this code will also start with main method. The first line tells that print ?Hello? it is a
normal print statement. A specific instruction, with type information, is built by replacing the ?T? in
the instruction template in the opcode column by the letter in the type column. In this case it is a
load instruction for type reference.
Invokespecial instruction must name an instance initialization method, a method in the current
class, or a method in a superclass of the current class. Class and interface initialization methods
are invoked implicitly by the Java virtual machine; they are never invoked directly from any Java
virtual machine instruction, but are invoked only indirectly as part of the class initialization
process.
invokevirtual or invokespecial is used to access a protected method of a superclass, then the type
of the class instance being accessed must be the same as or a subclass of the current class.
The Java virtual machine uses local variables to pass parameters on method invocation. On
class method invocation any parameters are passed in consecutive local variables starting from
local variable 0.
C# Hello Program:

using System;
class Hello
{
public static void Main(string[] args)
{
Console.WriteLine("Hello");
}
}

Disassembled C# Hello Program :

.method private hidebysig static void Main() cil managed


{
.entrypoint
// Code size 11 (0xb)
.maxstack 8
IL_0000: ldstr "Hello"
IL_0005: call void [mscorlib]System.Console::WriteLine(class System.String)
IL_000a: ret
} // end of method Hello::Main

Explanation of C# .Net Program:


The first line defines the Main method by using the .method MSIL keyword. Note that the
method is defined as being public and static, which are the default modifiers for the Main method.
Also note that this method is defined as managed.
The next line of code uses the MSIL .entrypoint keyword to designate this particular method as
the entry point to the application. When the .NET runtime executes this application, this is where
control will be passed to the program.
Next, look at the MSIL opcodes on lines IL_0000 and IL_0005. The first uses the the ldstr (Load
String) opcode to load a hard-coded literal ("Hello") onto the stack.
The next line of code calls the System.Console.WriteLine method. Notice that the MSIL prefixes
the method name with the name of the assembly that defines the method. This line also tells us
the number of arguments (and their types) that are expected by the method. Here, the method
will expect a System.String object to be on the stack when it's called. Finally, line IL_000a is a
simple ret MSIL opcode to return from the method

C# .Net Tutorial Attributes


This article deals with the new C# .net features named attributes. This can be used as a Self
paced C# .net training or C# tutorial material.
C# .net Attributes provide a powerful method of associating declarative information with C#
code. It can work with types, methods, properties and other language components.
As a thumb-rule, a class is said to be an attribute class, if it directly or indirectly derives from the
System.Attribute class.
Attributes in C# .net - Sample:
This C# tutorial on attributes uses custom attributes by defining an attribute class. The class
ProductInfo is derived from System.Attribute class. This class contains information about
application author and product version. The created class is:

[AttributeUsage(AttributeTargets.All)]
public class ProductInfo : System.Attribute
{
public ProductInfo(double version,string name)
{
m_version = version;
m_authorName = name;
}

public double Version


{
get { return m_version; }
}
private double m_version = 1.00;
public string AuthorName
{
get { return m_authorName; }
}
private string m_authorName;
}

There is a new item in the first line of code. It is an attribute that allows us to use a c# attribute
keyword AttributeUsage to all elements of our class (parameter AttributeTargets.All).
Now we can use these c# attributes in declaration of any other custom class:
[ProductInfo(1.005,"CoderSource"])
public class AnyClass { }
After creating simple custom type c# attributes, we can get information about all its attributes at
runtime. Such run-time information can be pulled out from a class by using the namespace
System.Reflection. In main function of our C# tutorial program we can get from our object all
information about custom attributes. Next piece of code demonstrate it:

MemberInfo membInfo;
membInfo = typeof(AnyClass);
object [] attributes;
attributes = membInfo.GetCustomAttributes(typeof(ProductInfo),true);
if(attributes.GetLength(0)!=0)
{
ProductInfo pr = (ProductInfo) attributes[0];
Console.WriteLine("Product author: {0}",pr.AuthorName);
Console.WriteLine("Product version; {0}",pr.Version);
}

Using standard methods of getting type information and members of his type we can get all
custom attributes.
The example in this C# tutorial uses only one of three library c# attributes (AttributeUsage). It
uses only with declaration some custom c# attribute. There is another standard attribute called
Conditional. It calls method with that attribute only in case when method application has some
needed condition. Most often it is used to define some preprocessor commands (?#?). And the
last reserved attribute is Obsolete. It helps us to mark some elements of program that are old to
use. These are basic things about attributes and their usage in applications.

C# .Net Tutorial Exceptions


This article is a basic c# tutorial dealing with exceptions in c# .net.
Practically any program including c# .net can have some amount of errors. They can be broadly
classified as compile-time errors and runtime errors. Compile-time errors are errors that can be
found during compilation process of source code. Most of them are syntax errors. Runtime errors
happen when program is running.
It is very difficult to find and debug the run-time errors. These errors also called exceptions.
Rules of good coding style say that program must be able to handle any runtime error. Exception
generates an exception call at runtime. Exceptions in C# can be called using two methods:

1. Using the throw operator. It call the manage code anyway and process an exception.
2. If using the operators goes awry, it can generate an exception.
Simple Exceptions - .Net C# Tutorial:
C# language uses many types of exceptions, which are defined in special classes. All of them
are inherited from base class named System.Exception. There are classes that process many
kinds of exceptions: out of memory exception, stack overflow exception, null reference exception,
index out of range exception, invalid cast exception, arithmetic exception etc. This c# tutorial
deals with DivideByZero c# exception and custom classes in c# exceptions.
C# has defined some keywords for processing exceptions. The most important are try, catch
and finally.
The first one to be known is the try operator. This is used in a part of code, where there exists a
possibility of exception to be thrown. But operator ?try? is always used with the operators: catch
and finally.
See the following example of handling a simple exception in c#.

//Sample code for C# Exception tutorial using try , catch


// try catch exception
int zero = 0;
try
{

int div = 100/zero;


}
catch(DivideByZeroException)
{
Console.WriteLine("Division by zero exception passed");
}

This code in runtime throws a DivideByZeroException and writes some message through the
console. But if you want to release some resources that were created you must use try ? finally
construction. Finally will be called even if there were no exceptions raised.

//Sample code for C# Exception tutorial using try, finally


Bitmap bit = null;
// try finally exception
try
{
bit = new Bitmap(100,100);
}
finally
{
bit.Dispose();
Console.WriteLine("bitmap is disposed");
}

In the similar way we can use try ? catch ? finally construction. The attached c# tutorial program
contains sample including all the three.
Custom Exception Classes - C# Tutorial:
Some larger projects might have a requirement of creating their own custom exception classes.
Let us try to create class that validates email address. It will validate for the ?@? symbol. Please
have a look on the following piece of code:

//Sample code for C# .Net Exception tutorial - validates an email address


public class TextException : Exception
{

public TextException() : base()<br>


{
}
public TextException(string message) : base(message)
{
}
}
public class MailValidator
{
MailValidator()
{
}
private static char symbol = '@';
public static void TestEnteredMail(string mailAddress)
{
if(mailAddress.IndexOf(symbol)==-1)
{
Console.WriteLine("The string entered is not a valid email address");<br>
throw(new TextException());
}
}
}

Here were created a C# .Net TextException class that inherits from System.Exception class
of .NET class library. Actually it does nothing, but there is an additional class MailValidator. It has
TestEnteredMail method that raises a TextException. Now look at usage of it in Main function.

try
{
MailValidator.TestEnteredMail(Console.ReadLine());
}
catch(TextException)
{

Console.WriteLine("Exception was passed");


}

So, if user enters mail address without it throws an exception. All the sources are attached and
compliable. The example can be compiled using .NET command line through the csc command
line program. You only need to type csc filename.cs.

C# .Net Tutorial Interfaces


This C# Tutorial deals with interfaces in C# .Net. An Interface is a reference type and it
contains only abstract members. Interface's members can be Events, Methods, Properties and
Indexers. But the interface contains only declaration for its members. Any implementation must
be placed in class that realizes them. The interface can't contain constants, data fields,
constructors, destructors and static members. All the member declarations inside interface are
implicitly public.
Defining an Interface:
Let us look at a simple example for c# interfaces. In this example interface declares base
functionality of node object.
interface INode
{
string Text
{
get;
set;
}
object Tag
{
get;
set;
}
int Height
{
get;
set;
}
int Width
{
get;
set;

}
float CalculateArea();
}
The above INode interface has declared a few abstract properties and function which should be
implemented by the derived classes.
//Sample for Deriving a class using a C# .Net interface - c# tutorial
public class Node : INode
{
public Node()
{}
public string Text
{
get
{
return m_text;
}
set
{
m_text = value;
}
}
private string m_text;
public object Tag
{
get
{
return m_tag;
}
set
{
m_tag = value;
}
}
private object m_tag = null;
public int Height
{

get
{
return m_height;
}
set
{
m_height = value;
}
}
private int m_height = 0;
public int Width
{
get
{
return m_width;
}
set
{
m_width = value;
}
}
private int m_width = 0;
public float CalculateArea()
{
if((m_width<0)||(m_height<0))
return 0;
return m_height*m_width;
}
}
Now the above code has created a c# class Node that inherits from INode c# interface and
implement all its members. A very important point to be remembered about c# interfaces is, if
some interface is inherited, the program must implement all its declared members. Otherwise the
c# compiler throws an error.
The above code was a simple example of c# interface usage. Now this has to be followed with
some advanced details of interface building in C# .Net. The previous example used only names
of methods or properties that have the same names as in interface. But there is another

alternative method for writing the implementation for the members in class. It uses full method or
property name e.g. INode.CalculateArea () {// implemetation}.
Multiple Inheritance using C# interfaces:
Next feature that obviously needs to be explained is multiple inheritance using c# interfaces.
This can be done using child class that inherits from any number of c# interfaces. The inheritance
can also happen with a combination of a C# .Net class and c# interfaces. Now let us see a small
piece of code that demonstrate us multiple inheritance using only interfaces as parent data types.
class ClonableNode : INode,ICloneable
{
public object Clone()
{
return null;
}
// INode members
}
The above example created a class ClonableNode. It implements all the functionality of INode
interface in the same way as it was done in Node class. Also it realizes Clone method only one
item of IClonable interface of .NET library.
is Operator for C# .Net interfaces - C# Tutorial:
At last a new C# operator that can be used to define that class should be explained. It is the is
operator. Look at the following piece of code:

if(nodeC is INode)
Console.WriteLine("nodeC is object of INode type");
else
Console.WriteLine("nodeC isn't object of INode type");
In example nodeC object was created as ClonableNode type, but when we run program "if
operator" returns true. It means that nodeC also is of INode type.

C# .Net Tutorial Multithreading


Any Windows application must have one or more processes. A Process is structural unit with a
memory block and using some set of resources. For each executable, the Windows operating
system creates some isolated memory block. This C# .Net Tutorial tries to explain the basics of
Multithreading in C# .Net.
Every process must have at least one thread. The first thread is created with a process and is
known as primary thread. This Primary Thread is entry point of application. In traditional Windows
applications it is the method WinMain() and in console applications it is named main().

Main goal of creating multithreading application is performance improvement. As an example,


imagine a situation where in a user starts a long process (e.g. copying), he can?t use a single
threaded application and wait for an infinite time for the operation to get completed. But if he uses
multi?threading application he can set copying process in the background and interact with
application without any problems.
At first, if one wants to create a multi-threaded application an important point to be remembered
is, a global variable, which is being accessed by different threads, can try to modify the same
variable. This is a generic problem, which is solved using a mechanism called Synchronization of
threads. Synchronization is nothing but the process of creating some set of rules to operate data
or resources.
The C# .Net language has a powerful namespace which can be used for programming with
Threads as well as Thread Synchronization in C# .Net programming. The name of the
namespace is Sytem.Threading. The most important class inside this namespace for
manipulating the threads is the C# .Net class Thread. It can run other thread in our application
process.
Sample program on C# Multithreading - C# Tutorial:
The example it creates an additional C# .Net class Launcher. It has only one method, which
output countdown in the console.

//Sample for C# tutorial on Multithreading using lock


public void Coundown()
{
lock(this)
{
for(int i=4;i>=0;i--)
{
Console.WriteLine("{0} seconds to start",i);
}
Console.WriteLine("GO!!!!!");
}
}

There is a new keyword lock inside the above chunk of .Net C# tutorial code. This provides a
mechanism for synchronizing the thread operation. It means at the same point of time only one

thread can access to this method of created object. Unless the lock is released after completion
of the code, the next routine or iteration cannot enter the block.
To understand it more clearly please have a look at the piece of main method?s code:

Launcher la = new Launcher();


Thread firstThread = new Thread(new ThreadStart(la.Coundown));
Thread secondThread =new Thread(new ThreadStart(la.Coundown));
Thread thirdThread = new Thread(new ThreadStart(la.Coundown));
firstThread.Start();
secondThread.Start();
thirdThread.Start();

As you see there were created three additional threads. These threads start a method of object
that has Launcher type. The above program is a very simple example of using multi-threading in
C#. Net. But C# .Net allows us to create more powerful applications with any level of complexity.

C# .Net Tutorial Reflection


Reflection is the feature in .Net, which enables us to get some information about object in
runtime. That information contains data of the class. Also it can get the names of the methods
that are inside the class and constructors of that object.
To write a C# .Net program which uses reflection, the program should use the namespace
System.Reflection. To get type of the object, the typeof operator can be used. There is one more
method GetType(). This also can be used for retrieving the type information of a class. The
Operator typeof allow us to get class name of our object and GetType() method uses to get data
about object?s type. This C# tutorial on reflection explains this feature with a sample class.

public class TestDataType


{
public TestDataType()
{
counter = 1;
}
public TestDataType(int c)
{
counter = c;
}

private int counter;


public int Inc()
{
return counter++;
}
public int Dec()
{
return counter--;
}
}

At first we should get type of object that was created. The following C# .Net code snippet shows
how to do it.
TestDataType testObject = new TestDataType(15);
Type objectType = testObject.GetType();

Now objectType has all the required information about class TestDataType. We can check if our
class is abstract or if it is a class. The System.Type contains a few properties to retrieve the type
of the class: IsAbstract, IsClass. These functions return a Boolean value if the object is abstract
or of class type. Also there are some methods that return information about constructors and
methods that belong to the current type (class). It can be done in a way as it was done in next
example:

Type objectType = testObject.GetType();


ConstructorInfo [] info = objectType.GetConstructors();
MethodInfo [] methods = objectType.GetMethods();
// get all the constructors
Console.WriteLine("Constructors:");
foreach( ConstructorInfo cf in info )
{
Console.WriteLine(cf);
}
Console.WriteLine();
// get all the methods
Console.WriteLine("Methods:");
foreach( MethodInfo mf in methods )
{
Console.WriteLine(mf);
}

Now, the above program returns a list of methods and constructors of TestDataType class.
Reflection is a very powerful feature that any programming language would like to provide,
because it allows us to get some information about objects in runtime. It can be used in the
applications normally but this is provided for doing some advanced programming. This might be
for runtime code generation (It goes through creating, compilation and execution of source code
in runtime).
The Sample code can be downloaded from here. The attached example can be compiled
using .NET command line csc command. Type csc Reflection.cs. This will create the executable
Reflection.exe, which can be run as a normal executable.

COM Interop
The ultimate goal of COM Interop is to provide access to the existing COM components
without requiring that the original component be modified. This tries to make the .NET
types equivalent to the COM Types.

COM Interop and Marshalling:


COM had a mechanism of marshalling and un-marshalling to convert between the source
and target data types. This is now totally covered in COM Interop using RCW or Runtime
Callable Wrapper. This automatically converts the .Net data types to the corresponding
COM data types.

RegAsm and tlbimp in COM Interop


In addition, COM Interop allows COM developers to access managed objects as easily as
they access other COM objects. It provides a specialized utility (RegAsm.exe) that
exports the managed types into a type library and registers the managed component as a
traditional COM component. At run time, the common language runtime marshals data
between COM objects and managed objects as needed.
This tutorial shows how C# can use COM objects to develop applications. First thing to
be done is the creation of wrapper class for the selected COM object. It can be done
manually through the command line application called TlbImp.exe (Type Library
Importer). This utility converts COM type library to the .NET Framework metadata. This
procedure can be done automatically through the .NET environment. We just need to add
reference to the COM object to our C# project. So, type in .NET command line next
string: tlbimp $WindowsPath$\system32\quartz.dll /out: quartzNET.dll. This command
will create a new dll with types that are compatible with any of Managed .NET
languages. Now we can add this dll to our C# project or compile our C# file with
additional feature "/r quartzNET.dll".
The following is an example of usage this COM object in C# managed code:

quartzNET.FilgraphManager manager = new quartzNET.FilgraphManagerClass();


quartzNET.IMediaControl mc = (quartzNET.IMediaControl)manager;
mc.RenderFile(args[0]);
mc.Run();
// Wait for completion.
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
Here is the MediaControl object, which was created in COM. This application gets a
name of video file that we want to play from command line and shows it. So, this is a
simple example of usage COM Interop. To compile an attached example we just need this
quartzNET.dll (is attached too) and .NET command line. Type here next command csc
InteropSample.cs /r:quartzNET.dll. It must create an executable file, but it can be run
using command line, just type InteroPsample.exe some.avi. So, it opens a console
application and also runs a standard Windows media player control to play the video.
.Net C# Delegates and Events
This tutorial describes some basics about some of the great features of C# language namely
Delegates and Events. These new constructs are used in object-oriented programming
languages like C# and Java.
Delegates in C# .Net:
If we look at C++ there is a feature called callback function. This feature uses Pointers to
Functions to pass them as parameters to other functions. Delegate is a similar feature but it is
more type safe, which stands as a stark contrast with C++ function pointers. A delegate can hold
reference/s to one more more functions and invoke them as and when needed.
A delegate needs the method's name and its parameters (input and output variables) when we
create a delegate. But delegate is not a standalone construction. it's a class. Any delegate is
inherited from base delegate class of .NET class library when it is declared. This can be from
either of the two classes from System.Delegate or System.MulticastDelegate.
If the delegate contains a return type of void, then it is automatically aliased to the type of
System.MulticastDelegate. This can support multiple functions with a += operator. If the delegate
contains a non-void return type then it is aliased to System.Delegate class and it cannot support
multiple methods.
Let us have a look at the following sample code.
class Figure
{
public Figure(float a, float b, float c)
{
m_xPos = a;
m_yPos = b;
m_zPos = c;
}
public void InvertX()

{
m_xPos = - m_xPos;
}
public void InvertY()
{
m_yPos = - m_yPos;
}
public void InvertZ()
{
m_zPos = - m_zPos;
}
private float m_xPos = 0;
private float m_yPos = 0;
private float m_zPos = 0;
}
Now, we have a class named Figure and it has three private fields that use to store position and
three methods to invert this position by every axis. In main class we declare delegate as follows:
public delegate void FigureDelegate();
And now in the main function we should use it like this:
Figure figure = new Figure(10,20,30);
FigureDelegate fx = new FigureDelegate(figure.InvertX);
FigureDelegate fy = new FigureDelegate(figure.InvertY);
FigureDelegate fz = new FigureDelegate(figure.InvertZ);
MulticastDelegate f_del = fx+fy+fz;
In this example we create three delegates of FigureDelegate type and attach to these elements
our three methods from Figure class. Now every delegate keeps the address of the attached
function. The last line of code is very interesting, here we create a delegate of base type
(MulticastDelegate) and attach three of our already created delegates. As all our methods are of
void return type they are automatically of type MutlticastDelegate and a MulticastDelegate can
support multiple methods invocation also. Hence we can write
Figure figure = new Figure(10,20,30);
FigureDelegate fMulti = new FigureDelegate(figure.InvertX);
fMulti += new FigureDelegate(figure.InvertY);
fMulti();

Events in C# .Net:
Delegate usefulness does not just lie in the fact that it can hold the references to functions but in
the fact that it can define and use function names at runtime and not at compile time. A large goal
of design delegates is their applicability in events model of .Net. Events are the actions of the
system on user manipulations (e.g. mouse clicks, key press, timer etc.) or any event triggered by

the program. To understand the usage of delegates for event model, the previous examples are
used here. We should add to our Figure class next things:
public delegate void FigureHandler(string msg);
public static event FigureHandler Inverted;
public void InvertZ()
{
m_zPos = - m_zPos;
Inverted("inverted by z-axis");
}
Now we have a delegate declared and event that uses this delegate's type. In every function we
should call our event. The next code snippet should explain it clearly:
static void Main(string[] args)
{
Figure figure = new Figure(10,20,30);
Figure.Inverted+=new Test.Figure.FigureHandler(OnFigureInverted);
figure.InvertX();
figure.InvertZ();
}
private static void OnFigureInverted(string msg)
{
Console.WriteLine("Figure was {0}",msg);
}
So, in the main function we should create an object of figure class and attach event handler to
the method OnFigureInverted. And when we call any of invert methods the event is fired and it
calls our event handler. The application will print the following string into the console: Figure was
inverted by x-axis Figure was inverted by z-axis There was simple examples of using delegates
and events and should be treated as a starting point to learn it more yourself. Download the C#
Delegates source files from the link. To compile and run it need to run .NET command line. Just
type: csc TestClass.cs. It creates TestClass.exe that can be run as standard executable file.
C# Method Overloading
Last Update: 9 February, 2005
In complex applications written in C#, we may need many methods which do essentially similar
functions but are just different enough to be considered unique. For example, we may have to
calculate a person's tax liability and would need to implement a method for doing this calculation
in our application program. However, there are many different rules when it comes to tax
calculations and they vary throughout the world. While there may be many rules, one basic
equation stays the same: Your net income equals your gross income minus a computed tax
amount. It is the method of computing your tax that varies.
We would probably have to implement different methods for each type of tax calculation. And, we
could give each method a unique name such as TaxCalc1, TaxCalc2, TaxCalc3, etc. But

wouldn't it be nice to just name the method TaxCalc and pass different arguments to it based on
the computation desired?
For instance, let's say you live in a region within your country where you are taxed on your
personal income, the value of your home, and any income you generate by doing business. On
sales you generate through business activity, you must pay a gross receipts tax on all sales. If
you own your home, you must pay a property tax on the imputed value of it. Then lastly, you must
pay a tax on all income you generate through a job with another employer.
An Example: Tax Calculation
For this article, we will use a hypothetical example of computing various taxes on a person's
property, sales, and income. Let's summarize the logical flow through pseudocode:
HV = Home_Value
HR = Home_Tax_Rate
GS = Gross_Sales
GR = Gross_Sales_Tax_Rate
PI = Personal Income
If YOU_OWN_YOUR_OWN_HOME and DO_NOT_OWN_A_BUSINESS THEN
Tax = TaxCalc(HV,HR) ' Calculate tax on the home's value
ELSE
If YOU_DO_NOT_OWN_YOUR_OWN_HOME and OWN_A_BUSINESS THEN
Tax = TaxCalc(GS,GR) ' Calculate tax on your gross receipts from sales
ELSE
If YOU_OWN_YOUR_OWN_HOME and OWN_A_BUSINESS THEN
Tax = TaxCalc(HV,HR,GS,GR) ' Calculate tax on both your home and gross receipts
ENDIF
ENDIF
ENDIF
Tax = Tax + TaxCalc(PI) ' And everyone gets a tax calculation on personal income
The pseudo code says that if you own your own home but do not have a business, then you will
calculate your tax based on the home's value and the tax rate on a home. If you don't own a
home but you own a business then you will calculate your tax based on your gross sales and
gross sales tax rate. Finally, if you own a home and a business then you will call TaxCalc and
pass all four values. Everyone is run through the TaxCalc method for personal income tax. We
don't have to pass a rate for personal income tax calculation because everyone is at a fixed rate
which is embedded within the method.
Keep in mind that this example of tax calculation is all hypothetical. Tax codes and calculations,
as you know, are much more complex than what is described here throughout the world. But for
the sake of this example, we see that we have a method named TaxCalc that is able to take one,
two, or four arguments based upon the type of tax we are calculating. Without method
overloading, we would have to create a unique method name for each type of calculation we
would have to do. We would probably name each TaxCalc1, TaxCalc2, and TaxCalc3 respectively.
How does C# know which method to call? It's easy. It knows which method to invoke based on
the number and type of arguments passed to it. This is also referred to as the signature of the
method. If C# sees you are calling TaxCalc with four arguments, then it will call that method with
four receiving arguments. The same is true for the other two methods as well.

The Overloaded Methods


Each method is defined as follows in our overloading example:
// This method takes for arguments: two amounts and two rates
public static double TaxCalc(double pamt1, double prate1, double pamt2,
double prate2)
{
double taxamt;
Console.WriteLine("Using method with 4 arguments");
taxamt = (pamt1 * prate1) + (pamt2 * prate2);
return taxamt;
} // *** TaxCalc ***
// This method only takes two arguments: an amount and a rate
public static double TaxCalc(double pamt1, double prate1)
{
double taxamt;
Console.WriteLine("Using method with 2 arguments");
taxamt = pamt1 * prate1;
return taxamt;
} // *** TaxCalc ***
// This method only takes one argument: an amount
public static double TaxCalc(double pamt)
{
double taxrate = 0.15;
double taxamt = 0;
Console.WriteLine("Using method with 1 argument");
taxamt = pamt * taxrate;
return taxamt;
} // *** TaxCalc ***
The methods are all very similar however they are differ by the number of arguments used in the
tax calculation. If we have two tax rates and two amounts, C# would pick the first method based
on four arguments and invoke that. The same holds true for the other two methods.
Caveat
It is important to remember that C# determines which method to call based upon the method's
signature. If you were to define two methods with the same name and the same number and type
of passed arguments, you would get a compile-time error in Visual Studio .NET of:
Class1.cs(53): Class 'MethodOverload.MainClass' already defines a member called
'TaxCalc' with the same parameter types
However, you can have two methods with the same name and the same number of arguments as
long as the argument types differ. Let's say, for instance, that you want to have another oneargument method named TaxCalc that takes a string argument. If the string is the literal
"TaxTable1", then it will return the tax rate used in personal tax calculations:

// This method only takes one argument as well but it differs


// from the above in the argument type.
public static double TaxCalc(string whichtable)
{
double taxrate = 0;
Console.WriteLine("Calling the method with on string argument");
if (whichtable == "TaxTable1")
taxrate = 0.15;
return taxrate;
} // *** TaxCalc ***
The Example Program: MethodOverLoad.exe
This article has an example application available for download with the above methods. It is a
C#.NET console application and can be invoked from a Dos command window by typing in
methodoverload in the folder containing methodoverload.exe. The main driver implements the
pseudocode we defined previously:
static void Main(string[] args)
{
bool ownshome = false;
bool ownsbusiness = false;
string inputs;
double hr = 0;
double hv = 0;
double gr = 0;
double gs = 0;
double pi = 0;
double totaltax = 0;
double pitaxrate = 0;
Console.WriteLine("Do you own a home? (y/n)");
inputs = Console.ReadLine();
if (inputs == "y")
{
ownshome = true;
Console.WriteLine("What is its value?");
inputs = Console.ReadLine();
hv = Convert.ToDouble(inputs);
Console.WriteLine("What is the home tax rate?");
inputs = Console.ReadLine();
hr = Convert.ToDouble(inputs);
}
Console.WriteLine("Do you own a business? (y/n)");
inputs = Console.ReadLine();
if (inputs == "y")
{
ownsbusiness = true;
Console.WriteLine("What was your total sales?");

inputs = Console.ReadLine();
gs = Convert.ToDouble(inputs);
Console.WriteLine("What is the gross sales tax rate?");
inputs = Console.ReadLine();
gr = Convert.ToDouble(inputs);
}
if (ownshome && !ownsbusiness)
totaltax = TaxCalc(hv, hr);
else
if (!ownshome && ownsbusiness)
totaltax = TaxCalc(gs, gr);
else
if (ownshome && ownsbusiness)
totaltax = TaxCalc(hv, hr, gs, gr);
Console.WriteLine("How much did you make last year?");
inputs = Console.ReadLine();
pi = Convert.ToDouble(inputs);
totaltax = totaltax + TaxCalc(pi);
Console.WriteLine("Your total tax is {0}", totaltax);
pitaxrate = TaxCalc("TaxTable1");
Console.WriteLine("Personal tax rate is {0}", pitaxrate);
} // *** Main ***

Conclusion
Method overloading is a powerful concept in C# in that it helps to simplify code reusability and
clarity. If our example method of TaxCalc was placed in a .dll file somewhere, I would only have to
remember that I have to call TaxCalc and only fill in the appropriate arguments to pass. Without
method overloading, I would have to try and remember which specific uniquely-named method to
call. This would present a problem if you do not code everyday using a particular method in that
time would be spent getting familiar with the methods all over.

Reflection in C#
This article is aimed to explain reflection in .NET Framework.
Reflection is one of the features of .Net framework and has greater importance during the
development of large applications. In brief it is a powerful way of collecting and
manipulate information present in application's assemblies and its metadata. Metadata
contain all the Type information used by the application. The ability to obtain
information at runtime also makes it even more advantageous. When reflection is used
along with system.type, it allows the developer to get the valuable information about all
the types and about the assemblies. We can even create the instances and then invoke
various types that are used across the application.

What is Reflection?
Reflection is the ability to find out information about objects, the application details
(assemblies), its metadata at run-time.
This allows application to collect information about itself and also manipulate on itself.
It can be used effectively to find all the types in an assembly and/or dynamically invoke
methods in an assembly. This includes information about the type, properties, methods
and events of an object and to invoke the methods of object Invoke method can be used
too. With reflection we can dynamically create an instance of a type, bind the type to an
existing object, or get the type from an existing object and invoke its methods or access
its fields and properties. If Attributes (C#) are used in application, then with help of
reflection we can access these attributes. It can be even used to emit Intermediate
Language code dynamically so that the generated code can be executed directly.
How to use Reflection in our applications?
System.Reflection namespace contains all the Reflection related classes. These classes
are used to get information from any of the class under .NET framework. The Type class
is the root of all reflection operations. Type is an abstract base class that acts as means to
access metadata though the reflection classes. Using Type object, any information related
to methods, implementation details and manipulating information can be obtained. The
types include the constructors, methods, fields, properties, and events of a class, along
with this the module and the assembly in which these information are present can be
accessed and manipulated easily.
As mentioned earlier, we can use reflection to dynamically create an instance of any type,
bind the type to an existing object, or get the type from an existing object. Once this is
done appropriate method can be invoked, access the fields and properties. This can be
done by specifying the Type of object or by specifying both assembly and Type of the
object that needs to be created. By this the new object created acts like any other object and
associated methods, fields and properties can be easily accessed. With reflection we can
also find out about various methods associated with newly created object and how to use
these object. To find out the attributes and methods associated with an object we can use
the abstract class MemberInfo, this class is available under the namespace
System.Reflection.

Understanding Constructors in C#
Constructors are used for initializing the members of a class whenever an object is
created with the default values for initialization.
If a class is not defined with the constructor then the CLR (Common Language Runtime) will
provide an implicit constructor which is called as Default Constructor.

A class can have any number of constructors provided they vary with the number of
arguments that are passed, which is they should have different signatures.

Constructors do not return a value.


Constructors can be overloaded.

If a class is defined with static and Non-static constructors then the privilege will
be given to the Non-static constructors.

The following are the access modifiers for constructors,


Public : A constructor that is defined as public will be called whenever a class is
instantiated.
Protected : A constructor is defined as protected in such cases where the base class will
initialize on its own whenever derived types of it are created.
Private : A constructor is defined as private in such cases whenever a class which
contains only static members has to be accessed will avoid the creation of the object for
the class.
Internal : An internal constructor can be used to limit concrete implementations of the
abstract class to the assembly defining the class. A class containing an internal
constructor cannot be instantiated outside of the assembly.
External : When a constructor is declared using an extern modifier, the constructor is
said to be an external constructor.

Types of Constructors
i) Static : Used for initializing only the static members of the class. These will be
invoked for the very first time the class is being loaded on the memory. They cannot
accept any arguments. Static Constructors cannot have any access modifiers.
Syntax ---Static ClassName()
{
//Initialization statements;
}
ii) Non-Static : are used for initializing the Non-Static and Static members of a class.
These will be invoked everytime a new object is defined for a class.

Syntax --Public ClassName([argsInfo])


{
//Initialization statements;
}
Example :
Using System;
Class SampleConstructor
{
public static int s;
public int ns;
static SampleConstructor()
{
s = 10;
//ns = 20; --- Error cannot be assigned like this
}
public SampleConstructor()
{
ns=100;
s=200;
}
}
Class UseSampleConstructor
{
public static void Main()
{

SampleConstructor sc = new SampleConstructor();


Console.WriteLine({0},{1},sc.s, SampleConstructor.s, sc.ns);
}l
}l
Error(Cannot call like this)
If you observe in the above example the static variable `ns' cannot be assigned a value in
the static Constructor as it is a Non-static member of the class and a static constructor
cannot initialize a Non-Static member.
Also as you see in the above code the Non-static constructor initializing the values for
both the static and Non-Static members of class, when we say `sc' we are creating an
object for the class and using the object we do the call the methods, it is not valid to call a
static member of class using the object of the class we have to call it through the method
name/Constructor name through the member will be invoked.

Das könnte Ihnen auch gefallen