220 likes | 316 Views
SWE 344 Internet Protocols & Client Server Programming. Getting Started with C#. 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.
E N D
SWE 344 Internet Protocols & Client Server Programming Getting Started with C#
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).
Basics of .NET The Common Language Runtime (CLR) environment
C# Program structure • C# program has 4 primary elements: • Namespace declaration • Class • Main method • Program statement • // Namespace DeclarationusingSystem;// Program start classclassClass name{// Main begins program execution.staticvoid Main() {Program statements; }}
A Simple C# Program • // Namespace Declarationusing System;// Program start classclassWelcomeCSS{// Main begins program execution. static void Main(string[] args) {// Write to consoleConsole.Write("Welcome to Hail University"); • Console.WriteLine(“……. Welcome"); • Console.WriteLine(“Computer Science and Software Engineering"); • // Keep screen from going away • Console.ReadLine(); }} Welcome to Hail University……Welcome Computer Science and Software Engineering
Notes about the program: • 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.
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;
Example: Boolean Type 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(); } } } The value of x = True The value of y = False
Operators, Types, and Variables Integral Types
Operators, Types, and Variables Floating Point and Decimal Types
Operators, Types, and Variables • 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 String Type
Operators, Types, and Variables C# Operators These expressions are built by combining variables and operators together into statements.
Example: C# Binary Operators using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespace ConsoleApplication3 { classProgram { staticvoid Main(string[] args) { int x, y, result; floatfloatresult; 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 • 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(); • } • } x+y: 12 x-y: 2 x*y: 35 x/y: 1 x/y: 1.4 x%y: 2 result+=x: 9
Operators, Types, and Variables The Array Type • 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: • varVariableName = new DataType[Number];
Operators, Types, and Variables The Array Type 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]; DataType[] VariableName = new DataType[Number]; 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; varVariableName = new DataType[Number];
Example: The Array Type using System; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; namespace ConsoleApplication3 { classProgram { staticvoid Main(string[] args) { int[] myInts = { 5, 10, 15 }; bool[][] myBools = newbool[2][]; myBools[0] = newbool[2]; myBools[1] = newbool[1]; double[,] myDoubles = newdouble[2, 2]; string[] myStrings = newstring[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] = "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(); • } • } • }
Example: The Array Type 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