Wednesday, April 25, 2007

foreach statement

C# introduce the foreach statement, which iterates through the elements of an entire array or collection.

foreach ( type identifier in arrayName )
statement



simple declaration and output syntax

Declaring and Creating Arrays

int[] c = new int[ 12 ];

other ways:
int[] c; // declare the array variable
c = new int[ 12 ]; // create the array; assign to array variable

single lines for some variables:
string[] b = new string[ 100 ], x=newstring[ 27 ];




it's suggested that :

For readability, declare only one variable per declaration. Keep each declaration on a separate line and include a comment describing the variable being declared.

common output for printing array elements:
- using for loop
// output each array element's value \
for ( int counter = 0; counter <>
Console.WriteLine( "{0,5}{1,8}", counter, array[ counter ] );

output:

Friday, April 20, 2007

Getting user input

In c#, all input from user will be taken as string. we need to convert user input to decimal if we want to use it as a number. C# provide a nice class called Convert to deal with this.

example code:

string temp;
decimal amount;

Console.Write("account 1 deposit: "); // print question
temp=Console.ReadLine(); // get user input as string

// convert to decimal
amount = Convert.ToDecimal(temp);

// print user input with string currency specifier
Console.WriteLine("total deposit:{0:C}", amount);


output:
account 1 deposit: [500] => my input
total deposit:$500.00

formatting your text in c#

some common escape sequence / special character

Escape sequence

Description

\n

Newline. Positions the screen cursor at the beginning of the next line.

\t

Horizontal tab. Moves the screen cursor to the next tab stop.

\r

Carriage return. Positions the screen cursor at the beginning of the current linedoes not advance the cursor to the next line. Any characters output after the carriage return overwrite the characters previously output on that line.

\\

Backslash. Used to place a backslash character in a string.

\"

Double quote. Used to place a double-quote character (") in a string. For example,

Console.Write( "\"in quotes\"" );

displays

"in quotes"



And,
C# has added some nice default features that we can use to format our text/string in console.

string format specifiers.

Format Specifier

Description

C or c

Formats the string as currency. Precedes the number with an appropriate currency symbol ($ in the US). Separates digits with an appropriate separator character (comma in the US) and sets the number of decimal places to two by default.

D or d

Formats the string as a decimal. Displays number as an integer.

N or n

Formats the string with commas and a default of two decimal places.

E or e

Formats the number using scientific notation with a default of six decimal places.

F or f

Formats the string with a fixed number of decimal places (two by default).

G or g

General. Formats the number normally with decimal places or using scientific notation, depending on context. If a format item does not contain a format specifier, format G is assumed implicitly.

X or x

Formats the string as hexadecimal.



example code 1:
Console.Write( "Welcome\nto\nC#\nProgramming!" );

output:
welcome
to
C#
Programming!

example code 2, we will use default string formating ( without string specifier):

Console.WriteLine( "{0}\n{1}", "Welcome to", "C# Programming!" );


output:
Welcome to
C# Programming!

example code 3, this program will show u how to create time format of hh:mm:ss

int hour = 4;
int min = 30;
int sec =23;
Console.WriteLine("time: {0:D2}:{1:D2}:{2:D2}",hour,min,sec);

output:
time: 04:30:23

example code 4, this code will use the string fromat specifier C for currency

Console.WriteLine( "account1 balance: {0:C}", 1000 );

output:
account1 balance: $1,000.00

it's cool, isn't it?

hello world!

1st ex code: of course it’s hello world!


1 // Welcome1.cs
2 // Text-printing application.
3 using System;
4
5 public class Welcome1
6 {
7 // Main method begins execution of C# application
8 public static void Main( string[] args )
9 {
10 Console.WriteLine( “Hello world!” );
11 } // end method Main
12 } // end class Welcome1