Thursday 12 December 2013

Home  Contents

C#

In this part of the C# tutorial, we will introduce the C# programming language.

Goal

The goal of this tutorial is to get you started with the C# programming language. The tutorial covers the core of the C# language, including variables, arrays, control structures and other core features. This tutorial uses command line compilers to build applications. It does not cover graphical interface development, or visual IDEs.

C#

C# is a modern, high-level, general-purpose, object-oriented programming language. It is the principal language of the .NET framework. It supports functional, procedural, generic, object-oriented, and component-oriented programming disciplines. The design goals of the language were software robustness, durability and programmer productivity. It can be used to create console applications, GUI applications, web applications, both on PCs and embedded systems. C# was created by Microsoft corporation. The name "C sharp" was inspired by musical notation where a sharp indicates that the written note should be made a semitone higher in pitch.
C# is a very popular language. Currently, it is one of the top 10 popular languages in the world. It was created on the Windows platform. The Mono project has created a clone for the Linux and Mac platforms. C# is a compiled language. The source code is compiled into executable (.exe) files which are executed by the .NET platform.

.NET

The .NET framework is an application software platform from Microsoft, introduced in 2002 and commonly called .NET ("dot net"). It uses an intermediate bytecode language that can be executed on any hardware platform that has a runtime engine. Programs written for the .NET Framework execute in a software environment, known as the Common Language Runtime (CLR). Common Language Runtime is an application virtual machine that provides services such as security, memory management, and exception handling. The class library and the CLR together constitute the .NET Framework. The .NET framework supports several programming languages. In addition to C#, development is possible in Visual Basic .NET, managed C++, F#, IronPython or IronRuby.

Compiling

There are two prominent C# compilers. The Microsoft C# compiler and the Mono C# compiler.
using System;

public class Simple 
{
    static void Main() 
    {
        Console.WriteLine("This is C#");
    }
}
This is a simple C# program that prints a message to the console.
On Linux, we need to install the Mono C# compiler. It is called gmcs for C# 3.0 profile and dmcsfor the C# 4.0 profile. To install the Mono C# compiler, we must install the Mono platform.
$ dmcs simple.cs
$ ./simple.exe 
This is C#
We compile and run a simple C# program on Linux.
On Windows, we have two basic options. We either use the command line compiler or some version of the Visual Studio. The .NET framework is already installed on many versions of Windows OS. If not, we can download it and install it from the microsoft.com website.
On our system, the .NET framework was installed on C:\WINDOWS\Microsoft.NET\Framework\v3.5. Here we find the csc.exe file which is the compiler of the C# language.
C:\programming\csharp>C:\WINDOWS\Microsoft.NET\Framework\v3.5\csc.exe simple.cs
C:\programming\csharp>simple.exe
This is C#
We compile and run a simple C# program on Windows.
Another option is to use a freely available Visual Studio C# Express Edition. We create a new project by selecting File/New Project or clicking Ctrl+N. From the available templates, we select a console application. (All programs in this tutorial are console programs.)
Console application
Figure: Console application
To run an example, we click the Ctrl + F5 key combination.

Sources

The following sources were used to create this tutorial:
In this part of the C# tutorial, we have introduced the C# language.
'm currently developing an ASP.NET MVC4 application which have 3 roles: admin, manager and editor. The editors can CRUD articles in the system BUT the editors can only read, update or delete their OWN articles. I've seen I can control the access to the controller and action by adding:
[Authorize(Roles="editor")]
but it only restricts the role not the information.
If the editor A created the article 1 ONLY the editor A has access to that article. No other role has access to the controller or the information. What would be the best practice in order to restrict the access by role and by context?