Follow
Publications: 82 | Followers: 0

week9-C-sharp

Publish on Category: Birds 0

… and C#
COMP 205 - Week 9ChunboChu
Your First C# Program
// Namespace DeclarationusingSystem;// Program start classclassHelloCLS{// Main begins program execution.staticvoidMain(){// Write to consoleConsole.WriteLine(“Hello World!");}}
Run it!
Open your Visual C# 2008 Express Edition…Create your first Windows Console program.
Variables and Types
C# is a "Strongly Typed" languageThe C# simple types consist ofBoolean typeboolcontent =true;boolnoContent=false;The only legal values for thebooltype are eithertrueorfalse, Not 0 or other values.Three numeric types - Integrals, Floating Point, Decimal, and String.
Integral Types
A category of types:sbyte, byte, short,ushort,int, char …Whole numbers, either signed or unsigned, and the char type
Floating Point and Decimal Types
A C# floating point type is either a float or doubleRepresents a real numberDecimal types should be used when representing financial or money valuesReason : because you can avoid rounding errors.
String Type
"This is an example of a string."
Table 2-3. C# Character Escape Sequences
String
Verbatim literal, which is a string with a @ symbol prefix, as [email protected]"Some string".Escape sequences translate as normal characters to enhance readability."c:\\topdir\\subdir\\subdir\\myapp.exe“@"c:\topdir\subdir\subdir\myapp.exe"
The Array Type
A container that has a list of storage locations for a specified type.When declaring an Array, specify the type, name, dimensions, and size.
using System;class Array{public static void Main(){int[]myInts= { 5, 10, 15 };bool[][]myBools= newbool[2][]; //jagged arraymyBools[0] = newbool[2];myBools[1] = newbool[1];double[,]myDoubles= new double[2, 2]; //two dimensional arraystring[]myStrings= new string[3];Console.WriteLine("myInts[0]: {0},myInts[1]: {1},myInts[2]: {2}",myInts[0],myInts[1],myInts[2]);myBools[0][0] = true;myBools[0][1] = false;myBools[1][0] = true;Console.WriteLine("myBools[0][0]: {0},myBools[1][0]: {1}",myBools[0][0],myBools[1][0]);myDoubles[0, 0] = 3.147;myDoubles[0, 1] = 7.157;myDoubles[1, 1] = 2.117;myDoubles[1, 0] = 56.00138917;Console.WriteLine("myDoubles[0, 0]: {0},myDoubles[1, 0]: {1}",myDoubles[0, 0],myDoubles[1, 0]);myStrings[0] = "Joe";myStrings[1] = "Matt";myStrings[2] = "Robert";Console.WriteLine("myStrings[0]: {0},myStrings[1]: {1},myStrings[2]: {2}",myStrings[0],myStrings[1],myStrings[2]);}}
Control Statements - Selection
theifstatements.theswitchstatement.howbreakis used inswitchstatements.
stringmyInput;intmyInt;Console.Write("Please enter a number: ");myInput=Console.ReadLine();myInt= Int32.Parse(myInput);// Multiple Case Decisionif(myInt< 0 ||myInt== 0){Console.WriteLine("Your number {0} is less than or equal to zero.",myInt);}elseif(myInt> 0 &&myInt<= 10){Console.WriteLine("Your number {0} is in the range from 1 to 10.",myInt);}elseif(myInt> 10 &&myInt<= 20){Console.WriteLine("Your number {0} is in the range from 11 to 20.",myInt);}elseif(myInt> 20 &&myInt<= 30){Console.WriteLine("Your number {0} is in the range from 21 to 30.",myInt);}else{Console.WriteLine("Your number {0} is greater than 30.",myInt);}
In other languages, such as C and C++, conditions can be evaluated where a result of 0 is false and any other number is true.In C#, the condition must evaluate to abooleanvalue of either true or false.If you need to simulate a numeric condition with C#, you can do so by writing it as (myInt!= 0), which means that the expression evaluate to true ifmyIntis not 0.
switch
stringmyInput;intmyInt;Console.Write("Please enter a number between 1 and 3: ");myInput=Console.ReadLine();myInt= Int32.Parse(myInput);// switch with integer typeswitch(myInt){case1:Console.WriteLine("Your number is {0}.",myInt);break;case2:Console.WriteLine("Your number is {0}.",myInt);break;case3:Console.WriteLine("Your number is {0}.",myInt);break;default:Console.WriteLine("Your number {0} is not between 1 and 3.",myInt);break;}
switch (myInt){case 1:case 2:case 3:Console.WriteLine("Your number is {0}.",myInt);break;default:Console.WriteLine("Your number {0} is not between 1 and 3.",myInt);break;}
Control Statements - Loops
Learn thewhileloop.Learn thedoloop.Learn theforloop.Learn theforeachloop.Complete your knowledge of thebreakstatement.Teach you how to use thecontinuestatement.
while (<boolenexpression>) { <statements> }intmyInt= 0;while (myInt< 10){Console.Write("{0} ",myInt);myInt++;}Console.WriteLine();}
More loops
do { <statements> } (<boolenexpression>)One reason you may want to use adoloop instead of awhileloop is to present a message or menu.forloops are appropriate when you know exactly how many times you want to perform the statements within the loop.The contents within theforloop parentheses hold three sections separated by semicolons(<initializerlist>; <booleanexpression>; <iteratorlist>) { <statements> }.
for loop
initializerlist is a comma separated list of expressions.These expressions are evaluated only once during the lifetime of theforloop.Then, theforloop gives control to its second section, thebooleanexpression.When thebooleanexpression evaluates totrue, the statements within the curly braces of theforloop are executed.Control moves to the top of loop and executes theiteratorlist, which is normally used to increment or decrement a counter.
break: It simply breaks out of the loop at that point and transfers control to the first statement following the end of theforblock.continuestatement causes control to skip over the remaining statements in the loop and transfer back to theiteratorlist.By arranging the statements within a block properly, you can conditionally execute them based upon whatever condition you need.
Activity
Write a for loop that prints the even numbers between 1 and 20Add an if statement to terminate the loop when you have reached 10.for(inti=1;i<=20;i++){if(i> 10)break;if(i% 2 == 0)continue;Console.Write("{0} ",i);}
foreach
Aforeachloop is used to iterate through the items in a list.foreach(<type> <iteration variable> in <list>) { <statements> }string[] names = {"Cheryl", "Joe", "Matt", "Robert"};foreach(string person in names){Console.WriteLine("{0} ", person);}
Namespaces
theusing System;directiveProvide assistance in avoiding name clashes between two sets of code.Help you organize your programs.Your classes will be named the same as classes in either the .NET Framework Class Library or a third party library and namespaces help you avoid the problems that identical class names would cause.
// Namespace Declarationusing System;// The C# Station Namespacenamespacecsharp_class{// Program start classclassNamespaceCSS{// Main begins program execution.public static void Main(){// Write to consoleConsole.WriteLine("This is the new C# class Namespace.");}}}
Namespacesallow you to create a system to organize your code.A good way to organize yournamespacesis via a hierarchical system.You put the more general names at the top of the hierarchy and get more specific as you go down.This hierarchical system can be represented by nestednamespaces.
using System;// The C# Station Tutorial Namespacenamespacecsharp_class{namespace tutorial{// Program start classclassNamespaceCSS{// Main begins program execution.public static void Main(){// Write to consoleConsole.WriteLine("This is the new C# class Tutorial Namespace.");}}}}
Windows Forms Application
Create your first Windows Forms ApplicationConsole.WriteLine(“Hello World!");MessageBox.Show("Hello World!");Next, create your own Webbroswer!myBrowser.Navigate(URL.Text);
Method
attributes modifiers return-type method-name(parameters ){statements}

0

Embed

Share

Upload

Make amazing presentation for free
week9-C-sharp