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», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

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

Интервал:

Закладка:

Сделать

The second statement is straightforward, as c1and c3are two different objects:

Console.WriteLine(c1.Equals(c3)); //---False---

The third and fourth statements assign c1to c3, which means that c1and c3are now two different variables pointing to the same object. Hence, Equals()returns True:

c3 = c1;

Console.WriteLine(c1.Equals(c3)); //---True---

The fifth and sixth statements test the reference equality of c1against c2and then c1against c3:

Console.WriteLine(Object.ReferenceEquals(c1, c2)); //---False---

Console.WriteLine(Object.ReferenceEquals(c1, c3)); //---True---

If two objects have reference equality, they also have value equality, but the reverse is not necessarily true.

Implementing Equals

By default the Equals()method tests for reference equality. To ensure that it tests for value equality rather than reference equality, you need to override the Equals()virtual method.

Using the same Contactclass used in the previous section, add the methods highlighted in the following code:

public class Contact {

public int ID;

public string FirstName;

public string LastName;

public string Email;

public override bool Equals(object obj) {

//---check for null obj---

if (obj == null) return false;

//---see if obj can be cast to Contact---

Contact c = obj as Contact;

if ((System.Object)c == null) return false;

//---check individual fields---

return

(ID == c.ID) && (FirstName == c.FirstName) &&

(LastName == c.LastName) && (Email == c.Email);

}

public bool Equals(Contact c) {

//---check for null obj---

if (c == null) return false;

//---check individual fields---

return

(ID == c.ID) && (FirstName == c.FirstName) &&

(LastName == c.LastName) && (Email == c.Email);

}

public override int GetHashCode() {

return ID;

}

}

Essentially, you're adding the following:

□ The Equals(object obj)method to override the Equals()virtual method in the System.Objectclass. This method takes in a generic object ( System.Object) as argument.

□ The Equals(Contact c)method to test for value equality. This method is similar to the first method, but it takes in a Contactobject as argument.

□ The GetHashCode()method to override the GetHashCode()virtual method in the System.Objectclass.

The as Keyword

In the Equals(object obj)method you saw the use of the as keyword:

Contact c = obj as Contact;

The as operator performs conversions between compatible types. In this case, it tries to cast the objobject into a Contactobject. The askeyword is discussed in detail in Chapter 5.

Notice that the Equals()methods essentially performs the following to determine if two objects are equal in value:

□ It checks whether the object passed is in null. If it is, it returns false.

□ It checks whether the object passed is a Contactobject (the second Equals()method need not check for this). If it isn't, it returns false.

□ Last, it checks to see whether the individual members of the passed-in Contactobject are of the same value as the members of the current object. Only when all the members have the same values (which members to test are determined by you) does the Equals()method return true. In this case, all the four members' values must be equal to the passed-in Contactobject.

The following statement will now print out True:

Console.WriteLine(c1.Equals(c2)); //---True---

ToString() Method

All objects in C# inherits the ToString()method, which returns a string representation of the object. For example, the DateTimeclass's ToString()method returns a string containing the date and time, as the following shows:

DateTime dt = new DateTime(2008, 2, 29);

//---returns 2/29/2008 12:00:00 AM---

Console.WriteLine(dt.ToString());

For custom classes, you need to override the ToString()method to return the appropriate string. Using the example of the Contactclass, an instance of the Contactclass's ToString()method simply returns the string " Contact":

Contact c1 = new Contact() {

ID = 1234,

FirstName = "Wei-Meng",

LastName = "Lee",

Email = "weimenglee@learn2develop.net"

};

//---returns "Contact"---

Console.WriteLine(c1.ToString());

This is because the ToString()method from the Contactclass inherits from the System.Objectclass, which simply returns the name of the class.

To ensure that the ToString()method returns something appropriate, you need to override it:

class Contact {

public int ID;

public string FirstName;

public string LastName;

public string Email;

public override string ToString() {

return ID + "," + FirstName + "," + LastName + "," + Email;

}

//...

}

In this implementation of the ToString()method, you return the concatenation of the various data members, as evident in the output of the following code:

Contact c1 = new Contact() {

ID = 1234,

FirstName = "Wei-Meng",

LastName = "Lee",

Email = "weimenglee@learn2develop.net"

};

//---returns "1234,Wei-Meng,Lee,weimenglee@learn2develop.net" ---

Console.WriteLine(c1.ToString());

Attributes

Attributes are descriptive tags that can be used to provide additional information about types (classes), members, and properties. Attributes can be used by .NET to decide how to handle objects while an application is running.

There are two types of attributes:

□ Attributes that are defined in the CLR.

□ Custom attributes that you can define in your code.

CLR Attributes

Consider the following Contactclass definition:

class Contact {

public string FirstName;

public string LastName;

public void PrintName() {

Console.WriteLine("{0} {1}", this.FirstName, this.LastName);

}

[Obsolete("This method is obsolete. Please use PrintName()")]

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

Интервал:

Закладка:

Сделать

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

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


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

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

x