Beruflich Dokumente
Kultur Dokumente
together variables of other types, Fields,methods and events. A class is like a blueprint.
It defines the data and behavior of a type.
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
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
=>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.
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.