Archive for the ‘02 – C# intro’ Category

Narrowing and widening

Sunday, March 8th, 2009

Narrowing and widening

If you know what casting is, this will be easy for you

A byte is a small integer that has a maximum value of 255, if you want to save 315 in that byte variable, you will need to widen it to an integer for example.

Date And Time data types

Sunday, March 8th, 2009

This one is so not important at this strange, i will come back to it.

Dealing with Simple Strings

Sunday, March 8th, 2009

From before, you know how to declare a string, here it is again for you

string myString;
myString = “Hi There, i am a string”;

Parsing
Assuming your string is
string myStringInteger = “10″;

and you want to get that value in an integer variable
You can simply use the parse keyword
int myInteger = int.Parse(myStringInteger);

You can do that to a boolean variable as well
string myBoolString = “True”;
bool myBool = bool.Parse(myBoolString);

Strings come with extra functionality as well.

myBoolString.Length will be equal to 4 since True has 4 letters.

Strings are no good for efficiency, rather you would want to use StringBuilder for larger texts

You may also be interested in knowing that although a string is not a value type, the == operator is overloaded to mean “Is the content identical in both strings”, If you don’t know why this is strange, You will find out as you read

Declaring Varibles

Sunday, March 8th, 2009

Declaring variables in C#

Here i am not talking about arrays or more complex structures, the basic variable declaration and initialization is as follows

Lines that start with // are not for the compiler, they are comments and effectivly ignored on compile

//Declare an integer
int myInteger;
//Give it initial value
myInteger = 15;
//Declare and Initialize @ once
int myOtherInteger = 16;
//Declare More than 1 variable at once
int var1, var2, var3;
//More on one line, Declare and initialize many
int var4 = 45, var5 = 46, var6 = 47;

Surely you can replace int with float or string or bool or any other.

Console Output with Console.WriteLine

Sunday, March 8th, 2009

So, you have already used this

But for the most part of this text we will use the functionality from System.Console class to display the output of our programs

Console.WriteLine(“A string”);

prints A string, if you want to print variables

Console.WriteLine(“Var1 {0} Var2: {1} Var4: {3} Var3: {2}”, var1, var2, var3, var4);

this will print the variables within the string, notice that Var4 and Var3 are reversed in the string, the variables takes the indexes 0, 1, 2, 3… you can use {0} more than once to print variable var1 more than once.

On a relevant note, Console.WriteLine will print the string on a new string, if you do not want that you can use Console.Write in the same way

C# rules – Starting C#

Sunday, March 8th, 2009

Ok, here is a quick intro to the language

1- C# is case sensitive, so myVariable is not the same as MyVariable
2- The entry point of the program by default returns void (You can change that into an integer if you insist on your program returning a zero for ok or an error code), it takes string[] args as parameter so that you can pass the program command line arguments
3- No globals
4- Command line arguments will be in the array, you can then foreach around them (Or use a for loop) to extract them
5- You can use GetCommandLineArgs() to get the array we talked about in 4, this method is static, you can learn what static is later

Will add things here as suitable as i go.

Your first C# program

Sunday, March 8th, 2009

To start, open your MSVS express (Free) or Pro

Go to File -> New -> Project

Chose Console Application from the icons that appear

Name your project HelloWorld

—-

You will see MSVS creating a new file for you named “Program.cs”, and in that file you should see something like.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Already we have a namespace (Same as project name), a Program Class,
and a program entry point that is always named Main.

So, Add the following line to your Main (Between the currly brackets)

Console.WriteLine(“HelloWorld”);
Console.ReadLine();

Now go to the build menu, and then to Build Solution

Now navigate to your Documents directory, In my case on Windows Vista it is
C:\Users\yazeed\Documents\Visual Studio 2008\Projects\HelloWorld\HelloWorld\bin\Debug

and double click HelloWorld.exe

bravo, you are done with your first C# program….

the C# compiler on the command line

Sunday, March 8th, 2009

You actually get the compiler when you download the .NET framework and not the Visual Studio.

You can run the compiler from the command line, So assuming you have a very simple program in myprog.cs and you want to compile it with the command line compiler, you can compile it at the command line like this.

csc /target:exe myprog.cs

there are pleanty of compiler flags and you even need to include the names of the files you want to make use of in your program, so why not skip this one and use the Microsoft IDE.

i will come back here and give you all the command line arguments and everything some time later, for now i am not using this, it involves more typing than the progam itself.