C# Primer

About C#

C# is one of the programming languages designed for the Common Language Infrastructure. C# is built on the syntax and semantics of C++, allowing C programmers to take advantage of .NET and the common language runtime.
http://en.wikipedia.org/wiki/C_Sharp_(programming_language)

Download C#

The fastest way to start developing applications for Windows 8.1, Windows Phone, the web or the cloud. Visual Studio Express products provide a free development environment to develop applications for the latest platforms. Since it is Visual Studio, applications created in Express can be supported in other versions of Visual Studio as your needs grow.
http://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx

Starting C#

Learn how easy it is to set up Visual Studio and connect to Visual Studio Online. Once you’re set up, we’ll show you how to create and organize your backlog, share your code with your team, and configure your app for continuous builds and deployment. Choose any of the sections below to get started.
http://www.visualstudio.com/get-started/overview-of-get-started-tasks-vs

Hello World!

The following procedure creates a C# version of the traditional “Hello World!” program. The program displays the string Hello World!
http://msdn.microsoft.com/en-us/library/k1sx6ed2.aspx

To create and run a console application

  1. Start Visual Studio.
  2. On the menu bar, choose File, New, Project.
  3. The New Project dialog box opens.
  4. Expand Installed, expand Templates, expand Visual C#, and then choose Console Application.
  5. In the Name box, specify a name for your project, and then choose the OK button.
  6. The new project appears in Solution Explorer.
  7. If Program.cs isn’t open in the Code Editor, open the shortcut menu for Program.cs in Solution Explorer, and then choose View Code.
  8. Replace the contents of Program.cs with the following code:
// A Hello World! program in C#.
using System;
namespace HelloWorld
{
    class Hello
    {
        static void Main()
        {
            Console.WriteLine("Hello World!");

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}
"Hello World!"

Variables

The C# typing system contains the following categories: Value types, Reference types, and Pointer types.
Variables that are value types store data, and those that are reference types store references to the actual data. Reference types are also referred to as objects. Pointer types can be used only in unsafe mode.
It is possible to convert a value type to a reference type, and back again to a value type, by using boxing and unboxing. With the exception of a boxed value type, you cannot convert a reference type to a value type.
This section also introduces void.
Value types are also nullable, which means they can store an additional non-value state. For more information, see Nullable Types.
http://msdn.microsoft.com/en-us/library/3ewxz6et.aspx

// A Hello World! program in C#.
using System;
namespace HelloWorld
{
    class Hello
    {
	string message = "Hello World!";
        static void Main()
        {
            Console.WriteLine(message);

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}
"Hello World!"

Functions

A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method. The Main method is the entry point for every C# application and it is called by the common language runtime (CLR) when the program is started.
http://msdn.microsoft.com/en-us/library/ms173114.aspx

using System;
namespace HelloWorld
{
	class Hello
	{
		string message = "Hello World!";
		static void Main()
        	{
			// Call the method
			WriteMessage()

			// Keep the console window open in debug mode.
			Console.WriteLine("Press any key to exit.");
			Console.ReadKey();
        	}

		static void WriteMessage()
		{
			Console.WriteLine(message);
		}
	}
}
"Hello World!"

Passing Variables to Functions

Previously, message was accessible because its accessibility level is private (default). Meaning it is accessible from within the Hello class. In this example, we will pass a string value to a method as a parameter.

using System;
namespace HelloWorld
{
    class Hello
    {
        static void Main()
        {
            // Call the method
            WriteMessage("Hello World!")

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }

		static void WriteMessage(string message)
		{
			Console.WriteLine(message);
		}
    }
}
"Hello World!"

Error Handling

A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.
http://msdn.microsoft.com/en-us/library/dszsf989.aspx

using System;
namespace HelloWorld
{
    class Hello
    {
        static void Main()
        {
            try
			{
				// Call the method
				WriteMessage("Hello World!");
			}
			catch (Exception e)
			{
				// Write exception to console
                Console.WriteLine(e.Message);
			}
            finally
			{
				// Keep the console window open in debug mode.
				Console.WriteLine("Press any key to exit.");
				Console.ReadKey();
			}
        }

        static void WriteMessage(string message)
        {
            Console.WriteLine(message);
        }
    }
}
"Hello World!"

Further Reading

C# Programming Guide

  • Most of this section assumes that you already know something about C# and general programming concepts. If you are a complete beginner with programming or with C#, you might want to visit the C# Developer Center, where you can find many tutorials, samples and videos to help you get started.

.Net Pearls

  • Documentation intended to be as simple as possible. Shows examples and performance(speed, memory, etc.).

Related Reading

StackOverflow

  • If you have a question, then chances are someone else did to. Chances are if you search you question on Goggle, you will get a StackOverflow result.