Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing with C# and the .NET Framework

Similar presentations


Presentation on theme: "Computing with C# and the .NET Framework"— Presentation transcript:

1 Computing with C# and the .NET Framework
C# Programming Basics

2 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.

3 abstract as base bool break
byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using virtual void volatile while

4 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

5 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

6 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.

7 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.

8 a. int age =19; b. age = 10; c. age = 20;

9 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.

10 public class AssignIt {
public static void Main( ) { int number1 = 25; int number2 = 12; number2 = number ; System.Console.WriteLine ("Number2 is now {0}", number2); } // Output -- Number2 is now 40

11 Constants const int FIXED = 25;
Cannot assign to a constant. We use upper case names for constants to remind us.

12 Input from the keyboard
System.Console.Write("Enter your name: "); // displays a prompt to the user String name = System.Console.ReadLine(); // reads the line the user enters // saves it as a string of characters int number = int.Parse (System.Console.ReadLine()); // converts input to an integer

13 Using System 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();

14 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

15 Arithmetic Operators Integer data produces integer results.
Binary operators, +, -, *, /, % two operands Unary operators +, - one operand

16 Operation Math notation C# (constants) C# (variables)
Addition a + b score1 + score2 Subtraction a - b bats - gloves Multiplication ab * twelve * dozens Division a/b / total / quantity Remainder r in a=qb+r % cookies % people Negation a amount C# arithmetic operations

17 Operator Precedence Evaluate 3 + 4 * 5
Which operator gets its operands first? has higher precedence than + so * 5 = = 23 Avoid parentheses With parentheses 3 + (4 * 5) = 23 but (3 + 4) * 5 = 7 * 5 = 35

18 Precedence Table Higher precedence -, + Unary Negation and Plus
*, /, % Multiplication, Division, Remainder +, - Binary Addition and Subtraction = Assignment Lower precedence

19 Combining Assign and Increment
Postincrement – evaluate then add 1 x=5 x++ evaluates to 5 and x becomes 6 Preincrement – 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 4 Predecrement – subtract 1 then evaluate x=5 --x, x becomes 4 and --x value is 4

20 Expression a) and equivalent expression b)
then 3 + x x++ a. b. 3 + x++ Expression a) and equivalent expression b)

21 Expression a) and equivalent expression b)
then ++x 3 + x a. b. 3 + ++x Expression a) and equivalent expression b)

22 Methods A method can contain the code for an operation.
public static int MultiplyBy4(int aNumber) { return 4*aNumber; } Parameters pass data to the method return statement specifies the return value, if any

23 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


Download ppt "Computing with C# and the .NET Framework"

Similar presentations


Ads by Google