Wednesday, 29 May 2013

Installing Visual Studio 2012 and starting with C#

C# is not too difficult to learn if you are familiar with the concept of object-oriented programming. When I first started learning C#, I had been programming in Java for most of my projects, with a little bit of experience in C and C++. It didn't take long to feel almost as natural as Java, and to actually appreciate some neat features it provides. 

You can start by installing an IDE for C#. If you are running Linux, you can use MonoDevelop or some other tools of your choice, but I would recommend using Visual Studio on Windows as there is a large support base and many useful extensions available. 



1. Install Visual Studio

Visual Studio Express can be downloaded from official MS website for free. You will be required to register after 30-day trial period, but you can continue to use the product for free after registration.



2. Create a solution

 Click "Create New Project" to create a solution (a collection of projects, such as Class Library or Windows application, that forms a program), select "Console Application"  and you will see there is one project (called ConsoleApplication1 by default) created with a class file called Program.cs.





3. Write the code



Now that we are ready, we can start with a "Hello World" program (as usual). You only need to add one line of code in the template created;


System.Console.WriteLine() is similar to printf() in C, or System.out.println() in Java. It simply means that 'WriteLine()' method is defined within the namespace called System.Console and need to be accessed by specifying it, unlike built-in function 'printf()' in C. Namespaces are used to organise methods into groups, or to disambiguate methods with the same name (you can think of it as a method/class group, or like packages in Java). Namespaces can be omitted by having 'using' clauses at the top instead.


Now the hello-world program is ready. Press Ctrl+F5 to build and run the project.







No comments:

Post a Comment