c# (c sharp): the programming language

0 Conversations

C# is a new programming language, with similarities to Java and initiated by Microsoft as part of its key .NET initiative. The new Visual Studio.NET package has Visual C++, C#, and Basic, with brand spanking new object-orientatedness stuffed inside. Here I shall give you the basics of C#. For any programmer fluent in other languages this should help you get a grasp of the language. If you are compiling with the .NET Framework SDK (not Visual Studio.NET) watch your casing. Console should be Console and not console. Otherwise csc will gag on you code.


Comments


delimited by //s.

// is a comment



Variables


example

int variable;

declares variable to be of type int



Common variable types

string String
int 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.

Bookmark on your Personal Space


Conversations About This Entry

There are no Conversations for this Entry

Entry

A734852

Infinite Improbability Drive

Infinite Improbability Drive

Read a random Edited Entry


Written and Edited by

Disclaimer

h2g2 is created by h2g2's users, who are members of the public. The views expressed are theirs and unless specifically stated are not those of the Not Panicking Ltd. Unlike Edited Entries, Entries have not been checked by an Editor. If you consider any Entry to be in breach of the site's House Rules, please register a complaint. For any other comments, please visit the Feedback page.

Write an Entry

"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

Write an entry
Read more