October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Learning C# and OOP: Getting Started, Objects and Encapsulation

  • October 21, 2003
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

C# Programming, Notes # 100


Preface

First in a miniseries

This lesson is the first in a new miniseries designed to teach you how to write object-oriented programs using C#.  This miniseries will describe and discuss the necessary and significant aspects of object-oriented programming (OOP) using C#.

Relatively high level

I will provide the information in a high-level format, devoid of any prerequisite requirement to know C# syntax.  In those cases where an understanding of C# syntax is required, I will provide the necessary syntax information in the form of sidebars and sample programs.

Therefore, if you have a general understanding of computer programming, you should be able to read and understand the lessons in this miniseries, even if you don't have a background in object-oriented programming, or a background in the C# programming language.

Comparisons with Java

Occasionally I will also provide comparative information for C# and Java.  By so doing, I will emphasize the similarities between the two programming environments that are likely to dominate the programming world in the foreseeable future.

If you already know how to do object-oriented programming using Java, it is relatively easy to learn how to do object-oriented programming using C#.  Similarly, if you learn how to do object-oriented programming using C#, it will be relatively easy to learn how to do object-oriented programming using Java.

Thus, by studying these lessons and learning about OOP using C#, you will, at the same time, be learning quite a lot about OOP using Java.

Viewing tip

You may find it useful to open another copy of this lesson in a separate browser window.  That will make it easier for you to scroll back and forth among the different listings while you are reading about them.

Supplementary material

I recommend that you also study the other lessons in my extensive collection of online programming tutorials.  You will find those lessons published at developer.com.  However, as of the date of this writing, developer.com doesn't maintain a consolidated index of my  tutorial lessons, and sometimes they are difficult to locate there.  You will find a consolidated index at www.DickBaldwin.com.

Preview

Getting started with C#

This tutorial lesson consists of two largely unrelated but important sections.  The first section provides the information that you will need to get started programming in C#.  This section includes information such as where to download the software development kit that makes it possible to compile and execute C# programs.

Getting started with OOP

The second section provides an introduction to objects and encapsulation.  In order to understand OOP, you need to understand the following three concepts:

  • Encapsulation
  • Inheritance
  • Polymorphism

This lesson will concentrate on encapsulation.  Encapsulation will be used as a springboard for a discussion of objects.

A description of an object-oriented program will be provided, along with a description of an object, and how it relates to encapsulation.

A real-world analogy

In order to relate object-oriented programming to the real world, a car radio will be used to illustrate and discuss several aspects of software objects.  For example, you will learn that car radios, as well as software objects, have the ability to store data, along with the ability to modify or manipulate that data.

Messages, actions, state, etc.

You will learn that car radios, as well as software objects, have the ability to accept messages and to perform an action, modify their state, return a value, or some combination of the above.

You will learn some of the jargon used in OOP, including persistence, state, messages, methods, and behaviors.

Where do objects come from?

You will learn where objects come from, and you will learn that a class is a set of plans that can be used to construct objects.  You will learn that a C# object is an instance of a class.

