Microsoft Visual C#

Implementation of programming language C#

Microsoft Visual C# is a reference implementation of C#, included in Visual Studio.

Examples:

Hello, World!:

Example for versions Microsoft Visual C# 2008, gmcs 2.0.1
using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");
    }
}

Factorial:

Example for versions Microsoft Visual C# 2008, gmcs 2.0.1

This example uses recursive factorial definition.

using System;

class Program
{
    static long Factorial(int n)
    {
        if (n == 0)
            return 1;
        else
            return n * Factorial(n - 1);
    }
    static void Main(string[] args)
    {
        for (int i = 0; i < 17; i++)
            Console.WriteLine("{0}! = {1}",i,Factorial(i));
    }
}

Fibonacci numbers:

Example for versions Microsoft Visual C# 2008, gmcs 2.0.1

This example uses recursive definition of Fibonacci numbers.

using System;

class Program
{
    static long Fibonacci(int n)
    {
        if (n < 3)
            return 1;
        else
            return Fibonacci(n - 1) + Fibonacci(n - 2);
    }
    static void Main(string[] args)
    {
        for (int i = 1; i < 17; i++)
            Console.Write("{0}, ", Fibonacci(i));
        Console.WriteLine("...");
    }
}

Quadratic equation:

Example for versions Microsoft Visual C# 2008, gmcs 2.0.1
using System;

class Program
{
    static void Main(string[] args)
    {
        int A, B, C, D;
        try
        {   Console.Write("A = ");
            A = Convert.ToInt32(Console.ReadLine());
            Console.Write("B = ");
            B = Convert.ToInt32(Console.ReadLine());
            Console.Write("C = ");
            C = Convert.ToInt32(Console.ReadLine());
        }
        catch
        {   Console.WriteLine("Invalid input");
            return;
        }
        if (A == 0)
        {   Console.WriteLine("Not a quadratic equation.");
            return;
        }
        D = B * B - 4 * A * C;
        if (D == 0)
            Console.WriteLine("x = {0}", -B / 2.0 / A);
        else if (D > 0)
            Console.WriteLine("x1 = {0}\nx2 = {1}", (-B + Math.Sqrt(D)) / 2 / A, (-B - Math.Sqrt(D)) / 2 / A);
        else
            Console.WriteLine("x1 = ({0},{1})\nx2 = ({0},-{1})", -B/2.0/A, Math.Sqrt(-D)/2/A);
    }
}