c# (c sharp): the programming language
Created | Updated Feb 19, 2003
Comments
delimited by //s.
// is a comment
Variables
example
int variable;
declares variable to be of type int
Common variable types
string Stringint Integer
long Long integer
double Floating point
decimal Works like int and double
bool Boolean
Variables can be both initialized and declared in one swoop, example
string stringvar = "blah";
Arrays
type[] var=new type[n]
type represents what the array is made up of, string, int, whatever. var represents the variable name. n is the dimension of the array.
Input/output
Printing text:
Console.WriteLine("string");
Console.WriteLine(variable);
Reading text:
string somevar=Console.ReadLine();
String to number:
//Code snippet
int a;
string b=Console.Readline()
a=Int32.Parse(b)
Common data constructs
If Then Else
Like all c# structures, delimited by { }s.
if(condition)
{
//statements
}
Conditions include
a==b a=b
a>b a>b
a>=b a greater than equal to b
a<b a < b
a<=b a less than equal to b
a!=b a not equal to b
boolean AND use (condition 1)&&(condition 2)
boolean OR use (condition 1)||(condition 2)
While
while(condition)
{
//statements
}
Do..While
do
{
//statements
}while(condition);
For loops
For loops are evaluated a bit differently than in other languages.
for(variable initial;condition;increment)
{
//statements
}
For clarity, the following c# for code line is equivalent to the next in BASIC.
for(x=1;x!=5;x=x+1)
is the same as
for x = 1 to 4
Case structures
switch(variable){
case condition1:
//statements
break;
case condition2:
//statements
break;
default:
//if none of the conditions apply above, 'otherwise'
//statements
break;
}
Foreach
foreach(type element in array)
{
//statements, eg
//tot=tot+element
}
type represents the type of what the array is made up of. array is the name of the array variable.
Sample hello world program
namespace HelloWorld
{
using System;
public class Class
{
public static int Main(string[] args)
{
string a="Hello World";
Console.WriteLine(a);
//All program should return 0
return 0;
}
}
}
And there you have it: a little introduction. I might add info wih classes and functions later, but this should get you started!
If you like what you see, head on over to http://msdn.microsoft.com/downloads and select .Net Framework SDK and download. Warning, it's big (131 M): but it's got everything you need to get started.