Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
63
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

The runtime would call the Destructor (or Finalize() method) when the object is no longer referenced.

Now the output would be like this:

Performing usual tasks...

Destructing object...

Freeing resources captured by this object...

Press any key to continue

Destructors and Performance Overhead

Having a destructor or Finalize() method in your class creates some performance overhead, therefore, it is recommended that you do not declare these in your classes unless your class is holding un-managed resources like file handles, databases or network connections.

System.GC.Collect() method

The System.GC class provides a static method named Collect() which enforces the Garbage Collection to start at your will. It is usually better to leave the invocation of the Garbage Collector to the .Net Runtime and it is not wise to start a Garbage Collector process just for freeing some objects in memory. But, you may use the method when a large number of objects in your program are un-referenced (E.g., an array or collection) and you want the memory to be re-claimed instantly.

Nested Classes in C#

So far, we have only seen top-level classes (classes only bound by namespace) in our C# programs. We can also define nested classes in C#. Nested classes (also called inner classes) are defined inside another class, like this:

using System;

namespace CSharpSchool

{

class Test

{

static void Main()

{

Outer.Inner inner = new Outer.Inner(); inner.InnerFun();

}

}

112

Programmers Heaven: C# School

class Outer

{

private static string name="Faraz";

public Outer()

{

Console.WriteLine("Constructor of Outer called...");

}

public class Inner

{

public Inner()

{

Console.WriteLine("Constructor of Inner called...");

}

public void InnerFun()

{

Console.WriteLine("InnerFun() called...");

Console.WriteLine("Name in Outer is {0}", name);

}

}

}

}

Here, we have defined a nested class named Inner, contained inside the top-level class Outer. Both the Outer class and the Inner class have no-argument constructors. While the top-level classes can only be marked as public or internal, nested classes can be marked with all the access modifiers: private, public, protected, internal, internal OR protected, with the same meaning as described in lesson 5. Like other class members, the default access modifier for nested classes is private, which means that the nested class can not be referenced and accessed outside the containing class. In order to reference and instantiate the Inner class in the Main() method of the Test class, we mark it as public in our program. It is also worth-noting that nested classes are always accessed and instantiated with reference to their container or enclosing class. That is the reason why we instantiated the Inner class in our Main() method as:

static void Main()

{

Outer.Inner inner = new Outer.Inner(); inner.InnerFun();

}

113

Programmers Heaven: C# School

Another important point about nested classes is that they can access all the (private, internal, protected, public) static members of the enclosing class. We have demonstrated this in the InnerFun() method of the Inner class, where it accesses the private member (name) of the enclosing (Outer) class.

public void InnerFun()

{

Console.WriteLine("InnerFun() called...");

Console.WriteLine("Name in Outer is {0}", name);

}

When we compile and run the above program, we get the following output:

Constructor of Inner called...

InnerFun() called...

Name in Outer is Faraz

Press any key to continue

Author's Note: Nested classes in C# are similar to Java's static inner classes, but there is no concept like Java's non-static inner

classes in C#.

Since the nested classes are instantiated with reference to the enclosing class, their access protection level always remains less than or equal to that of the enclosing class. Hence, if the access level of the enclosing class is internal and the access level of the nested class is public, the nested class would only be accessible in the current assembly (project).

114

Соседние файлы в папке books