Wei-Meng Lee - C# 2008 Programmer's Reference

Здесь есть возможность читать онлайн «Wei-Meng Lee - C# 2008 Programmer's Reference» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Город: Indianapolis, Год выпуска: 2009, ISBN: 2009, Издательство: Wiley Publishing, Inc., Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

C# 2008 Programmer's Reference: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «C# 2008 Programmer's Reference»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

C# 2008 Programmers Reference provides a concise and thorough reference on all aspects of the language. Each chapter contains detailed code samples that provide a quick and easy way to understand the key concepts covered.

C# 2008 Programmer's Reference — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «C# 2008 Programmer's Reference», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Console.WriteLine("Fourth constructor");

}

}

The statement:

Contact c1 = new Contact(1234, "Wei-Meng", "Lee", "weimenglee@learn2develop.net");

prints the following output:

Second constructor

Third constructor

Fourth constructor

Static Constructors

If your class has static members, it is only sometimes necessary to initialize them before an object is created and used. In that case, you can add static constructors to the class. For example, suppose that the Contactclass has a public staticmember countto record the number of the Contact object created. You can add a static constructor to initialize the static member, like this:

public class Contact {

//...

public static int count;

static Contact() {

count = 0;

Console.WriteLine("Static constructor");

}

//---first constructor---

public Contact() {

count++;

Console.WriteLine("First constructor");

}

//...

}

When you now create instances of the Contactclass, like this:

Contact c1 = new Contact();

Contact c2 = new Contact();

Console.WriteLine(Contact.count);

the static constructor is only called once, evident in the following output:

Static constructor

First constructor

First constructor

2

Note the behavior of static constructors:

□ A static constructor does not take access modifiers or have parameters.

□ A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

□ A static constructor cannot be called directly.

□ The user has no control on when the static constructor is executed in the program.

Copy Constructor

The C# language does not provide a copy constructor that allows you to copy the value of an existing object into a new object when it is created. Instead, you have to write your own.

The following copy constructor in the Contactclass copies the values of the properties of an existing object (through the otherContactparameter) into the new object:

class Contact {

//...

//---a copy constructor---

public Contact(Contact otherContact) {

this.ID = otherContact.ID;

this.FirstName = otherContact.FirstName;

this.LastName = otherContact.LastName;

this.Email = otherContact.Email;

}

//...

}

To use the copy constructor, first create a Contact object:

Contact c1 = new Contact(1234, "Wei-Meng", "Lee",

"weimenglee@learn2develop.net");

Then, instantiate another Contact object and pass in the first object as the argument:

Contact c2 = new Contact(c1);

Console.WriteLine(c2.ID); //---1234---

Console.WriteLine(c2.FirstName); //---Wei-Meng---

Console.WriteLine(c2.LastName); //---Lee---

Console.WriteLine(c2.Email); //---weimenglee@learn2develop.net---

Object Initializers (C# 3.0)

Generally, there are two ways in which you can initialize an object — through its constructor(s) during instantiation or by setting its properties individually after instantiation. Using the Contactclass defined in the previous section, here is one example of how to initialize a Contactobject using its constructor:

Contact c1 = new Contact(1234, "Wei-Meng", "Lee", "weimenglee@learn2develop.net");

You can also set an object's properties explicitly:

Contact c1 = new Contact();

c1.ID = 1234;

c1.FirstName = "Wei-Meng";

c1.LastName = "Lee";

c1.Email = "weimenglee@learn2develop.net";

In C# 3.0, you have a third way of initializing objects — when they are instantiated. This feature is known as the object initializers. The following statement shows an example:

Contact c1 = new Contact() {

ID = 1234,

FirstName = "Wei-Meng",

LastName = "Lee",

Email = "weimenglee@learn2develop.net"

};

Here, when instantiating a Contactclass, you are also setting its properties directly using the {}block. To use the object initializers, you instantiate an object using the newkeyword and then enclose the properties that you want to initialize within the {}block. You separate the properties using commas.

Do not confuse the object initializer with a class's constructor(s). You should continue to use the constructor (if it has one) to initialize an object. The following example shows that you use the Contact's constructor to initialize the IDproperty and then the object initializers to initialize the rest of the properties:

Contact c2 = new Contact(1234) {

FirstName = "Wei-Meng",

LastName = "Lee",

Email = "weimenglee@learn2develop.net"

};

Destructors

In C#, a constructor is called automatically when an object is instantiated. When you are done with the object, the Common Language Runtime (CLR) will destroy them automatically, so you do not have to worry about cleaning them up. If you are using unmanaged resources, however, you need to free them up manually.

When objects are destroyed and cleaned up by the CLR, the object's destructor is called. A C# destructor is declared by using a tilde (~) followed by the class name:

class Contact : Object {

//---constructor---

public Contact() {

//...

}

//---destructor---

~Contact() {

//---release unmanaged resources here---

}

//...

}

The destructor is a good place for you to place code that frees up unmanaged resources, such as COM objects or database handles. One important point is that you cannot call the destructor explicitly — it will be called automatically by the garbage collector.

To manually dispose of your unmanaged resources without waiting for the garbage collector, you can implement the IDisposableinterface and the Dispose()method.

Chapter 5 discusses the concept of interfaces in more detail.

The following shows the Contactclass implementing the IDisposableclass and implementing the Dispose()method:

class Contact : IDisposable {

//...

~Contact() {

//-–-call the Dispose() method---

Dispose();

}

public void Dispose() {

//---release unmanaged resources here---

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «C# 2008 Programmer's Reference»

Представляем Вашему вниманию похожие книги на «C# 2008 Programmer's Reference» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «C# 2008 Programmer's Reference»

Обсуждение, отзывы о книге «C# 2008 Programmer's Reference» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x