Presentation is loading. Please wait.

Presentation is loading. Please wait.

Getting Started with C# 1 SWE 344 Internet Protocols & Client Server Programming.

Similar presentations


Presentation on theme: "Getting Started with C# 1 SWE 344 Internet Protocols & Client Server Programming."— Presentation transcript:

1 Getting Started with C# 1 SWE 344 Internet Protocols & Client Server Programming

2 Outlines: Understand the basic structure of a C# program. Obtain a basic familiarization of what a "Namespace" is. Obtain a basic understanding of what a Class is. Learn what a Main method does. Learn how to obtain command-line input. Learn about console input/output (I/O). 2

3 Basics of.NET The Common Language Runtime (CLR) environment 3

4 C# Program structure  C# program has 4 primary elements: Namespace declaration Class Main method Program statement // Namespace Declaration using System; // Program start class class Class name { // Main begins program execution. static void Main() { Program statements; } } 4

5 C# Program structure 5

6 // Namespace Declaration using System; // Program start class class WelcomeCSS { // Main begins program execution. static void Main(string[] args) { // Write to console Console.Write("Welcome to Hail University"); Console.WriteLine(“……. Welcome"); Console.WriteLine(“Computer Science and Software Engineering"); // Keep screen from going away Console.ReadLine(); } } A Simple C# Program Welcome to Hail University……Welcome Computer Science and Software Engineering 6

7 The Main method specifies its behavior with the Console.WriteLine(...) statement. Console is a class in the System namespace. WriteLine(...) is a method in the Console class. We use the ".", dot, operator to separate subordinate program elements. Note that we could also write this statement as System.Console.WriteLine(...). This follows the pattern "namespace.class.method. Observe that comments are marked with "//". These are single line comments, meaning that they are valid until the end-of-line. If you wish to span multiple lines with a comment, begin with "/*" and end with "*/". Everything in between is part of the comment. Comments are ignored when your program compiles. They are there to document what your program does. All statements end with a ";", semi-colon. Classes and methods begin with "{", left curly brace, and end with a "}", right curly brace. Any statements within and including "{" and "}" define a block. Blocks define scope (or lifetime and visibility) of program elements. Notes about the program: 7

8 Operators, Types, and Variables "Variables" are simply storage locations for data. You can place data into them and retrieve their contents as part of a C# expression. The interpretation of the data in a variable is controlled through "Types". The Boolean Type Boolean types are declared using the keyword, bool. They have two values: true or false. bool x= true; bool y= false; 8

9 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { bool x = true; bool y = false; Console.WriteLine(“The value of x = {0}", x); Console.WriteLine(" The value of y = {0}", y); Console.ReadLine(); } Example: Boolean Type The value of x = True The value of y = False 9

10 Integral Types Operators, Types, and Variables TypeSize (in bits)Range sbyte8-128 to 127 byte80 to 255 short16-32768 to 32767 ushort160 to 65535 int32-2147483648 to 2147483647 uint320 to 4294967295 long64 -9223372036854775808 to 9223372036854775807 ulong640 to 18446744073709551615 char160 to 65535 10

11 Floating Point and Decimal Types Operators, Types, and Variables Type Size (in bits) precisionRange float327 digits 1.5 x 10 -45 to 3.4 x 10 38 double6415-16 digits 5.0 x 10 -324 to 1.7 x 10 308 decimal128 28-29 decimal places 1.0 x 10 -28 to 7.9 x 10 28 11

12 String Type A string is a sequence of text characters. We typically create a string with a string literal, enclosed in quotes: “Hail University”. Some characters aren't printable, but you still need to use them in strings. Therefore, C# has a special syntax where characters can be escaped to represent non-printable characters. Operators, Types, and Variables 12

13 Escape Sequence Meaning \'Single Quote \"Double Quote \\Backslash \0Null, not the same as the C# null value \aBell \bBackspace \fform Feed \nNewline \rCarriage Return \tHorizontal Tab \vVertical Tab String Type Operators, Types, and Variables 13

14 C# Operators These expressions are built by combining variables and operators together into statements. Operators, Types, and Variables Category (by precedence) Operator(s) Associativi ty Unary+ - ! ~ ++x --x left Multiplicative* / %left Additive+ -left Shift >left Relational = is asleft Equality== !=right Logical AND&left Logical XOR^left Logical OR|left Conditional AND&&left Conditional OR||left Assignment = *= /= %= += - = >= &= ^= |= => right 14

15 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { int x, y, result; float floatresult; x = 7; y = 5; result = x + y; Console.WriteLine("x+y: {0}", result); result = x - y; Console.WriteLine("x-y: {0}", result); Example: C# Binary Operators 15

16 result = x * y; Console.WriteLine("x*y: {0}", result); result = x / y; Console.WriteLine("x/y: {0}", result); floatresult = (float)x / (float)y; Console.WriteLine("x/y: {0}", floatresult); result = x % y; Console.WriteLine("x%y: {0}", result); result += x; Console.WriteLine("result+=x: {0}", result); Console.ReadLine(); } Example: C# Binary Operators x+y: 12 x-y: 2 x*y: 35 x/y: 1 x/y: 1.4 x%y: 2 result+=x: 9 16

17 The Array Type Operators, Types, and Variables Array data type can be thought of as a container that has a list of storage locations for a specified type. When declaring an Array, specify the type, name, dimensions, and size. An array is considered a reference type. Therefore, an array requests its memory using the new operator. Based on this, one of the formulas to declare an array is: DataType[] VariableName = new DataType[Number]; Alternatively, you can use the var keyword to create an array. The formula to use would be: var VariableName = new DataType[Number]; 17

18 The Array Type Operators, Types, and Variables long[] ShelfNumbers = new long[10]; string[] Titles = new string[10]; string[] Directors = new string[10]; int[] Lengths = new int[10]; string[] Ratings = new string[10]; double[] Prices = new double[10]; var Numbers = new double[5]; Numbers[0] = 12.44; Numbers[1] = 525.38; Numbers[2] = 6.28; Numbers[3] = 2448.32; Numbers[4] = 632.04; DataType[] VariableName = new DataType[Number]; var VariableName = new DataType[Number]; 18

19 Example: The Array Type using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { int[] myInts = { 5, 10, 15 }; bool[][] myBools = new bool[2][]; myBools[0] = new bool[2]; myBools[1] = new bool[1]; double[,] myDoubles = new double[2, 2]; string[] myStrings = new string[3]; Console.WriteLine("myInts[0]: {0}, myInts[1]: {1}, 19

20 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] = "Hail"; myStrings[1] = "is my"; myStrings[2] = "City"; Console.WriteLine("myStrings[0]: {0}, myStrings[1]: {1}, myStrings[2]: {2}", myStrings[0], myStrings[1], myStrings[2]); Console.ReadLine(); } 20

21 output: myInts[0]: 5, myInts[1]: 10, myInts[2]: 15 myBools[0][0]: true, myBools[1][0]: true myDoubles[0, 0]: 3.147, myDoubles[1, 0]: 56.00138917 myStrings[0]: Hail, myStrings[1]: is my, myStrings[2]: City Example: The Array Type 21

22 22 END


Download ppt "Getting Started with C# 1 SWE 344 Internet Protocols & Client Server Programming."

Similar presentations


Ads by Google