Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# Data type Chapter 1.

Similar presentations


Presentation on theme: "C# Data type Chapter 1."— Presentation transcript:

1 C# Data type Chapter 1

2 Most of them are same as in C++
Major C# Data Type Type Meaning bool true or false byte 8 bits long sbyte signed byte char Character int 4 bytes integer number short 2 bytes integer number long 8 bytes integer number uint unsigned integer ulong unsigned long integer float 4 bytes decimal number double 8 bytes decimal number Most of them are same as in C++

3 Some C# Math functions No Math.Random() function Function Meaning
Math.Abs(x) Absolute value of x : |x| Math.Sqrt(x) Square root of x Math.Exp(x) Exponential value ex Math.Pow(x, n) Power value xn Math.Log(x) Ln(x): Natural logarithm of x Math.Log10(x) Log10(x): logarithm of x in base of 10 Math.Ceiling(x) The smallest integer bigger or equal to x Math.Floor(x) The largest integer less or equal to x Math.Round(x) Round up integer of x Math.Sin(x) sin(x) similar for cos(x) and tan(x) Math::PI  = ….. Math.Max(x,y) Maximum of x and y Math.Min(x,y) Minimum of x and y Math.Sign(x) Sign of x No Math.Random() function

4 Random class Random class object is used to generate random integer, random floating number or random bytes. Random rnd = new Random(); // Random object rnd int n = rnd.Next(); // Random integer int k = rnd.Next()%7; // Random integer 0-6 double d = rnd.NextDouble() // Random double number between 0.0 and 1.0 Byte[] bArray = new Byte[10]; rnd.NextBytes(bArray); // Now bArray is filled with 10 Random bytes

5 Most operators in C++ are good for C#
=, +=, -=, *=, /= //assignment operators ==, != // compare operators <, <=, >, >= +, //signed operators +, -, *, /, % // math operation ++, -- <<, >> //shift operators &, |, ^ // bitwise AND, OR, XOR The following are new operators Console.Readline() // standard input Console.WriteLine() // standard output

6 Most Branch operators in C++ are also good for C#
if(x>1 && y<2){ } if(a<2 || b>3){ } for loop while loop do while loop switch break in the loop continue in the loop All those operators are exactly same as in C++ or Java Be careful that C# is case sensitive

7 String object String or string is the most important class:
string msg = new string("Hello NET"); string msg = "Hello NET"; char[] charArray = {'H', 'e', 'l', 'l', 'o', ' ', 'N', 'E', 'T'}; string msg = new string(charArray); Operators: string stringCopy(str) int CompareTo(str) int IndexOf(str) int LastIndexOf(str) string ToLower() string ToUpper() Concatenation String msg = "Good" +" Morning"; (use +)

8 Standard input and output
Standard input is: Console.Read(); Console.ReadLine(); Standard output is: string s = "Hello"; Console.ReadLine(s);

9 MessageBox.Show Other buttons: MessageBox.Show(
"Missing data input", // message string "input Error", // title caption string MessageBoxButtons.OK, // button type MessageBoxIcon.Exclamation // icon ); Other buttons: MessageBoxButtons.AbortRetryIgnore MessageBoxButtons.OK MessageBoxButtons.OKCancel MessageBoxButtons.RetryCancel MessageBoxButtons.YesNo MessageBoxButtons.YesNoCancel

10 Other icons: MessageBoxIcon.Asterisk MessageBoxIcon.Error
MessageBoxIcon.Exclamation MessageBoxIcon.Hand MessageBoxIcon.Information MessageBoxIcon.None MessageBoxIcon.Question MessageBoxIcon.Stop MessageBoxIcon.Warning

11 Return value of MessageBox
The return value type of MessageBox.Show() is DialogResult which has the following values: DialogResult.Abort DialogResult.Cancel DialogResult.Ignore DialogResult.No DialogResult.None DialogResult.OK DialogResult.Retry DialogResult.Yes

12 String Split function Basic usage is
public string[] Split(params char[]); string charStr = " ,.:"; char [] charArray = charStr.ToCharArray(); string words = "one two,three:four."; string [] split = word.Split(charArray); Then we can use foreach() to read this string array foreach (string s in split) { Console.WriteLine(s); }

13 Type converting String to integer uses Int32.Parse(str);
String str = "1234"; try { int n = Int32.Parse(str); } catch(FormatException e) Console.WriteLine(e.Message); String to double uses Double.Parse(str)

14 Array declaration and initialization
An integer Array must be defined like: int [] myArray = new int[50]; string [] strs = {"Mike", "John", "Kathy"}; However declarations int myArray [] ; string strs []; are wrong! It means that [] must be before the variable of array. Loop operator foreach foreach(int n in myArray){ } foreach(string s in strs){ } is special for array.

15 Switch operator can use strings
string [] strs = {"Mike", "John", "Kathy"}; foreach(string one in strs) { switch(one) case "Mike": Console.write(one); break; case "John": Console.write(one); case "Kathy": Console.write(one); default: Console.write("Not found"); }


Download ppt "C# Data type Chapter 1."

Similar presentations


Ads by Google