Using Barcode creation for Software Control to generate, create UPC - 13 image in Software applications.

www.OnBarcode.com

C# static constructors are the equivalent of Java static initialization blocks. The syntax of a C# static constructor is similar to that of an instance constructor, supporting attributes and the extern modifier. No other modifiers are supported, including access modifiers. For example:

Only one static constructor can be defined per class; Java supports multiple static initialization blocks.

Destructors

Destructors are the C# equivalent of Java finalizers. To implement a finalizer in Java, a class overrides the java.lang.Object.Finalize method. In C#, a destructor is a method with a name equal to the class name, preceded by a tilde (~). Destructors are used by the garbage collector to allow the CLR to reclaim resources. The following example demonstrates the declaration of a destructor:

public class MyClass { // The following method is the destructor for MyClass ~MyClass() { // Destructor Code } }

Although destructors are methods, the following limitations apply:

Classes can define only one destructor. Structs cannot define destructors. Structs are allocated memory on the stack, not the managed heap. Stack memory is freed as soon as a struct goes out of scope; hence, the garbage collector is not involved.

5. Data Types

A destructor never takes parameters or returns a value. The only modifier applicable to a destructor is extern. Destructors are not inherited by derived classes. Destructors cannot be explicitly called; they can be invoked only by the garbage collector. Destructors implicitly call the Finalize method on the object base class.

Warning

As in Java, destructors must be used with care and planning. The incorrect use of destructors can introduce unexpected behavior into an application. Destructors are covered in more detail in Appendix D, "Garbage Collection."

IDisposable

As an alternative to destructors, C# provides the System.IDisposable interface for clearing up object resources without waiting for the automated garbage collection process to call a destructor. A class that implements IDisposable should correctly release any resources it holds when its Dispose method is called. C# provides the using statement, which is shorthand syntax for acquiring and disposing of objects that implement the IDisposable interface. See the "Statements" section in 4 for complete details of the using statement.

Methods

The only difference between C# and Java is the additional types of parameters available.

Declaration

Method declarations have the following syntax: [attributes] [modifiers] return-type identifier ( parameter-list ) { body } For example, a protected static method named MyMethod that returns an integer and takes a string and a bool as parameters is declared as follows:

When applying modifiers to method declarations, the following restrictions apply:

The static, virtual, and override modifiers are mutually exclusive. The new and override modifiers are mutually exclusive. If the method is abstract, it cannot be static, virtual, or extern. If the method is private, it cannot be virtual, override, or abstract. The sealed modifier must be accompanied by override.

Parameters

All parameters are passed by value in Java. In addition to pass-by-value parameters, C# supports two new parameter types: reference and output parameters. C# also provides a mechanism called parameter arrays, which is used to pass a variable number of arguments to a method.We discuss parameter arrays in the following section. See the "Variables" section later in this chapter for a complete coverage of value, reference, and output parameters.