Tuesday, February 21, 2012

What is this thing called C#?

C# is a computer programming language based on C and C++. If you've ever learned either of those languages C# will fit like a glove.

C# has all the operators of C and C++.

C# does away with the header files used in C++.

Like C and C++ every line of code in C# is terminated with a semicolon.

A simple variable declaration looks like:

string aString = "some string";

A simple test looks like:

if( aString == "some string" )
{
   // do something usefull
}

A for loop looks like:

for (int i = 0; i <= 10; ++i )
{
   // do something usefull
}

If you have a collection of objects, like:

List<string> aList = new List<string>();

You can use a for each loop:

for each( string str in aList )
{
   // do something with str
}

If you have a loop of indeterminate length, you can do something like:

bool flag = false;
while( flag == false )
{
  // do something usefull

 // when done simply:
  flag = true;
}

If you have a loop that needs to always go through a least 1 iteration, you can do something like:

bool flag = false;
do while ( flag == false )
{
   // do somethig usefull

   // when done simply
   flag = true;
}

To define a namespace, you simply do this:

namespace my.nameSpace
{
   // whatever you want in that namespace
}

To define a class, you simply do this:

public class myClass
{
    // these are the class members
    private int anInt;
    private string aString;

    public myClass ( int i, string s )  // this is the class constructor, it initilizes the class members
    {
       anInt = i;
       aString = s;
    }

    public anAction()
    {
       // perform an action on the class members
    }

    public anotherAction()
   {
       // perform another action on the class members
   }
}

To use your newly defined class, you simply

using my.nameSpace;

myClass _myClass = new myClass( 1, "some string" );

_myClass.anAction();
_myClass.anotherAction();

Of course the language of C# is not hard to learn and remember, the .Net framework is the challenging part of this equation. Your best on-line resource for the .Net framework is: http://msdn.microsoft.com/en-us/library

No comments:

Post a Comment