logo search
CSharp_Prog_Guide

Идентификация объекта и идентификация значения26

Сравнивая два объекта на предмет равенства, сначала необходимо определить, нужно ли узнать, представляют ли две переменные один объект в памяти или значения одного или нескольких их полей являются равными. Если планируется сравнить значения, следует решить, являются ли объекты экземплярами типов значений (структурами) или ссылочными типами (классами, делегатами, массивами).

Classes

A class is the most powerful data type in C#. Like structures, a class defines the data and behavior of the data type. Programmers can then create objects that are instances of this class. Unlike structures, classes support inheritance, a fundamental part of object-oriented programming. For more information, see Inheritance.

Declaring Classes

Classes are defined by using the class keyword, as shown in the following example:

public class Customer

{

//Fields, properties, methods and events go here...

}

The class keyword is preceded by the access level. Because public is used in this case, anyone can create objects from this class. The name of the class follows the class keyword. The remainder of the definition is the class body, where the behavior and data are defined. Fields, properties, methods, and events on a class are collectively referred to as class members.

Creating Objects

Although they are sometimes used interchangeably, a class and an object are different things. A class defines a type of object, but it is not an object itself. An object is a concrete entity based on a class, and is sometimes referred to as an instance of a class.

Objects can be created by using the new keyword followed by the name of the class that the object will be based on, like this:

Customer object1 = new Customer();

Классы

Класс — это самый эффективный тип данных в C#. Как и структуры, класс определяет данные и поведение типа данных. При наличии класса программисты могут создавать объекты, являющиеся его экземплярами. В отличие от структур классы поддерживают наследование, фундаментальную часть объектно-ориентированного программирования. Дополнительные сведения см. в разделе Наследование.