Object-Oriented Programming in C#
- Creating Classes
- Creating Objects
- Using Access Modifiers
- Creating Fields and Using Initializers
- Creating Methods
- Creating Properties
- Read-only Properties
- Creating Constructors
- Creating Structs
- Creating Static Members
- Creating Static Fields
- Creating Static Properties
- Creating Destructors and Handling Garbage Collection
- Overloading Methods
- Overloading Operators
- Creating Namespaces
- In Brief
In this chapter
Creating Classes
Creating Objects
Using Access Modifiers
Creating Fields and Using Initializers
Creating Methods
Creating Properties
Read-only Properties
Creating Constructors
Creating Structs
Creating Static Members
Creating Static Fields
Creating Static Properties
Creating Destructors and Handling Garbage Collection
Overloading Methods
Overloading Operators
Creating Namespaces
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 compartmentalizationwhere data and methods are rolled up into a single classis 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.
You create classes in C# with the class statement:
[attributes] [modifiers] class identifier [:base-list] { class-body }[;]
Here are the parts of this statement:
attributes (Optional)Attributes hold additional declarative information, as we'll see in Chapter 14, "Using Attributes and Reflection."
modifiers (Optional)The allowed modifiers are new, static, virtual, abstract, override, and a valid combination of the four access modifiers we'll see in this chapter.
identifierThe class name.
base-list (Optional)A list that contains the base class and any implemented interfaces, separated by commas.
class-bodyDeclarations of the class members.
For C++ Programmers
The semicolon after a class declaration is optional in C#, unlike C++.
We've been using classes since the first page of this book, as in example ch01_01.cs:
class ch01_01 { static void Main() { System.Console.WriteLine("Hello from C#."); } }
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:
class ch03_01 { static void Main() { . . . } } class Calculator { public long Addem(int value1, int value2) { return value1 + value2; } }
We've created a new class nowthe Calculator class. To put that class to use, we simply have to create an object of that class.