Particular instances of a class are called objects. The difference between a class and an object is the same as the difference between the concept of a Dog and the particular dog who is sitting at your feet as you read this. You can't play fetch with the definition of a Dog, only with an instance.
A Dog class describes what dogs are like; they have weight, height, eye color, hair color, disposition, and so forth. They also have actions they can take, such as eat, walk, bark, and sleep. A particular dog (such as my dog Milo) will have a specific weight (62 pounds), height (22 inches), eye color (black), hair color (yellow), disposition (angelic), and so forth. He is capable of all the actions — methods, in programming parlance — of any dog (though if you knew him you might imagine that eating is the only method he implements).
Suppose, for instance, you want to sort the contents of an instance of a Windows listbox control. The listbox control is defined as a class. One of the properties of that class is that it knows how to sort itself. Sorting is encapsulated within the class, and the details of how the listbox sorts itself are not made visible to other classes. If you want a listbox sorted, you just tell the listbox to sort itself, and it takes care of the details.
So, you simply write a method that tells the listbox to sort itself — and that's what happens. How it sorts is of no concern; that it does so is all you need to know.
Class behavior is created by writing methods (sometimes called member functions). A method is a small routine that every object of the class can execute. For example, a Dog class might have a bark method, and a listbox class might have a sort method.
Class state is maintained by fields (sometimes called member variables). Fields may be primitive types (e.g., an int to hold the age of the dog or a set of strings to hold the contents of the listbox), or fields may be objects of other classes (e.g., an Employee class may have a field of type Address).
Finally, classes may also have properties, which act like methods to the creator of the class, but look like fields to clients of the class. A client is any object that interacts with instances of the class.
When you define a new class, you define the characteristics of all objects of that class, as well as their behaviors. For example, if you create your own windowing operating system, you might want to create screen widgets, (known as a control in Windows). One control of interest might be a listbox, a control that is very useful for presenting a list of choices to the user and enabling the user to select from the list.
Listboxes have a variety of characteristics: height, width, location, and text color, for example. Programmers have also come to expect certain behaviors of listboxes — they can be opened, closed, sorted, and so on.
Object-oriented programming allows you to create a new type, ListBox, which encapsulates these characteristics and capabilities.
[attributes] [access-modifiers] class identifier [:base-class]
The identifier is the name of the class that you provide. Typically, C# classes are named with nouns (e.g., Dog, Employee, ListBox). The naming convention (not required, but strongly encouraged) is to use Pascal notation. In Pascal notation, you don't use underbars or hyphens, but if the name has two words (Golden Retriever) you push the two words together, each word beginning with an uppercase letter (GoldenRetriever).
related postVISUAL STUDIO INTRODUCTION
C SHARP INTRODUCTION
C SHARP OUT LOOK
DOT NET AND C SHARP
C SHARP APPLICATION STRICTURE
OOPS AND C SHARP
IDE AND C SHARP
INSTANTIATING OBJECTS IN C SHARP
CLASSES AND OBJECTS IN C SHARP
OPERATORS IN C SHARP
SWITCH AND ITERATION IN C SHARP
BRANCHING IN C SHARP
CONSTANTS AND STRING
STATIC AND INSTANCE MEMBERS IN DOT NET