Download - Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman
![Page 1: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/1.jpg)
![Page 2: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/2.jpg)
Computing with C#and the .NET Framework
Chapter 2
C# Programming Basics
©2003, 2011 Art Gittleman
![Page 3: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/3.jpg)
Variables
A variable represents a storage location.
Every variable has a name and a type.
int age = 19;
Name is “age” Type is “int”
Value is 19.
age 19
![Page 4: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/4.jpg)
Identifiers
An identifier names program elements.
C# is case sensitive.
Digits may occur, but cannot be the first character.
Identifiers may use the underscore character.
Keywords are identifiers reserved for special uses.
![Page 5: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/5.jpg)
abstract as base bool break byte case catch char checkedclass const continue decimal defaultdelegate do double else enumevent explicit extern false finallyfixed float for foreach gotoif implicit in int interfaceinternal is lock long namespacenew null object operator outoverride params private protected publicreadonly ref return sbyte sealedshort sizeof stackalloc static stringstruct switch this throw truetry typeof uint ulong uncheckedunsafe ushort using virtual voidvolatile while
![Page 6: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/6.jpg)
Some valid C# identifiers are: savings textLabel rest_stop_12 B3 _test My____my
Some invalid identifiers are: 4you // Starts with a number x<y // Includes an illegal character, < top-gun // Includes an illegal character, - int // Reserved keyword
![Page 7: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/7.jpg)
The character set
The ASCII character set contain 128 printing and nonprinting characters including upper and lower case letters, digits, and punctuation.
C# uses Unicode with contains thousands of characters including ASCII.
For English ASCII is sufficient
![Page 8: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/8.jpg)
Type int
A type defines the values a variable can have.
Type int represents a range of integer values from
int.MaxValue -2,147,483,648
to
int.MinValue 2,147,483,647.
![Page 9: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/9.jpg)
Assignment
Initialize a variable once
int age = 19;
Assign values during runtime
The assignment operator is the equals sign, =
age = 10;
The value is stored in the memory location allocated for that variable.
![Page 10: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/10.jpg)
19
10
20
a. int age =19; b. age = 10; c. age = 20;
![Page 11: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/11.jpg)
Constructing a program
Define a class (used later to define object types)
Include a method that contains executable code
The Main method starts execution. It must have the static modifier to be accessible before any objects are created.
![Page 12: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/12.jpg)
public class AssignIt { public static void Main( ) { int number1 = 25; int number2 = 12; number2 = number1 + 15; System.Console.WriteLine ("Number2 is now {0}", number2); }}// Output -- Number2 is now 40
Example 2.3
![Page 13: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/13.jpg)
UML (Unified Modeling Language)
Standard notation for object-oriented design
Class diagram has three parts
Upper section – class name
Middle section – data fields
Lower section – methods
Underline static methods
AssignIt
Main
![Page 14: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/14.jpg)
Constants
const int FIXED = 25;
Cannot assign to a constant. We use upper case names for constants to remind us.
![Page 15: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/15.jpg)
Input from the keyboard
System.Console.Write("Enter your name: "); // displays a prompt to the userString name = System.Console.ReadLine(); // reads the line the user enters // saves it as a string of charactersint number = int.Parse
(System.Console.ReadLine()); // converts input to an integer
![Page 16: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/16.jpg)
Using
The statement
using System;
Allows the code to refer to classes in the System namespace with the explicit prefix. We can replace
System.Console.ReadLine();
with
Console.ReadLine();
![Page 17: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/17.jpg)
Formatting output
{0:C} formats output as currency, dollars and cents in the US.
{0, 10} right-aligns the output in a 10 character field
{0, -10} left-aligns the output in a 10 character field
![Page 18: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/18.jpg)
Arithmetic Operators
Integer data produces integer results.
Binary operators, +, -, *, /, %
two operands
Unary operators +, -
one operand
![Page 19: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/19.jpg)
Operation Math notation C# (constants) C# (variables)
Addition a + b 3 + 4 score1 + score2 Subtraction a - b 3 - 4 bats - gloves Multiplication ab 12 * 17 twelve * dozens Division a/b 7 / 3 total / quantity Remainder r in a=qb+r 43 % 5 cookies % people Negation -a -6 -amount
Figure 2.9 C# arithmetic operations
![Page 20: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/20.jpg)
Operator Precedence
Evaluate 3 + 4 * 5Which operator gets its operands first?
has higher precedence than +so 3 + 4 * 5 = 3 + 20 = 23Avoid parenthesesWith parentheses 3 + (4 * 5) = 23 but (3 + 4) * 5 = 7 * 5 = 35
![Page 21: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/21.jpg)
3 + 4 * 5
Figure 2.10 Multiplication gets its operands first
![Page 22: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/22.jpg)
* 5(3 + 4)
Figure 2.11 Compute within parentheses first
![Page 23: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/23.jpg)
Precedence Table
Higher precedence-, + Unary Negation and Plus*, /, % Multiplication, Division, Remainder+, - Binary Addition and Subtraction= Assignment
Lower precedence
See Appendix for full table
![Page 24: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/24.jpg)
Combining Assign and Increment
Postincrement – evaluate then add 1 x=5 x++ evaluates to 5 and x becomes 6Preincrement – add 1 then evaluate x=5 ++x, x becomes 6 then ++x value is 6 Postdecrement – evaluate then subtract 1 x=5 x-- evaluates to 5 and x becomes 4Predecrement – subtract 1 then evaluate x=5 --x, x becomes 4 and --x value is 4
![Page 25: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/25.jpg)
Figure 2.12 Expression a) and equivalent expression b)
then
3 + x
x++
a. b.
3 + x++
![Page 26: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/26.jpg)
Figure 2.13 Expression a) and equivalent expression b)
then
++x
3 + x
a. b.
3 + ++x
![Page 27: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/27.jpg)
Methods
A method can contain the code for an operation. public static int MultiplyBy4(int aNumber) {
return 4*aNumber;}Parameters pass data to the methodreturn statement specifies the return value, if
any
![Page 28: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/28.jpg)
Argument 5
multiplyBy4
Return value 20
![Page 29: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/29.jpg)
Pass By Value
C#passes arguments by value by default
The called method receives the value of the argument not the location
The value from the caller gets copied to the parameter which is like a local variable of the method
PassByValue.cs illustrates
value of argument x copied to aNumber
![Page 30: Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman](https://reader035.vdocuments.us/reader035/viewer/2022062304/56649f395503460f94c56bd7/html5/thumbnails/30.jpg)
Figure 2.17 Memory usage
cube aNumber
12x
value
0
After the call
12
17280
12 then 17
During the call
12
1728
main
result
Before the call to cube(x)