Sie sind auf Seite 1von 7

A class is a construct that enables you to create your own custom types by grouping

together variables of other types, Fields,methods and events. A class is like a blueprint.
It defines the data and behavior of a type.

The following are class members:


Fields :
Fields store the data a class needs to fulfill its design. For example, a class representing a calendar
date might have three integer fields: one for the month, one for the day, and one for the year.
Fields are declared within the class block by specifying the access level of the field, followed by the
type of the field, followed by the name of the field. For example:
C#

public class CalendarDate


{
public int month;
public int day;
public int year;
}
Accessing a field in an object is done by adding a period after the object name, followed by the
name of the field, as in objectname.fieldname. For example:
C#

CalendarDate birthday = new CalendarDate();


birthday.month = 7;
A field can be given an initial value by using the assignment operator when the field is declared. To
automatically assign the month field to 7, for example, you would declare month like this:
C#

public class CalendarDateWithInitialization


{
public int month = 7;
//...
}
Fields are initialized immediately before the constructor for the object instance is called, so if the
constructor assigns the value of a field, it will overwrite any value given during field declaration.
Fields can be marked as public, private, protected, internal, or protected internal. These access
modifiers define how users of the class can access the fields.
A field can optionally be declared static. This makes the field available to callers at any time, even
if no instance of the class exists..
A field can be declared readonly. A read-only field can only be assigned a value during initialization
or in a constructor. A staticreadonly field is very similar to a constant, except that the C#
compiler does not have access to the value of a static read-only field at compile time, only at run
time.

Method:
A method is a code block containing a series of statements. In C#, every executed
instruction is done so in the context of a method.
Methods are declared within a class or struct by specifying the access level, the return
value, the name of the method, and any method parameters. Method parameters are
surrounded by parentheses, and separated by commas. Empty parentheses indicate that
the method requires no parameters. This class contains three methods:
C#
class Motorcycle
{
public void StartEngine() { }
public void AddGas(int gallons) { }
public int Drive(int miles, int speed) { return 0; }
}
Calling a method on an object is similar to accessing a field. After the object name, add a
period, the name of the method, and parentheses. Arguments are listed within the
parentheses, and separated by commas. The methods of the Motorcycle class can
therefore be called like this:
C#
Motorcycle moto = new Motorcycle();
moto.StartEngine();
moto.AddGas(15);
moto.Drive(5, 20);
Method Parameters
As shown in the previous code snippet, passing arguments to a method is simply a
matter of providing them in the parentheses when calling a method. To the method
being called, the incoming arguments are called parameters.
The parameters a method receives are also provided in a set of parentheses, but the
type and a name for each parameter must be specified. The name does not have to be
the same as the argument. For example:
C#
public static void PassesInteger()
{
int fortyFour = 44;
TakesInteger(fortyFour);
}
static void TakesInteger(int i)
{
i = 33;
}
Here a method called PassesInteger passes an argument to a method
called TakesInteger. Within PassesInteger, the argument is named fortyFour, but

inTakeInteger, this is a parameter named i. This parameter exists only within


