380 likes | 607 Views
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
E N D
BIM313 – Advanced Programming Techniques C# Basicsand 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 • 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
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
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
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
Integer Types u: unsigned s: signed
Precisions The decimal value type is generally used in currencies which require more precision!
Other Simple Types Note that char type is stored in 2 bytes and it is Unicode!
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.
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
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.
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
Example: Valid Variable Names • myBigVar • VAR1 • _test • i • myVariable • MyVariable • MYVARIABLE
Example: Invalid Variable Names • a+b • 99bottles • namespace • double • my-result
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.
Hungarian Notation • Place a lowercase prefix which shows the type of the variable • nAge • iAge • fDelimeter • btnClick • txtName
Camel Case • Begin first word with lowercase, others with uppercase • age • firstName • lastName • birthDay
Pascal Case • Start all words with uppercase letters • Age • FirstName • LastName • WinterOfDiscontent
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”
Variable Declaration and Assignment • int age; • age = 25; • int age = 25; • int xSize, ySize; • int xSize = 4, ySize = 5; • int xSize, ySize = 5;
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
Mathematical Operators % : Remainder operator Example: 8 % 3 gives 2.
Increment and Decrement Operators Increment first, assign next Assign first, increment next
Exercise int var1, var2 = 5, var3 = 6; var1 = var2++ * --var3; Console.WriteLine("var1={0}, var2={1}, var3={2}", var1, var2, var3); How?
Printing Variable Values int var1 = 3, var2 = 5; Console.WriteLine("var1 = {0}, var2 = {1}", var1, var2); var1 = 3, var2 = 5
Printing Variable Values int var1 = 3, var2 = 5; Console.WriteLine("{0}{1}{0}{1}{1}", var1, var2); 35355
Reading Strings string userName; Console.Write("Your name: "); userName = Console.ReadLine(); Console.WriteLine("Welcome {0}!", userName); Your name: Muzaffer Welcome Muzaffer!
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());
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());
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