Preview

C SHARP FUNDAMENTALS

Good Essays
Open Document
Open Document
4128 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
C SHARP FUNDAMENTALS
Classes
A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events. A class is like a blueprint. It defines the data and behaviour of a type. If the class is not declared as static, client code can use it by creating objects or instances which are assigned to a variable. The variable remains in memory until all references to it go out of scope. At that time, the CLR marks it as eligible for garbage collection. If the class is declared as static, then only one copy exists in memory and client code can only access it through the class itself, not an instance variable. For more information, see Static Classes and Static Class Members (C# Programming Guide).
Unlike structs, classes support inheritance, a fundamental characteristic of object-oriented programming. For more information, see Inheritance (C# Programming Guide).
Declaring Classes
Classes are declared by using the class keyword, as shown in the following example:
C#
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 theclass 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:
C#
Customer object1 = new Customer();
When an instance of a class is created, a reference to the object is passed back to

You May Also Find These Documents Helpful

Related Topics