Download presentation
Presentation is loading. Please wait.
Published byJoan Robertson Modified over 9 years ago
1
C# C1 CSC 298 Elements of C# code (part 1)
2
C# C2 Style for identifiers Identifier: class, method, property (defined shortly) or variable names class, method, property: capitalize the first letter of each word in the name, e.g. class ThisIsMyClass variable: same except for the first letter which is lowercase, e.g. myVariable constant: capitalize every letter. Write an underscore between the words in the name MAX_VALUE (this style is not always followed)
3
C# C3 Common types Similar list as in C/C++ and Java int for an integer (-1, 3, …) float or double for a floating point value (1.2f, -4.24, …) Careful: double x = 4. // error: need 4.0 char for a character ('F', '@', …) bool for boolean values (true or false) Others (but we won't use them much): byte, sbyte, short, ushort, uint, long, ulong
4
C# C4 Other common types string: for text string s = "Hello, world!"; The string type has many useful methods (Substring, StartsWith,…). Check the documentation. Note that a string is immutable. decimal: for precise manipulation of floating point values (e.g. for currency amounts). decimal d = 1.34224m;
5
C# C5 const keyword Sometimes, a variable should not change (e.g. a mathematical constant such as pi) To make a variable constant, use const when declaring it const double PI=3.1416;... PI = 4.0; // Error // PI is a constant All constants are implicitly static (one copy shared by all instances of the class).
6
C# C6 Doing Arithmetic Usual arithmetic operations: *, /, + and – Between integers, / is the integer division 27/12 is 2 (not 2.25) % : a%b is the remainder of the division of a by b, e.g. 27%12 is 3 since 12 27 2 3 49%5 is 4 2%3 is 2 6.2%2.9 is 0.4
7
C# C7 An example Input 2 integers and test if the first one is a multiple of the second one input: use Console.ReadLine, which gives back a string translate the string to an int with int.Parse test : if (i1%i2==0)… display the result with MessageBox.Show See CommonTypeExample.cs on the web site
8
C# C8 Casting Consider double x = 3.8; int i = x; // Error! Convert to an int using a cast, i.e. write int i = (int)x; // OK, i is 3 C# is strongly typed. It only performs safe automatic conversions (int to double…). Other conversions are the programmer's responsibility. In general, sometype var; var = (sometype)expression; /* not always allowed. The compiler will tell you (e.g. bool b = (bool)3.4 is an error) */
9
C# C9 Formatting output Use placeholders {} to write values within a string when using Console.Write (or WriteLine) double x = 3.41, y = 5.6; Console.Write("x={0}, y={1}, x<y is {2}", x, y, x<y); // prints x=3.41, y=5.6, x<y is True The placeholder can contain display information, e.g. Console.Write("x={0:E}",x); //prints x=3.410000E+000 Many other options: check the documentation (search for String.Format method)
10
C# C10 string formatting A good trick: string filename = @"C:\temp\myFile.txt"; // @ means that \ doesn't mark escape // characters as in \n \t string.Format to get a formatted string. double x = 3.14; string s = string.Format("x={0}",x); // s is x=3.14 // s = "x=" + x; is also OK (but // formatting can't be controlled as well)
11
C# C11 Types in C# Every property, method and variable has a type 2 categories value types, for simple objects. An example is an integer value (int). reference (or class) types, e.g. string
12
C# C12 Defining your own type Value type: use a struct Reference type: use a class How to choose? if the type is lightweight (contains only simple data), use a struct. If not, use a class. Differences? In terms of programming, not many: a struct can have fields, methods, properties (static or not) like a class. small differences, e.g. a struct has always a default constructor (though you can't write it). The compiler will tell you these details. Major difference is how the type is handled by the CLR.
13
C# C13 struct ≠ class a value type object (=an instance of a struct) is stored on the stack of the method that uses it. a reference type object (=an instance of a class) is defined on the heap. stack: defined by the compiler. The size of every object that goes on the stack must be known at compile time. heap: managed at run time Accessing the stack is more efficient than accessing the heap.
14
C# C14 Example public struct PointS { public int x, y;} public class PointC { public int x, y;} // in some method PointS ps = new PointS(); PointC pc = new PointC(); ps x 0 y 0 pc x 0 y 0 stack heap address of the PointC object on the heap
15
C# C15 ref keyword (1) Arguments in a method call are passed by value public class MyClass{ public void Frodo(int n){n++;} } // in some method of some other class MyClass c = new MyClass(); int j = 10; c.Frodo(j); Console.WriteLine(j); // What is printed? Answer: 10 (not 11!). Why? Because Frodo works on a copy of j What if we want Frodo to change j? Use the ref keyword.
16
C# C16 ref keyword (2) public class MyClass{ public void Frodo(ref int n){n++;} } // in some method of some other class MyClass c = new MyClass(); int j = 10; c.Frodo(ref j); Console.WriteLine(j); // What is printed? Answer: 11. Now, Frodo works directly with j (the variable is called n in the context of Frodo, but it refers to the same memory location as j). A detail: a variable passed by reference (with ref) must be initialized before being passed. Use the out keyword if you don't want to initialize the variable (see documentation).
17
C# C17 C# aliases The types available in C# are defined by the CLR. C# offers the option to use C# specific names for some (of the more common) types. e.g. int for the CLR defined Int32, string for the CLR defined String, short for the CLR defined Int16, etc… you can use either notation in your programs int.Parse(...) is the same as Int32.Parse(...)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.