This excerpt from Microsoft Visual C#.NET 2003 Kick Start covers the essentials of C# OOP, starting with creating classes and objects. Learn how access modifiers let you restrict access to the members of classes and structs, encapsulating your data and methods as needed.

Creating Classes

This chapter discusses object-oriented programming in C#. OOP is what C# is
all about; in this chapter, we're going to specialize on this topic. You
may well be an accomplished OOP programmer already, in which case it's
still a good idea to scan this chapter. OOP in C# has several differences from
all other object-oriented languages.

If you're an OOP programmer, you know that object-oriented programming
centers on creating types. The simple type int lets you declare integer
variables, and in the same way, you can create your own classes, which contain
not only data like the simple types, but methods as well. Just as you create
integer variables with the int type, so you create objects from
classes. An integer variable is an instance of the int type,
just like an object is an instance of a class.

Classes are types, but are far more powerful than the simple types like
int and float. Not only can you customize your data storage
using classes, but you can also add methods to classes. That kind of
compartmentalizationwhere data and methods are rolled up into a single
classis the entire reason that OOP was introduced in the first place. It
enables the programmers to deal with larger programs. The process of wrapping
related data and methods into a class (and so preventing them from cluttering up
the rest of the program) to create a single entity is called
encapsulation.

Here, the code is all contained in one class, the ch01_01 class. But
there's nothing to stop you from creating other classes in the same file.
For example, here, we've added another class, Calculator, with one
method, Addem, which adds two integers and returns their sum: