Thursday, 17 April 2014

c#- Numeric Data Types

Strong and Weak Typed Languages

C# is a strongly typed language. All variables in a program must be declared as being of a specific type. The variable's behaviour is defined by the chosen type. For example, an integer variable can only contain whole numbers. If a number in an integer variable needs a fractional part, it must first be converted to a different type, possibly being stored in a new variable as a part of the translation.
The alternative to a strongly typed language is a weakly typed language. An example would be VBScript used by many classic ASP developers. In VBScript, the type of the variable is not declared and the behaviour of the variable may appear to change from one line of code to the next.

Variable Declaration and Assignment

A variable can be declared with one line of code, specifying the variable type and its name. In the following code, an integer variable is declared using the data type int.
 int numberofArticles;
A variable can be given a value using the assignment operator, (=). The variable to the left of the operator is assigned the value to the right. The following code shows a variable being declared and assigned a value.
int numberofArticles;
numberofArticles=3;
You do not need to assign a value to a new variable immediately. There may be many lines of code between the declaring a variable and giving it a value. However, if you do wish to declare a variable and assign a value at the same time, this can be achieved in a single statement. For example:
int numberofArticles=3; 
It is possible to declare multiple variables of the same type in a single line of code. You can also assign the same value to multiple variables in one statement. To complicate things further (or to show the elegance of C#, depending on your viewpoint), these operations can be combined. The following code shows three examples.
NB: Multiple variables can be assigned the same value in this manner because the assignment operation returns the value that has been assigned.

Assignment Problems

Definite Assignment

The C# compiler enforces a rule known as definite assignment. This states that a variable may not be read until a value has been assigned. This prevents you from writing code that reads a variable with an undefined value, as this could give unpredictable results.
The following code fails to compile, displaying the error "Use of unassigned variable numberOfArticles".


  











 

 

 

   

Numeric Data Type Reference

I will end this article with a quick reference to the numeric data types. For integer types, the declaration keyword, a description of the type, the range of possible values and the number of bits used to represent the value are given. For non-integers, the scale and the number of digits of accuracy are given, rather than minimum and maximum. This allows you to determine which data type should be used for any numeric variable.
The Boolean type is included, which although not technically numeric, fits well in this table. The Boolean type can hold either true or false. It is named after the mathematician, George Boole.

c# comment

How to Comment
Now that we have investigated why and what to comment, we need to know how to comment. There are three types of comment that C# can include. They are single line, trailing and multi-line comments.
Single Line Comments
The simplest form of comment in C# is the single line comment. The line is started with a double forward slash, (//). Any text or code following the double slash is ignored during compilation.

Hello world


Console vs Windows vs Web Application
The .NET framework, combined with C#, allows you to develop several types of software. These include Windows-based programs, web-based applications and even console applications for execution from the command prompt. In this article, we will create a console application similar to the original Hello World program. A console application requires little supporting code and gives the clearest opportunity for understanding the language.
Creating the Program
Step 1 - Create a Console Application
Console Application IconI will assume that you are using Microsoft Visual Studio or Microsoft C# Express Edition to develop the program. Launch your chosen development environment and create a new project of the type, "Console Application". Name the application, "HelloWorld".
Step 2 - Add the Functionality
The development environment should have created all of the template code needed for a console application. This includes a "Main" function that is similar though not identical to the original K&R application. All that is left is for you to do is to add the code that actually prints "hello, world".
C# is derived from C but does not share all of its commands. We cannot use the printf command to output text, as this command is not supported. Instead we use the Console.WriteLine function. Add the following code between the braces {} of the Main function, ending the second line with a semicolon (;).

 

Step 3 - Test the Program

You can execute your program directly from Visual Studio or C# Express Edition by selecting "Start Without Debugging" from the Debug menu or by hitting Ctrl-F5. You should see the words, "Hello World" in a console window and an instruction to press a key to continue.
Note: You would usually start programs with debugging enabled by pressing F5. In the case of a console application, the application may close before the results are seen. Using Ctrl-F5 adds the requirement to press a key when the application stops.