If you want to write a simple C# program or utility with 50-100 lines of code, for example, a program that printing “Hello World” on screen, set the date of a file or simple math quiz program (like the one below) you do not need to download any version of Visual Studio.
This article teaches you how to compile C# program using the C# compiler that comes with Windows 8. In order to demo this I have return a simple math quiz program.
You can copy the code below to compile in your Windows 8.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConAppMathWin8
{
classProgram
{
staticvoid Main(string[] args)
{
//Declare for the ramdon number
byte[] bytes1 = newbyte[50];
int result = 0;
int choose = 0;
float answer = 0;
float[] ansCollection = newfloat[5];
Random rnd1 = newRandom();
//Init the random number
rnd1.NextBytes(bytes1);
//Selct a choose
Console.WriteLine(“========Play math========”);
Console.WriteLine(“1. Add”);
Console.WriteLine(“2. Subtract”);
Console.WriteLine(“3. Multiply”);
Console.WriteLine(“4. Divide”);
Console.Write(“Choose: “);
//Get the user input for choose
try
{
choose = int.Parse(Console.ReadLine());
}
catch (Exception ex)
{
Console.WriteLine(“Error: {0}”, ex.ToString());
}
for (int i = 0; i < 5; i++)
{
switch(choose)
{
case 1:
Console.Write(“What is the answer of {0} + {1} = “, bytes1[i], bytes1[i + 5]);
//Storing result into array
ansCollection[i] = Add(bytes1[i], bytes1[i + 5]);
break;
case 2:
Console.Write(“What is the answer of {0} – {1} = “, bytes1[i], bytes1[i + 5]);
//Storing result into array
ansCollection[i] = Subtract(bytes1[i], bytes1[i + 5]);
break;
case 3:
Console.Write(“What is the answer of {0} x {1} = “, bytes1[i], bytes1[i + 5]);
//Storing result into array
ansCollection[i] = Multiply(bytes1[i], bytes1[i + 5]);
break;
case 4:
Console.Write(“What is the answer of {0} / {1} = “, bytes1[i], bytes1[i + 5]);
//Storing result into array
ansCollection[i] = Divide(bytes1[i], bytes1[i + 5]);
break;
}
//Get input from player
try
{
answer = float.Parse(Console.ReadLine());
}
catch (Exception ex)
{
Console.WriteLine(“Error: {0}”, ex.ToString());
}
//Add to counter if the answer is correct
if (answer == ansCollection[i])
result++;
}
Console.WriteLine(“******** Answer ************”);
for (int i = 0; i < 5; i++)
{
//write answer
Console.WriteLine(“Answer {0}: {1}”, i, ansCollection[i]);
}
Console.WriteLine(“%%%%%%%%%%%%%%%%%%%%”);
//Write total correct answer(s)
Console.WriteLine(“You got {0} correct!”, result);
Console.ReadLine();
}
//Add 2 numbers
publicstaticint Add(int i1, int i2)
{
return (i1+i2);
}
//Subtract 2 numbers
publicstaticint Subtract(int i1, int i2)
{
return (i1-i2);
}
//Multiply 2 numbers
publicstaticint Multiply(int i1, int i2)
{
return (i1*i2);
}
//Divide 2 numbers
publicstaticfloat Divide(int i1, int i2)
{
return (i1/i2);
}
}
}
1. Copy the code above (if you do not have your own C# code).
2. Paste the above code to notepad and save as math.cs (if you copy the code).
3. Press Windows key and type cmd.
4. Go to the folder that you safe your file with cs extension, in our case math.cs.
5. Type c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc math.cs.
csc is the C# compiler.
6. You will find math.exe is generated.
7. Type the program name, math.exe, then the program will run.