Download presentation
Presentation is loading. Please wait.
Published byBrianne Cook Modified over 9 years ago
1
1
2
2
3
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Decisions { class Program { static void Main(string[] args) { Console.WriteLine("Please enter something and press Enter"); string userInputValue; userInputValue = Console.ReadLine(); //Reading User input Console.WriteLine("You entered {0}", userInputValue); Console.ReadLine(); //WAITING } 3
4
static void Main(string[] args) { Console.WriteLine("Please 1 or 2"); string userInputValue; userInputValue = Console.ReadLine(); //READING USER INPUT if (userInputValue == "1") { Console.WriteLine("You entered ONE"); } else if (userInputValue == "2") { Console.WriteLine("You entered TWO"); } else { Console.WriteLine("You entered SOMETHING ELSE"); } Console.ReadLine(); //WAITING } 4
5
static void Main(string[] args) { Console.WriteLine("Please 1 or 2"); string userInputValue; userInputValue = Console.ReadLine(); //READING USER INPUT string message; if (userInputValue == "1") { message = "You entered ONE"; } else if (userInputValue == "2") message = "You entered TWO"; else { message = "You entered SOMETHING ELSE"; } Console.WriteLine(message); Console.ReadLine(); //WAITING } 5 No {} Only if it is 1 line of code, but not recommended
6
6
7
7
8
8
9
== (test equality) - results true or false = (assignment) 9
10
if (condition 1 ) { One line does no need {} but use them anyway } else if (condition 2 ) { } else { } 10
11
11
12
DateTime dt = new DateTime(2012, 1, 17, 9, 30, 0); string city = "Chicago"; int temp = -16; string output = String.Format("At {0} in {1}, the temperature was {2} degrees.", dt, city, temp); Console.WriteLine(output); // The example displays the following output: // At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees. 12
13
C# Primitives TypePrimitiveUsageRange boolSystem.Booleanbooleantrue, false byteSystem.Byte8 bit integer0 - 255 charSystem.Char16 bit Unicode character/u0000 - /uffff decimalSystem.Decimal128 bit decimal +/-1.0x10-28 to +/-7.9x10+28 precision of 28-29 digits doubleSystem.Double64 bit floating point -1.79769313486232e308 to 1.79769313486232e308 floatSystem.Single32 bit floating point +/-1.5x10-45 to +/-3.4x10+38 precision of 7 digits intSystem.Int3232 bit integer-2,147,483,648 to 2,147,483,647 longSystem.Int6464 bit integer -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 sbyteSystem.SByte8 bit integer-128 to 127 shortSystem.Int1616 bit integer-32,768 to 32,767 stringSystem.String-immutable, specified length uintSystem.UInt3232 bit unsigned integer0 to 4,294,967,295 ulongSystem.UInt6464 bit unsigned integer0 to 18,446,744,073,709,551,615 ushortSystem.UInt1616 bit unsigned integer0 to 65,535 13
14
shortcut 14
15
15
16
16
17
Prefix and Postfix Operators 17
18
18
19
19
20
string message = (userInputValue == “1”)? “You entered ONE”:”You entered SOMETHING ELSE”; 20
21
21
22
22
23
By default int type, can be changed 23
24
later 24
25
25
26
26
27
In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property No need for getX and setX 27
28
// This class is mutable. Its data can be modified from outside the class. class Customer { // Auto-Implementated Properties for trivial get and set public double TotalPurchases { get; set; } public string Name { get; set; } public int CustomerID { get; set; } // Constructor public Customer(double purchases, string name, int ID) { TotalPurchases = purchases; Name = name; CustomerID = ID; } // Methods public string GetContactInfo() {return "ContactInfo";} public string GetTransactionHistory() {return "History";} //.. Additional methods, events, etc. } class Program { static void Main() { // Intialize a new object. Customer cust1 = new Customer ( 4987.63, "Northwind",90108 ); //Modify a property cust1.TotalPurchases += 499.99; } 28
29
29
30
Local variables 30
31
31
32
32
33
33
34
3 phases 34
35
35
36
36
37
37
38
// determine the average of an arbitrary number of grades public void DetermineClassAverage() { int total; // sum of grades int gradeCounter; // number of grades entered int grade; // grade value double average; // number with decimal point for average // initialization phase total = 0; // initialize total gradeCounter = 0; // initialize loop counter // processing phase // prompt for and read a grade from the user Console.Write( "Enter grade or -1 to quit: " ); grade = Convert.ToInt32( Console.ReadLine() ); // loop until sentinel value is read from the user while ( grade != -1 ) { total = total + grade; // add grade to total gradeCounter = gradeCounter + 1; // increment counter // prompt for and read the next grade from the user Console.Write( "Enter grade or -1 to quit: " ); grade = Convert.ToInt32( Console.ReadLine() ); } // end while // termination phase // if the user entered at least one grade... if ( gradeCounter != 0 ) { // calculate the average of all the grades entered average = ( double ) total / gradeCounter; // display the total and average (with two digits of precision) Console.WriteLine( "\nTotal of the {0} grades entered is {1}", gradeCounter, total ); Console.WriteLine( "Class average is {0:F}", average ); } // end if else // no grades were entered, so output error message Console.WriteLine( "No grades were entered" ); } // end method DetermineClassAverage } Avoid infinite loop! 38
39
39
40
Explicitly and Implicitly Converting Between Simple Types To perform a floating-point calculation with integer values, we temporarily treat these values as floating-point numbers. A unary cast operator such as (double) performs explicit conversion. C# performs an operation called promotion (or implicit conversion) on selected operands for use in the expression. 40
41
The cast operator is formed by placing parentheses around the name of a type. This operator is a unary operator (i.e., an operator that takes only one operand). Cast operators associate from right to left and have the same precedence as other unary operators, such as unary + and unary -. This precedence is one level higher than that of the multiplicative operators *, / and %. In this app, the three grades entered during the sample execution of class GradeBookTest (Fig. 5.10) total 263, which yields the average 87.66666…. The format item rounds the average to the hundredths position, and the average is displayed as 87.67. 41
42
42
43
43
44
44
45
45
46
46
47
C# statically types at compile time. After variable declaration the type is set Conversion: ◦ Implicit (when it is safe) From smaller to larger ( int to double ) From derived to base class ◦ Explicit (cast) Variables compatible, but risk of precision loss (smaller to larger) ◦ Using helpers (between non-compatible types) System.BitConverter System.Convert Int32.Parse The Convert.ToInt32(String, IFormatProvider) underneath calls the Int32.Parse. So the only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException. MSDNMSDN 47
48
48
49
49
50
50
51
// Fig. 5.16: Increment.cs // Prefix increment and postfix increment operators. using System; public class Increment { public static void Main( string[] args ) { int c; // demonstrate postfix increment operator c = 5; // assign 5 to c Console.WriteLine( c ); // display 5 Console.WriteLine( c++ ); // display 5 again, then increment Console.WriteLine( c ); // display 6 Console.WriteLine(); // skip a line // demonstrate prefix increment operator c = 5; // assign 5 to c Console.WriteLine( c ); // display 5 Console.WriteLine( ++c ); // increment then display 6 Console.WriteLine( c ); // display 6 again } // end Main } 51
52
52
53
53
54
The table in Appendix B, Simple Types, lists the 13 simple types in C#. C# requires all variables to have a type. Instance variables of types char, byte, sbyte, short, ushort, int, uint, long, ulong, float, double, and decimal are all given the value 0 by default. Instance variables of type bool are given the value false by default. 54
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.