Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Introducing C#

An introduction to C Sharp and object oriented programming. Superficially covers a few aspects of C Sharp and builds your first C# program, the classic 'Hello World'.

On Friday, April 2nd 2004 at 02:44 PM
By Andrew Pociu (View Profile)
*****   (Rated 4.7 with 25 votes)
Contextual Ads
More C# Resources
Advertisement

Introducing C#


C Sharp is part of the next generation of programming languages.
The transition from C++ to C# is similar to the transition from C to C++. C++ was an object oriented language, while C Sharp is a fully object oriented language. You will notice this even in your first C# program, 'Hello World'.
C Sharp combines the security of Java with the rapid development of VisualBasic and of course with the strenght of C++.

Hello World in C#


For your first C# program, I suppose you have Microsoft Visual Studio .NET installed on your station.
After opening MS Visual Studio .NET, from the File menu choose New Project.
Select the 'Visual C# Projects' folder and choose 'Console Application'. In the 'Name' field choose a name for your project, perhaps 'HelloWorld'. Also, in the 'Location' field choose the folder where you want the project files to be stored.

After you click 'OK', Visual Studio builds your project named 'HelloWorld'.
In the project there is an opened file called Class1.cs and inside the file there is this code:


using System;

namespace HelloWorld
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //
            // TODO: Add code to start application here
            //
        }
    }
}



First, let's rename the class, from Class1 to 'HelloWorld'. For that you need to change:


    /// </summary>
    class Class1
    {



to


    /// </summary>
    class HelloWorld
    {



and to rename the file 'Class1.cs' to 'HelloWorld.cs'. Rename the file from 'Solution Explorer' window (upper-right corner) using right-click and 'Rename'.

Now replace the comment:


      //
      // TODO: Add code to start application here
      //



with


      Console.WriteLine("Hello world!");



and remove the argument


string[] args



from 'static void Main(string[] args)' because we don't need 'Main' taking any arguments.

We are almost at the part where we compile the code that now looks like this:


using System;

namespace HelloWorld
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class HelloWorld
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Console.WriteLine("Hello world!");
        }
    }
}



But first, let's talk a little bit about object oriented programming, which is the heart of C#.

C# - Object oriented programming



Suppose we have a movie. A movie can have a runtime, a director, a certification, a price...
Also, a movie can be of many types, or genres. A comedy, a drama, a documentary.
All three of them are movies, but different type of movies.
In C#, if we had to include our movie in a program, the movie would be a class. A comedy, drama or documentary are objects of that class.
That means a class is composed from objects. We access the objects of that class using:


Movie.Comedy
Movie.Drama
Movie.Documentary


It makes sense, 'Comedy' is usually a type of movie. If you would have a big database of movies, similar to the IMDB one, you would organize it by the genre of the movies.

Moreover, a movie can also be a comedy and a sci-fi.
Let's take the old 'Back to the future' movie as an example. We can add it like this:


Movie.Comedy.Sci-Fi.BackToTheFuture



Further, what can you do with a movie? Play it, of course. We can represent this as:


Movie.Comedy.Sci-Fi.BackToTheFuture.Play()



OK, I think you got the point about object oriented programming. Every entity in a program is represented as an object, even if it's tangible or rather abstract.

Now, let's suppose we use the class 'movie' a lot in our program. For the sake of productivity we can skip typing 'Movie' from 'Movie.Comedy.Sci-Fi.BackToTheFuture.Play()' by writing:


using Movie;


before the class definition. If you know a bit of C++ programming, you probably remember this from namespaces (and of course 'using namespace std').

By specifing 'using Movie' before the class definition we can now use 'Comedy.Sci-Fi.BackToTheFuture.Play()' inside the class.

Back to 'Hello World'



Now you should be able to recognize the 'HelloWorld' class inside our little program.
It's time to compile our code. Press Ctrl+Shift+B or choose 'Build Solution' from the 'Build' menu and after the build project is complete, press F5 (or the '>' shaped button on the toolbar).
If the compile is successful you should be able to see the result, the 'Hello World!' text beeing displayed in a MS-DOS console.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by nickeax on Saturday, May 7th 2005 at 08:38 PM

Great work, and thanks very much for introducing me to this.

by Adam487 on Sunday, July 30th 2006 at 05:52 PM

thank you for the indroduction

by Imran on Friday, September 15th 2006 at 02:51 AM

Thanks to introduce me.Its good example to learn.

by generic viagra on Wednesday, July 27th 2011 at 05:05 AM

thank you for all

by cheap viagra on Wednesday, July 27th 2011 at 05:07 AM

The post is written in very a good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post.

by kamagra on Tuesday, October 4th 2011 at 08:28 AM

hey buddy,this is one of the best posts that I’ve ever seen; you may include some more ideas in the same theme. I’m still waiting for some interesting thoughts from your side in your next post.

by buy viagra on Tuesday, October 4th 2011 at 08:33 AM

Pleased to visit your site,just good job. Thanks for sharing the wonderful experience.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia
Other Resources
Bargain EZ Tech Coupons & Deals