(Note, a C# object can also be an instance of a struct.  A struct is also a set of plans from which objects can be built.  The main difference between objects built from classes and objects built from structs is how they are stored in memory.  I will deal mostly with objects built from classes in this set of tutorial lessons, but will also deal with structs when appropriate.)

A little bit of C# code

In this lesson, you will see a little bit of C# code, used to create an object, and then to send a message to that object (invoke a method on the object).

You will learn about C# references and reference variables.  You will also learn a little about memory allocation for objects and variables in C#.

Getting Started with C#

The good news

The good news is that you don't need to go out and spend a lot of money purchasing a high-level development environment just to begin programming with C#.  As of the date of this writing, everything that you need to get started can be downloaded for free.  All you need is the Software Development Kit (SDK) from Microsoft and a simple text editor.  I will tell you where to get both for free.

The lessons in this miniseries will be slanted toward the use of the SDK and a text editor as opposed to the use of a high-level development environment.  Of course, if you want to spend a lot of money, you can purchase Microsoft's Visual Studio and use it instead of a text editor.

The bad news

The bad news is that the download file for the SDK is very large (137.3 megabytes).  Unless you have high-speed Internet access, it is going to take you a long time to download it.  Fortunately, the material is also available as ten smaller files for the benefit of those of you who are using a dial-up Internet connection, and is also available on a CD for a modest fee..

Where to get the SDK

For the big picture viewpoint, you might want to visit the Microsoft® .NET Framework Home Page at the following URL:

http://msdn.microsoft.com/netframework/

As of the date of this writing, the Microsoft® .NET Framework Software Development Kit (SDK) is available for free downloading at the following URL:

http://msdn.microsoft.com/downloads/default.asp?url=/downloads/sample.asp?url=/msdn-files/027/000/976/msdncompositedoc.xml&frame=true

If downloading the file is impractical for you because of its size, you can purchase a CD containing the SDK for a nominal fee at the following URL:

http://microsoft.order-2.com/trialstore/category.asp?catalog%5Fname=MSTrialAndEval&category%5Fname=Developer+Tools&Page=1

What you get in the download

According to Microsoft,

"The Microsoft® .NET Framework Software Development Kit (SDK) includes the .NET Framework, as well as everything you need to write, build, test, and deploy .NET Framework applicationsdocumentation, samples, and command-line tools and compilers."

Other capabilities

My primary interest at this point is C#, and the ability to compile and execute C# programs is included in the SDK.  Although I haven't taken the time to explore the other capabilities of the SDK, I believe that the SDK may also provide other programming capabilities such as Visual Basic (VB.net).

SDK updates

As of the date of this writing, there are a couple of SDK updates available for downloading at the same URL listed above.  Other updates may be added over time as the product develops.  You might as well go ahead and download the updates while you are there.

System requirements

According to Microsoft, the .NET Framework SDK runs on:

  • Microsoft Windows NT® 4.0 (SP 6a required)
  • Microsoft Windows® 2000 (SP 2 recommended)
  • Microsoft Windows XP Professional

The installed size is around 340 megabytes and the installable file is around 137 megabytes, so you will need quite a lot of available disk space to support this programming environment.  (Of course, you can delete the installable file after the installation is complete.)

I am running the SDK on Windows 2000, Version 5.0, Service Pack 2.

SDK updates

As mentioned above, I have also downloaded and installed a couple of SDK updates listed as:

  • .NET Framework Service Pack 1
  • .NET Framework ASP.NET Session State Hotfix

Note that it was necessary for me to install the updates in the order listed above, after installing the SDK.

Installation

Installation of the SDK and the updates is very straightforward.  When you download and save the SDK, you will end up with a file named setup.exe on your hard drive.  Simply close all applications and double click on that file to install the SDK.  The installer will walk you through the installation process, and the SDK will be ready to run when installation is complete.

Similarly, when you download the updates, you will end up with executable files with names something like the following:

  • NDP10SP317396.EXE
  • NDP10_QFEM_Q322289_En.exe

Just double-click on each update file in succession, and the installer will walk you through the installation process for the updates.  As I mentioned earlier, I had to install my two updates in the following order:

  • .NET Framework Service Pack 1
  • .NET Framework ASP.NET Session State Hotfix

Be patient

The installation takes a long time, at least that is the case on my machine.  Although I didn't time it, the installation of the SDK must have taken at least half an hour.  It seemed that long anyway. (In fact, I had previously installed a beta version of the SDK, and at least half an hour was required to uninstall the beta version before beginning the installation of the new version.)

Installation of each update also required fifteen or twenty minutes as well.

A C# editor

In addition to the SDK, you will need an editor for creating source code files.  Any editor capable of creating clean ASCII text will do (you could use Windows NotePad if you wish, but I wouldn't recommend it).

Although just about any editor will do, it's nice to use an editor that is color-coded for C#.  If you go to Google and search for c# editor, you will find several downloadable editors ranging from free to expensive.  Some are color-coded, and some are not.  Some are simply editors while others make it possible to compile and execute your program from within the editor (that might be referred to as a simple IDE).

SharpDevelop IDE

One such free color-coded editor that allows you to compile and execute from within the IDE can be downloaded from the following URL:

http://www.icsharpcode.net/OpenSource/SD/Download/default.asp

I haven't used this editor very much, so I don't recommend for or against it.  However, it seems to work OK and its free, so you might as well give it a try.

Testing your installation

Once you have completed your installation, you are ready to test the installation.  If you download and use the SharpDevelop IDE mentioned above, you can create a source file and test it from within the IDE.  However, at this stage, in order to eliminate as many variables as possible, I recommend that you test from a Windows command prompt.

From a command-prompt window

Open a command-prompt window, which should display a command prompt something like the following:

D:\Baldwin\CsharpProg\MySampProgs>

Enter the following command at the command prompt:

csc /help

If your installation is OK, this should cause the C# compiler to run and to display a long list of usage instructions on the screen (csc is the name of the C# compiler).  If that doesn't happen, you need to determine what went wrong with your installation and fix the problem.  I'm afraid that I can't help you much with that.

Edit, compile, and run a simple program

If all goes well to this point, copy the code shown in Listing 1 into your editor, and save the file as Hello01.cs (C# source files should have an extension of .cs).
 

public class Hello01{
  public static void Main(){
    System.Console.WriteLine(
                     "Hello C# World");
  }//end Main
}//end class Hello01

// Listing 1

Back to the command prompt

Returning to your command prompt, change directories to the directory containing the file named Hello01.cs.  Then enter the following command at the command prompt (as mentioned above, csc is the name of the C# compiler program):

csc Hello01.cs

This should compile your program stored in the file named Hello01.cs, and produce an output file named Hello01.exe.

To execute your new C# program, simply enter the name of the exe file at the command prompt as follows:

Hello01

This should cause the program to execute and to produce the following output in the command-prompt window:

Hello C# World

Congratulations

You have just written, compiled, and executed your first C# program.  Don't worry about what the code in Listing 1 means at this point.  You will learn that as you study the lessons in this miniseries.  At this point, the objective is simply to confirm that you have a good installation of the SDK.

Documentation

When you installed the SDK, you should have ended up with the following directory and file structure on your hard drive:

...\Microsoft.NET\FrameworkSDK\StartHere.htm

The file named StartHere.htm points to a large documentation package for the framework.  Load that file into Internet Explorer.  This will expose a page containing links to various kinds of information including:

  • Getting Started with the .NET Framework
  • .NET Framework SDK documentation
  • Tool Developer's guide
  • QuickStarts, tutorials, and samples
  • Tools and debuggers

A large documentation package

This is a large documentation package, and learning to navigate it is an exciting challenge in its own right.  I will leave it up to you to deal with that challenge.  In the meantime, however, you can access a topical index by locating the Internet Explorer link corresponding to the second item in the above list and clicking on that link.  Then click on the Index button at the bottom left of the Internet Explorer screen to get access to an alphabetical index of the topics in the documentation.


Tags: C#, OOP

Originally published on http://www.developer.com.


Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel