Thursday, March 27, 2008
Visual Studio 2008 IDE C# - UK Launch Notes
- VS08 allows seamless (re)targeting of apps to .NET 2.0, 3.0 and 3.5. This provides graceful upgrading of existing applications. Applications initially targeted for 3.x can also be targeted to .NET 2.0 as long as features of 3.x have not been used.
- Properties have now been improved so the implementation of the get and set methods do not need to be provided if the default implementation is all that is required. Eg./
public string FirstName { get; set; }This creates a hidden private member variable. - It is now possible to use braces to initialise multiple object properties:
Thread myThread = new Thread(MyThreadMethod)
{
Name="my thread",
IsBackground=true
}; - Anonymous types can be created using the 'var' keyword. This is NOT weak typing - C# knows what the type is and it cannot change. Anonymous types are used in instances where the developer does not know the type, the primary instance of this being LINQ. Eg./
var anonobject = new {Name = "Bob", Age = "40"}; - Anonymous methods are best described in detail here:
http://msdn.microsoft.com/msdnmag/issues/04/05/C20/#S5 - Partial methods can be used as a lightweight event system in cases where a large number of objects are held in memory (IE. LINQ) or where the method may be optionally implemented. Eg./
partial class MyClass
{
MyClass()
{
DisplayMessage(); // Only called if definition exists
}
static partial DisplayMessage(); // Not implemented
}
partial class MyClass
{
static partial DisplayMessage() // Optional
{
Console.WriteLine("MyClass created");
}
} - Extension methods allow you to 'tack on' functionality to classes that you would otherwise not be able to due to inheritance being prevented (IE. .NET classes). Eg./
public static class MyExtensions
{
public static bool
IsValidEmailAddress(this string s) // "this" keyword
{
Regex regex =
new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
return regex.IsMatch(s);
}
}
public class MyClass
{
public void MyMethod()
{
string email = "paul@here.com";
// Extension method used like a standard string method
if (email.IsValidEmailAddress())
{
Console.WriteLine("Valid!");
}
}
}
C# Programming - Part 2 - Conditions
Scope of work
The work is split into two parts: the first is a training part where you are encouraged to use books and the Internet to help you complete the questions; the second is a test part where you should complete the questions with no external help. The pass score is 80%.
1. if, else if, else
2. Nested conditions
3. && and
4. switch statement
Training Questions
Create console applications to:
1. Read a number from the console (use ReadLine and Int32.Parse) and using if, else if and else output one of the following
“The number is negative”
“The number is lower than 100”
“The number is 100 or above”
2. Read two numbers from the console (one after the other) and by nesting if, else if and else output one of the following:
“The numbers are the same”
“The numbers are not the same”
– the first is lower than the second”
“The numbers are not the same
– the second is lower than the first”
3. Read two numbers from the console (one after the other) and using if, else if and else along with && output one of the following:
“The numbers are the same and are negative”
“The numbers are the same and are positive”
“The numbers are not the same”
4. Read a number from the console (use ReadLine and Int32.Parse) and using a switch statement with cascading, and depending on an input 1-10 output one of the following:
“You entered an even number” or
“You entered an odd number” or
“You entered a number that is not 1-10”
5. Read a number from the console (use ReadLine and Int32.Parse) and by nesting if, else if and else output one of the following depending on what range the number is in, and whether it is divisible by 5:
“The number is 0-25 and is divisible by 5” or
“The number is 0-25 and is not divisible by 5” or
“The number is 26-50 and is divisible by 5” or
“The number is 26-50 and is not divisible by 5” or
“The number is 51-75 and is divisible by 5” or
“The number is 51-75 and is not divisible by 5” or
“The number is above 75”
* Note. To check if a number is divisible by 5 you can use:
(number % 5 == 0)
Test Questions
Create console applications to:
1. Read two numbers from the console (one after the other) and using if, else if and else output one of the following:
“The first number is lower than the second”
“The second number is lower than the first”
“The numbers are the same”
2. Read two numbers from the console (one after the other) and by nesting if, else if and else output one of the following:
“Both numbers are negative”
”Both numbers are positive”
“The first number is positive, the second is negative”
“The second number is positive, the first is negative”
3. Read a letter from the console and by if, else if and else with (or) output one of the following:
“1” (if the letter is a, b or c)
“2” (if the letter is d, e or f)
“3” (if the letter is g, h or i)
“4” (if the letter is j, k or l)
4. Read a number from the console (use ReadLine and Int32.Parse) and using a switch statement with cascading, and depending on an input 1-9 output one of the following:
“You entered the number one, two or three” or
“You entered the number four, five or six” or
“You entered the number seven, eight or nine” or
“You entered a number that is not 1-9”
5. Read three numbers from the console (one after the other) and by nesting if, else if and else sort the three numbers and output them in order:
Example: input numbers: 2 5 and 1
Output: 1, 2, 5
Example: input numbers 100, 45, 30
Output: 30, 45, 100
The work is split into two parts: the first is a training part where you are encouraged to use books and the Internet to help you complete the questions; the second is a test part where you should complete the questions with no external help. The pass score is 80%.
1. if, else if, else
2. Nested conditions
3. && and
4. switch statement
Training Questions
Create console applications to:
1. Read a number from the console (use ReadLine and Int32.Parse) and using if, else if and else output one of the following
“The number is negative”
“The number is lower than 100”
“The number is 100 or above”
2. Read two numbers from the console (one after the other) and by nesting if, else if and else output one of the following:
“The numbers are the same”
“The numbers are not the same”
– the first is lower than the second”
“The numbers are not the same
– the second is lower than the first”
3. Read two numbers from the console (one after the other) and using if, else if and else along with && output one of the following:
“The numbers are the same and are negative”
“The numbers are the same and are positive”
“The numbers are not the same”
4. Read a number from the console (use ReadLine and Int32.Parse) and using a switch statement with cascading, and depending on an input 1-10 output one of the following:
“You entered an even number” or
“You entered an odd number” or
“You entered a number that is not 1-10”
5. Read a number from the console (use ReadLine and Int32.Parse) and by nesting if, else if and else output one of the following depending on what range the number is in, and whether it is divisible by 5:
“The number is 0-25 and is divisible by 5” or
“The number is 0-25 and is not divisible by 5” or
“The number is 26-50 and is divisible by 5” or
“The number is 26-50 and is not divisible by 5” or
“The number is 51-75 and is divisible by 5” or
“The number is 51-75 and is not divisible by 5” or
“The number is above 75”
* Note. To check if a number is divisible by 5 you can use:
(number % 5 == 0)
Test Questions
Create console applications to:
1. Read two numbers from the console (one after the other) and using if, else if and else output one of the following:
“The first number is lower than the second”
“The second number is lower than the first”
“The numbers are the same”
2. Read two numbers from the console (one after the other) and by nesting if, else if and else output one of the following:
“Both numbers are negative”
”Both numbers are positive”
“The first number is positive, the second is negative”
“The second number is positive, the first is negative”
3. Read a letter from the console and by if, else if and else with (or) output one of the following:
“1” (if the letter is a, b or c)
“2” (if the letter is d, e or f)
“3” (if the letter is g, h or i)
“4” (if the letter is j, k or l)
4. Read a number from the console (use ReadLine and Int32.Parse) and using a switch statement with cascading, and depending on an input 1-9 output one of the following:
“You entered the number one, two or three” or
“You entered the number four, five or six” or
“You entered the number seven, eight or nine” or
“You entered a number that is not 1-9”
5. Read three numbers from the console (one after the other) and by nesting if, else if and else sort the three numbers and output them in order:
Example: input numbers: 2 5 and 1
Output: 1, 2, 5
Example: input numbers 100, 45, 30
Output: 30, 45, 100
Saturday, March 22, 2008
C# Programming - Part 1 - Strings
As languages become more high-level it seems training material focuses more on dragging and dropping components and interacting with the IDE than teaching fundamentals like problem solving and creating logical procedural code. From this deficit I created a set of C# Programming tasks that concentrate on problem solving and creating logical code. This work is made available by kind approval of Siemens AG Industry.
The tasks are provided free of charge but may NOT be copied or redistributed. The work is copyright and all rights are reserved.
Scope of work
The work is split into two parts: the first is a training part where you are encouraged to use books and the Internet to help you complete the questions; the second is a test part where you should complete the questions with no external help. The pass score is 80%.
1. Manipulating strings
2. Substrings
3. Upper/lower case
4. Concatenation
5. Locating a character in a string
6. Replacing characters in a string
Training Questions
Create console applications to:
1. Store your name as a string literal and output it to screen.
2. Read a string from the console and output it in upper case.
3. Read a name from the console (for example "Bob") and output the following:
"Hello, Bob!"
4. Use the following string:
string telephone = "0800 123 4567";
and replace the space with a - so that the output is:
0800-123-4567
5. Use the following string:
string alphabet = "abcdefghijklmnopqrstuvwxyz";
and output six letters starting from e, so that the output is:
efghij
6. Create a simple CSV (comma separated values) parser capable of dealing with a string read in from the console. The string will always be in the format:
firstvalue,secondvalue,thirdvalue
Some example strings:
"1,2,3"
"aa,bb,cc"
"apple,banana,pear"
The output (using the third string as an example) should be output to screen as follows:
First value: apple
Second value: banana
Third value: pear
Test Questions
Create console applications to:
1. Store your name in a string and output it to the screen in upper case.
2. Use the following strings:
string countryCode = “44”;
string areaCode = “1260”;
string number = “123456”
along with string literals to output:
+44-1260-123456
to screen
3. Use a string as follows:
string test = "siemens automation and drives";
Replace every i character with a 1 and then output the string in upper case as so:
S1EMENS AUTOMAT1ON AND DR1VES
4. Parse the following string:
string surnameforename = "Siemens, Werner";
so the forename appears before the surname as follows:
"Werner Siemens"
store it in a string variable and output it to screen.
5. Read in a string from the console that will always be in the following format:
+
This should be done using the substring function.
For example:
12+4
Where the example would output:
First number: 12
Second number: 4
The tasks are provided free of charge but may NOT be copied or redistributed. The work is copyright and all rights are reserved.
Scope of work
The work is split into two parts: the first is a training part where you are encouraged to use books and the Internet to help you complete the questions; the second is a test part where you should complete the questions with no external help. The pass score is 80%.
1. Manipulating strings
2. Substrings
3. Upper/lower case
4. Concatenation
5. Locating a character in a string
6. Replacing characters in a string
Training Questions
Create console applications to:
1. Store your name as a string literal and output it to screen.
2. Read a string from the console and output it in upper case.
3. Read a name from the console (for example "Bob") and output the following:
"Hello, Bob!"
4. Use the following string:
string telephone = "0800 123 4567";
and replace the space with a - so that the output is:
0800-123-4567
5. Use the following string:
string alphabet = "abcdefghijklmnopqrstuvwxyz";
and output six letters starting from e, so that the output is:
efghij
6. Create a simple CSV (comma separated values) parser capable of dealing with a string read in from the console. The string will always be in the format:
firstvalue,secondvalue,thirdvalue
Some example strings:
"1,2,3"
"aa,bb,cc"
"apple,banana,pear"
The output (using the third string as an example) should be output to screen as follows:
First value: apple
Second value: banana
Third value: pear
Test Questions
Create console applications to:
1. Store your name in a string and output it to the screen in upper case.
2. Use the following strings:
string countryCode = “44”;
string areaCode = “1260”;
string number = “123456”
along with string literals to output:
+44-1260-123456
to screen
3. Use a string as follows:
string test = "siemens automation and drives";
Replace every i character with a 1 and then output the string in upper case as so:
S1EMENS AUTOMAT1ON AND DR1VES
4. Parse the following string:
string surnameforename = "Siemens, Werner";
so the forename appears before the surname as follows:
"Werner Siemens"
store it in a string variable and output it to screen.
5. Read in a string from the console that will always be in the following format:
+
This should be done using the substring function.
For example:
12+4
Where the example would output:
First number: 12
Second number: 4
Subscribe to Posts [Atom]