German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Fundementals of Variables, Data Types, Functions, Classes, Construtors & Console Application

by Omerkamal Jun 20, 2006
This Tutorials covers fundementals of Variables, Data types, Functions, Construtors, Classes and Console Programming - Reader level: Biginner.

We need to learn some basic things to go next. You will find that Our method to learn C# is very practical. We did not start with learning basic programming but we just jumped into what can be very interesting for us. ( if you have know how about data types skip the following and just go ahead for the "Practicle") 

I think, for us is more easy to stay in touch with programming when we can feel more creativity for what we doing. I am sure you do know some basics of the Programming But just for the sake of revision we are going to repeat some fundamentals.

If you are good in Maths and especially Algebra then you do know about variables and constants. So, what is variable? It’s a symbol to which we can assign any type of value. In programming languages variable have some basic types which are called "date types".

"Data type" specifies the type of the variable. Every type of variable can hold its own type of data only. Data have basic two types Word/Sentence and a Number. Then there are many sub categories. But, We will only dive-in to the depth we need for our next step.

1. "string" holds a sentence or a word.

2. int only holds integers.

Now we will see how to declare and store data in a variable.

e.g.

 string mystring="Hello world";

String at the start means: its a string type of data. mystring is its name and you assigned "Hello world" to it. So how we will declare an integer data type?

int myint=124;

You will notice that We used ";" after the declaration. You always put this semicolon when ever a statement is written in C#. This way Compiler knows where your statement ends.

Data types:

The following list shows the list of data types available in C# and their corresponding class/struct in .NET class library.


Data type Mapped to
sbyte System.SByte
byte System.Byte
char System.Char
float System.Single
decimal System.Decimal
double System.Double
ushort System.UInt16
short System.Int16
uint System.UInt32
int System.Int32
ulong System.UInt64
long System.Int64
bool System.Boolean
string System.String
object System.Object

Practical:

Open the program “HelloWorld”. Doc the Solution Explorer Bar. Now you can see a list of the folders and files Visual Studio generated for you. You need to worry about any thing still. You will learn step-by-step every thing what you can see now.

Right click "Form1" and Click “View Code”. Now you can see the code behind your Form. In first view you can see that it contains list of “using” statements and a Class. Using statement includes the code files for you so you can easily right code ( For more Explanation see Namespaces in .NET).

Virtually thinking, this Class ( "Form1")is actually your Form you saw earlier. It Creates the Form the way you want to. We will discuss about class in next steps. Soon you will be able to create your own classes and manipulate them.

Add some code to your class now. Change text of the “Label1” Text Property. Your Class will look like this now.

namespace HelloWorld{

public partial class Form1 : Form{

public Form1(){

         InitializeComponent();

         label1.Text = "From Class";

       }

    }

}

“Namespace” is name of your Program. In one application you can have different namespaces. Every block starts with “{“ and ends with “}”. More over, blocks are always Concentric i.e they can’t overlap, the block starts 1st will end in the last.

In the class you notice another block? This is called a function or method. This function name is the same as Class. It’s called special function and named as “Class Constructor”. you only need a class constructor if you want to initialize some properties or variables when a Class is construted.

Example of a Constructor;

Class myClass{

private string myString="I am here to learn";

public int Stringlength;

   myClass(){

   Stringlength=myString.Length;

   }

}

Class myNextClass{

static void Main(){

myClass mClass=new myClass();

// here the value of Stringlength is assigned automatically to length of the string 

Console.WriteLine("My string length="+mClass.Stringlength);

      }

} 

Output:

My string length= 18

Above small application is a console program. You can select between winodws application or Console application when you "Create New Project" in Visual Stodio .NET.

You Noticed "static void Main" Function. This is the entry point for Console type of applications. Windows type Applications use "Program" Class as an entry point ( Point from where the application starts running). where, If create a Console application you will notice that your Cosole application is itself encapsulate in the "Program" Class.

 

Console Application:

To Create a Console Application repeat 3 and 4 as First Step VC# . In 5 Select Cosole Application instead. Name the Application "ConsoleApplication1" ( which is by default).

Our console application with above Classes would look like the following:

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

namespace ConsoleApplication1 {
class Program {

static void Main(
string[] args) {
myClass mClass=new myClass();
Console.
WriteLine("My string length="+mClass.Stringlength);
Console.Read();

// so the console wait for you to enter some charecter instead of hiding it self
}

class myClass {
private string
myString="I am here to learn";
public int Stringlength;
public myClass(){
Stringlength=myString.Length;
         }

      }
   }
}

Visitors/Readers Comments
(for questions please use The Forum)



"Be the First to Comment!"


Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)