The C family of programming languages are rivaled only by Java in their popularity.  In 2014, the C family (that is C, C++ and C#) had a combined market share of 27.3% to Java’s 19.3%[1]   Considering this, you will almost certainly learn one or the other in your college or university, and many software professionals have had successful careers specialising in the C family.

The oldest of the languages, C, was designed by Dennis Ritchie in 1972.[2] It has been a mainstay of operating system and low level application programming for decades.  It is what is known as an imperative language, which means a program is made up of functions that execute in a linear progression, with each one changing some state of the program.  For example, a line of code may add a number to another in memory, display a certain value on screen, or write a value to a file on disk.  Programs are structured with “if” statements which fork the execution path of a program into multiple directions, or loops, which repeat operations until a condition is met.  Complex data structures can be implemented, but in a more crude form than in C++ or C#, and the key factor is that the functions always take and manipulate these data structures, rather than exist as a part of them.

C++ moves us away from this paradigm into the Object-Oriented Programming (OOP) world.  In OOP, the data structures we once knew are objects capable of their own actions.[3]  Commonly encountered examples in university tutorials are things such as a car, which would have methods (the actions) such as driving or changing gears.  Instead of having a central program that operates on data structures, the data structures themselves gain some level of independence through what’s known as ‘encapsulation’.  This allows program objects to hide their internal workings and instead provide publically accessible interfaces – rather than setting the location on the aforementioned car, we must call the drive function to direct it where to go.  This allows the object to keep a consistent internal state and only allow other objects in the system to have as much knowledge about each other as is needed to perform their function, and never enough to break the functionality of another object.[4] Understanding this key aspect which differentiates C++ from C is crucial in becoming confident and competent with C++ and more modern programming languages as a whole.

C# is the last member of the traditional C family of languages (although Apple/iOS-focused Objective C has its place too) and is usually only considered in association with Microsoft’s .Net framework.[5]  Syntactically C# can be viewed as just a cleaner version of C++, and although it introduces a lot of modern programming language quirks, it is less of a departure from C++ than C++ was from C.  The .Net framework, on the other hand, provides easy to use tools to build Windows applications of all types, including desktop applications, web applications, web services, Windows services and Microsoft Office plugins.  This is essentially what drives the market share in C#.  The language has been improved significantly to modernise C++ to rival Java, as well.  Memory management is no longer the responsibility of the programmer, as the application is run within what’s known as managed execution, which resembles Java’s virtual machine.[6]  This attempts to handle one of the most common problems with C and C++ applications, which was improper memory usage leading to applications that become slower and more resource-heavy during their lifecycle.

This is but an overview of one of the largest and most feature-rich programming languages in the field of computer programming, but it touches on some of the most important fundamentals you will have to learn on your journey to becoming a software developer.  Indeed, you will encounter most of these concepts again and again regardless of which language you learn, so even if you don’t plan to make a career out of C programming, knowing these core concepts will stand you in good stead.

References:

1.  http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

2.  http://www.bell-labs.com/usr/dmr/www/chist.html

3.  http://searchsoa.techtarget.com/definition/object-oriented-programming

4.  http://www.tutorialspoint.com/cplusplus/cpp_data_encapsulation.htm

5.  https://msdn.microsoft.com/en-us/library/zw4w595w(v=vs.110).aspx

6.  https://msdn.microsoft.com/en-us/library/f144e03t(v=vs.110).aspx

 

Your first C# program

It is difficult for a developer to consider writing C# code to any real degree without dipping into the .NET Framework or Mono extensively.  As a result, most tutorials and examples you encounter take this coupling for granted and do not really differentiate between the two, and this will be no exception.  The following code is for use with .NET 4.6 (the latest version of the framework at the time of writing) but should work on Mono or older versions without too many problems.

First let’s turn our attention to the inevitable “Hello World” application.  First off we can make a new solution (this code is targeted at Visual Studio, the dominant IDE for C#), selecting the Console Application template.  This gives us a single code file, “Program.cs”, and an application config file called App.config, which we will disregard for now.  By default, the following code is given to us:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Test

{

    class Program

    {

        static void Main(string[] args)

        {

        }

    }

}

Anyone familiar with writing C console applications will recognize what they are seeing here.  The program class is designed to be the startup class for our application (from the project and solution properties) and the Main() method is our entry point.  At execution time, code will come in here, execute to the end of Main() function, and then quit.  If you run this application by hitting ‘start’, you will see a console window appear and disappear just as quickly, which means our empty application has executed without error.  The first thing we can do is add the following line to our application so that when it has completed, the application waits for us to press a key before exiting.  We will leave this line at the end of our application for the duration of this tutorial.  Add it, and try running your application again.

            Console.ReadKey();

Now add the following line above this one, to create our ‘Hello World’ application. 

            Console.WriteLine("Hello, World!");

When you run this, you will now see your greeting as text.  Let’s examine what’s happening here with both lines of code.  Console is a static class provided to us from the System namespace, which we can see at the top of our file (“using System;”).  Static classes do not require an instantiated object to execute their functions, and are instead executed solely from any code module that has access to the namespace of your static class.  The static methods we are using here are ‘WriteLine’ and ‘ReadKey’, which are fairly self-explanatory.  WriteLine is passed one argument, which is our text string, “Hello World!”  When we run that line of code, the application prints our text to the console, followed by a new-line.  The program then waits (also referred to as ‘blocking for input’) at the ‘Console.ReadKey()’ line until the user pushes a key and the program can continue to the end, exiting.  As you can see, these two lines actually perform a large number of low-level operations without us ever having to think about them, which is the strength of high level languages from a programming perspective.

Let’s go ahead and customize our input by reading from the console.  Above our WriteLine method, we can add another method – ReadLine.  ReadLine is like ReadKey, but instead we wait for a user to type several characters and press return, at which point the text they have typed will be returned and we can store it in a variable.  This is done with the equals (=) operator.  In the following example, we combine variable declaration and assignment into one line after prompting for input.  The output of the ReadLine method is subsequently stored in the userName variable which is of type string (as in a sequence of characters, or text).  Now, our program looks like this:

        static void Main(string[] args)

        {

            Console.WriteLine("Please enter your name:");

            string userName = Console.ReadLine();

            Console.WriteLine("Hello, " + userName);

            Console.ReadKey();

        }

However, this program is identical to the following:

        static void Main(string[] args)

        {

            Console.WriteLine("Please enter your name:");

            string userName;

            userName = Console.ReadLine();

            Console.WriteLine("Hello, " + userName);

            Console.ReadKey();

        }

This is because declaring a variable and later assigning it is the same as doing both actions on the same line.   In fact, the second example is a bad practice, as you may forget to assign your variable to some value before you use it, which will cause your program to crash.

The other part of this example that has changed is the ‘WriteLine’ method, which now is outputting "Hello, " + username.  In this instance, the + operation usually associated with adding numbers is combining two strings. As a result, when executed, the value inside ‘username’ is appended to the end of “Hello, “ to create a new string.  Try running this program now and experimenting with the output.

One brief word on variable declarations before we move on: You may see other tutorials and subsequent ones in this series use ‘var’ instead of ‘string’ (or any other type like ‘int’).  This is because modern versions of C# can save the programmer the job of declaring a variable type and instead let the compiler do that work.  As a result, the compiler has access to information about the ‘Console.ReadLine()’ method and can correctly determine that it will return a string, which it substitutes for ‘var’ at compile time.  As a result, this is equally valid, and an even better practice:

var userName = Console.ReadLine();

Having built this basic application you have gained a brief insight into input/output methods and the structure of an application.  Once you understand such procedural code and basic variables, it is time to move on to understand object oriented programming in order to make the most of the power of a high level language like C#.

To fulfill our tutoring mission of online education, our college homework help and online tutoring centers are standing by 24/7, ready to assist college students who need homework help with all aspects of C-family programming.  Our computer science tutors can help with all your projects, large or small, and we challenge you to find better online C-family programming tutoring anywhere.

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR REQUEST
For faster response, you may skip assigning directly to a tutor to receive the first tutor available.
That tutor may not be available for several hours. Please try another tutor if you're in a hurry.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

Thank You
Thank you!

Your request has been submitted!
A tutor will contact you soon!

  • 1
  • 2
  • 3
Get help from a qualified tutor
Live Chats