Home > Articles > Programming

Object-Oriented Programming in C#

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Microsoft Visual C#.NET 2003 Kick Start

This chapter is from the book
Microsoft Visual C#.NET 2003 Kick Start

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.

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 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.

  • Share ThisShare This
  • Your Account

Discussions

what is the asynchronous methods in c#.net
Posted Apr 2, 2008 04:40 AM by jeevan.mummadi
0 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevMinutes from the October 2009 Meeting
By Danny Kalev on Yesterday No Comments

The minutes from the Santa Cruz (October 2009) meeting are available here. Even if you're not a language layer at heart, I encourage you to read them.

Danny KalevA Reader's Opinion on Attributes
By Danny Kalev on October 20, 2009 No Comments

In August I dedicated a series to the debate about C++0x attributes. I believe that it covered the subject in a balanced and detailed way, but I keep getting complaints from C++ users who don't like attributes for various reasons. Here's a recent email I received from a Polish C++ programmer. While it  doesn't represent my opinion about attributes -- I'm rather neutral about this feature and consider it a "solution waiting for a problem" -- but it suggests that attributes are still a highly controversial issue that will haunt C++ for a long time. The email is quoted here with minor edits that and as usual, with all private details removed.

Danny KalevFollowup: The Web 2.0 Guy I Ain't
By Danny Kalev on October 16, 2009 1 Comment

Almost a year ago, I posted here The Web 2.0 Guy I Ain't. People wonder whether I still resist all those Web 2.0 features and technologies at the end of 2009.

See All Related Blogs

Informit Network