Using Exception Handling
using System;
using System.Collections;
class ExceptionsSample
{
static void Main( string[] args )
{
try
{
// Create a container to hold employees
Employees employees = new Employees(4);
// Add some employees
addOneEmployee ( employees, "Timothy", "Arthur",
"Tucker", "555-55-5555" );
addOneEmployee ( employees, "Sally", "Bess",
"Jones", null );
addOneEmployee ( employees, "Jeff", "Michael",
"Simms", "777-77-7777" );
addOneEmployee ( employees, "Janice", "Anne",
"Best", "9888-88-88889" );
// Display the employee list on screen
foreach ( Employee employee in employees )
{
string name = employee.FirstName + " " +
employee.MiddleName + " " + employee.LastName;
string ssn = employee.SSN;
Console.WriteLine( "Name: {0}, SSN: {1}", name, ssn );
}
}
catch ( Exception exception )
{
// Display any errors on screen
Console.WriteLine( exception.Message );
}
}
// Helper method to add an employee to the list
static void addOneEmployee( Employees employees,
string FirstName, string MiddleName, string LastName,
string SSN )
{
bool addedEmployee = false;
try
{
Console.WriteLine( "Adding an employee" );
if ( SSN == null )
throw new ArgumentNullException( "SSN is null!" );
if ( SSN.Length != 11 )
throw new ArgumentOutOfRangeException(
"SSN length invalid!" );
Showing posts with label C# Structure. Show all posts
Showing posts with label C# Structure. Show all posts
Sunday, April 11, 2010
C# control Statement
Using the while Statement
The while statement is also used to loop through a series of statements until a test
Boolean expression evaluated at the beginning of the loop is false.The following
code has the same result as the previous for statement example:
int i = 0;
while ( i < 5 )
{
Console.WriteLine( "I will not talk in class" );
i++;
}
Using the do while Statement
The do while statement is also used to loop through a series of until a test
Boolean expression evaluated at the end of the loop is false.Therefore, the series
of statements contained within the do while loop will always execute at least once:
int i = 6;
do
{
Console.WriteLine( "I will not talk in class" );
i++;
}
while ( i < 5 );
Using the break Statement
The break statement exits the loop of a for, while, or do while statement regardless
of value of the test Boolean expression. In each of the following examples, the
WriteLine method will execute two times:
int j = 0;
for ( int i = 0; i < 5; i++ )
{
Console.WriteLine( "I will not talk in class" );
j++;
if ( j == 2 )
The while statement is also used to loop through a series of statements until a test
Boolean expression evaluated at the beginning of the loop is false.The following
code has the same result as the previous for statement example:
int i = 0;
while ( i < 5 )
{
Console.WriteLine( "I will not talk in class" );
i++;
}
Using the do while Statement
The do while statement is also used to loop through a series of until a test
Boolean expression evaluated at the end of the loop is false.Therefore, the series
of statements contained within the do while loop will always execute at least once:
int i = 6;
do
{
Console.WriteLine( "I will not talk in class" );
i++;
}
while ( i < 5 );
Using the break Statement
The break statement exits the loop of a for, while, or do while statement regardless
of value of the test Boolean expression. In each of the following examples, the
WriteLine method will execute two times:
int j = 0;
for ( int i = 0; i < 5; i++ )
{
Console.WriteLine( "I will not talk in class" );
j++;
if ( j == 2 )
Explaining Control Structures of C#
Using the if-else Statement
The if-else statement adds a path for the false evaluation of the Boolean expression.
int i = 3;
int j = 0;
int k = 0;
if ( i > 2 )
{
j = 3;
}
else
{
j = 4;
k = 5;
}
The if-else statement adds a path for the false evaluation of the Boolean expression.
int i = 3;
int j = 0;
int k = 0;
if ( i > 2 )
{
j = 3;
}
else
{
j = 4;
k = 5;
}
Subscribe to:
Posts (Atom)
-
In object-oriented languages the term "interface" is often used to define an abstract type that contains no data but exposes behav...
-
The following article discusses the WPF command binding feature with relation to Mouse clicks. One of WPF powerful features is the bindin...