Languages - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Languages arrow Object-Oriented Programming in C#
Object-Oriented Programming in C#
By Steven Holzner

Rate This Article: Add This Article To:

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 ac

Microsoft Visual C#.NET 2003 Kick Start

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.

ADVERTISEMENT

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.

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.

  • identifier—The class name.

  • base-list (Optional)—A list that contains the base class and any implemented interfaces, separated by commas.

  • class-body—Declarations 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 now—the Calculator class. To put that class to use, we simply have to create an object of that class.

Buy this book today!

Read more from this chapter...

Copyright 2003 Pearson Education, Inc.




Discuss Object-Oriented Programming in C#
 
>>> Be the FIRST to comment on this article!
 

 
 
>>> More Languages Articles          >>> More By Steven Holzner
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.