1 / 37

BIM313 – Advanced Programming Techniques

BIM313 – Advanced Programming Techniques. C# Basics and Variables. Contents. Variables and Expressions Comments Variables Expressions Operators Namespaces. Basic C# Syntax. White spaces (space, carriage return, tab) are ignored by the C# compiler

kolton
Download Presentation

BIM313 – Advanced Programming Techniques

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BIM313 – Advanced Programming Techniques C# Basicsand Variables

  2. Contents • Variables and Expressions • Comments • Variables • Expressions • Operators • Namespaces

  3. Basic C# Syntax • White spaces (space, carriage return, tab) are ignored by the C# compiler • Statements are terminated with a semicolon (;) • C# code is case-sensitive • C# is a block-structured language and blocks are delimited with curly brackets (‘{’ and ‘}’) • Please indentyour code so that your code becomes more readable • Write commentswhile writing the codes

  4. Comments • Type I /* Single line comment */ /* Multi- Line Comment */ • Type II // Another single line comment a = 0; // Initialize the count • Type III /// Special comments used for documentation

  5. Variables • Think variables as boxesto store data in them • Variables have types, names, and values intnum = 5; • Here, int is the type, num is the name, and 5 is the value of the variable • All variables should be declaredbefore using them

  6. Simple Types • Simple types include types such as numbers and Boolean (true or false) values • There are several types to represent numbers, because different amount of bytes are required for each type

  7. Integer Types u: unsigned s: signed

  8. Floating-Point Value Types

  9. Precisions The decimal value type is generally used in currencies which require more precision!

  10. Other Simple Types Note that char type is stored in 2 bytes and it is Unicode!

  11. Variable Declaration, Assignment, and Printing Example static void Main(string[] args) { int myInteger; string myString; myInteger = 17; myString = "\"myInteger\" is"; Console.WriteLine("{0} {1}.", myString, myInteger); } Two variables are declared here Values are assigned to the variables Variables are displayed on the screen. "myInteger" is 17.

  12. Printing Variable Values • Use Console.Write() or Console.WriteLine() methods to display variable values on the screen • Console.WriteLine() method adds a new line at the end of the line • The methods have several facesto print several types; use the most suitable one

  13. Some Console.WriteLine() Faces

  14. Printing an int on the screen • int x = 17, y = 25; • Console.WriteLine(x); • Console.WriteLine(x.ToString()); • Console.Write(“x = ”); • Console.WriteLine(x); • Console.WriteLine(“x = ” + x); • Console.WriteLine(“x = ” + x.ToString()); • Console.WriteLine(“x = {0}, y = {1}.”, x, y); 17 17 x = 17 x = 17 x = 17 x = 17, y = 25.

  15. Variable Naming • The first character must be either a letter, or an underscore character (_) • Subsequent characters may be letters, underscore character, or numbers. • Reserved words can’t be used as variable names • If you want a reserved word as variable name, you can put an at character (@) at the beginning

  16. Example: Valid Variable Names • myBigVar • VAR1 • _test • i • myVariable • MyVariable • MYVARIABLE

  17. Example: Invalid Variable Names • a+b • 99bottles • namespace • double • my-result

  18. Keywords

  19. C# Contextual Keywords Contextual keyword are used in certain language constructs. They can’t be used as identifier in those constructs. Otherwise, they can be used as identifiers.

  20. Hungarian Notation • Place a lowercase prefix which shows the type of the variable • nAge • iAge • fDelimeter • btnClick • txtName

  21. Camel Case • Begin first word with lowercase, others with uppercase • age • firstName • lastName • birthDay

  22. Pascal Case • Start all words with uppercase letters • Age • FirstName • LastName • WinterOfDiscontent

  23. Escape Sequences

  24. More About Strings… • You can use Unicode values after \u • “Karli\’s string” • “Karli\u0027s string” • If you place the @ character before a string, all escape sequences are ignored. • “C:\\inetpub\\wwwroot\\” • @“C:\inetpub\wwwroot\” • “A short list:\nitem 1\nitem 2” • @“A short list: item 1 item 2”

  25. Variable Declaration and Assignment • int age; • age = 25; • int age = 25; • int xSize, ySize; • int xSize = 4, ySize = 5; • int xSize, ySize = 5;

  26. Operators • Addition, subtraction, etc. are made using operators • Three types of operators: • Unary – Act on single operand • Binary – Act on two operands • Tertiary – Act on three operands

  27. Mathematical Operators % : Remainder operator Example: 8 % 3 gives 2.

  28. Increment and Decrement Operators Increment first, assign next Assign first, increment next

  29. Exercise int var1, var2 = 5, var3 = 6; var1 = var2++ * --var3; Console.WriteLine("var1={0}, var2={1}, var3={2}", var1, var2, var3); How?

  30. Printing Variable Values int var1 = 3, var2 = 5; Console.WriteLine("var1 = {0}, var2 = {1}", var1, var2); var1 = 3, var2 = 5

  31. Printing Variable Values int var1 = 3, var2 = 5; Console.WriteLine("{0}{1}{0}{1}{1}", var1, var2); 35355

  32. Reading Strings string userName; Console.Write("Your name: "); userName = Console.ReadLine(); Console.WriteLine("Welcome {0}!", userName); Your name: Muzaffer Welcome Muzaffer!

  33. Reading Integers • int age; • Console.WriteLine("Your age: "); • age = int.Parse(Console.ReadLine()); • Console.WriteLine("Your age is {0}.", age); Equivalent code: Convert.ToInt32(Console.ReadLine());

  34. Reading Doubles • double w; • Console.WriteLine("Your weight (in kg.): "); • w = double.Parse(Console.ReadLine()); • Console.WriteLine("You weigh {0} kg.", w); Equivalent code: Convert.ToDouble(Console.ReadLine());

  35. Assignment Operators

  36. Operator Precedence

  37. Namespaces • .NET way of providing containers • Header files in C and C++ • Packages in Java • .NET classes are grouped in namespaces • Sin, Cos, Atan, Acos, Pi, Sqrt, etc. in Mathnamespace • Int32, Double, etc. in Systemnamespace • Windows Forms classes in System.Windows.Forms • Registry operations in Microsoftnamespace • You also can write your programs or DLLs in a separate namespace, e.g. using your company name

More Related