the TakesInteger method. Any number of other variables can be named i, and they can
be of any type, so long as they are not parameters or variables declared inside this
method.
Notice that TakesInteger assigns a new value to the provided argument. One might
expect this change to be reflected in the PassesInteger method onceTakeInteger returns,
but in fact the value in the variable fortyFour remains unchanged. This is
because int is a value type. By default, when a value type is passed to a method, a copy
is passed instead of the object itself. Because they are copies, any changes made to the
parameter have no effect within the calling method. Value types get their name from the
fact that a copy of the object is passed instead of the object itself. The value is passed,
but not the same object.
For more information on passing value types, see Passing Value-Type Parameters (C#
Programming Guide). For a list of value types integral to C#, see Value Types Table (C#
Reference).
This differs from reference types, which are passed by reference. When an object based
on a reference type is passed to a method, no copy of the object is made. Instead, a
reference to the object being used as a method argument is made and passed. Changes
made through this reference will therefore be reflected in the calling method. A reference
type is created with the class keyword, like this:
C#
public class SampleRefType
{
public int value;
}
Now, if an object based on this type is passed to a method, it will be passed by
reference. For example:
C#
public static void TestRefType()
{
SampleRefType rt = new SampleRefType();
rt.value = 44;
ModifyObject(rt);
System.Console.WriteLine(rt.value);
}
static void ModifyObject(SampleRefType obj)
{
obj.value = 33;
}
This example essentially does the same thing as the previous example. But, because a
reference type is used, the modification made by ModifyObject is made to the object
created in the TestRefType method. The TestRefType method will therefore display the
value 33.
For more information, see Passing Reference-Type Parameters (C# Programming
Guide) and Reference Types (C# Reference).
Return Values
Methods can return a value to the caller. If the return type, the type listed before the
method name, is not void, then the method can return the value using

the returnkeyword. A statement with the keyword return followed by a value that
matches the return type will return that value to the method caller. The return keyword
also stops the execution of the method. If the return type is void, a return statement
with no value is still useful to stop the execution of the method. Without
the return keyword, the method will stop executing when it reaches the end of the code
block. Methods with a non-void return type are required to use the return keyword to
return a value. For example, these two methods use the return keyword to return
integers:
C#
class SimpleMath
{
public int AddTwoNumbers(int number1, int number2)
{
return number1 + number2;
}
public int SquareANumber(int number)
{
return number * number;
}
}
To use a value returned from a method, the calling method can use the method call itself
anywhere a value of the same type would suffice. You can also assign the return value to
a variable. For example, the following two code examples accomplish the same goal:
C#
int result = obj.AddTwoNumbers(1, 2);
obj.SquareANumber(result);
C#
obj.SquareANumber(obj.AddTwoNumbers(1, 2));

Constructor
It is a Member function, which can be called automatically while creation of Object. It is
used to Control the Behavior of Object.
It is also used for automatic initialization of Objects
Types of Constructors
=>Class Constructor (static)
=>Instance Constructor (non static)
Class Constructor
It is method, which can be called automatically. It can initialize the Class Members. It will
be the first call in a Class. It can be executed only once.
It can be called by Class Loader of CLR.
It does not allow parameters. So that it can't be overloaded.
syntax : -(Class Constructor)

static ClassName()
{
------------------}
Instance Constructor
It is a Method, which can be called while initialization of Object. It can initialize the
nstance Members. It can be called as many times as we initialize the Class.
It allows parameters, So that it can be overloaded.
It should be public and same as Class Name and No Return Type.
Syntax :- (Instance Constructor)
public ClassName(parameters)
{
----------------}
How to call a Constructor
new ClassName();
or
new ClassName(arg1,arg2,----);
Indexers
It is a Sub Program, It used to Access an Object of a class as an Array.
It consists of Two Accessors
=>get Accessor
=>set Accessor
If an Indexer consists of only get Accessor, it is called as Read-Only Indexer
If an Indexer consists of only set accessory, it is called Write Only Indexer
syntax : AccessSpecifier ReturnType this[DataType Index]
{
set
{
}
get
{
}
}

Properties
It
It
It
A

is a Sub Program, It used to set or get a Value to the Private Member.


is used to Control the Behavior of given Data Member.
behaves as function, but we can access it like a Variable.
Property consists of Two Accessors

=>get Accessor
=>set Accessor
If a Property contains only get accessor, then it is called as a Read Only Property

If a Property contains only set accessor, then it is called a Write Only Property.

syntax : AccessSpecifier ReturnType PropertyName


{
set
{
}
get
{
}
}

Static
Use the static modifier to declare a static member, which belongs to the type itself
rather than to a specific object. The static modifier can be used with classes, fields,
methods, properties, operators, events, and constructors, but it cannot be used with
indexers, destructors, or types other than classes. For more information, see Static
Classes and Static Class Members (C# Programming Guide).
Example
The following class is declared as static and contains only static methods:
C#
static class CompanyEmployee
{
public static void DoSomething() { /*...*/ }
public static void DoSomethingElse() { /*...*/ }
}
A constant or type declaration is implicitly a static member.
A static member cannot be referenced through an instance. Instead, it is referenced
through the type name. For example, consider the following class:
C#
public class MyBaseC
{
public struct MyStruct
{
public static int x = 100;
}
}
To refer to the static member x, use the fully qualified name, MyBaseC.MyStruct.x,
unless the member is accessible from the same scope:
C#
Console.WriteLine(MyBaseC.MyStruct.x);

While an instance of a class contains a separate copy of all instance fields of the class,
there is only one copy of each static field.
It is not possible to use this to reference static methods or property accessors.
If the static keyword is applied to a class, all the members of the class must be static.
Classes and static classes may have static constructors. Static constructors are called at
some point between when the program starts and the class is instantiated.

Das könnte Ihnen auch gefallen