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

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.

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top