180 likes | 385 Views
Chapter 1. C# Data type. Major C# Data Type. Most of them are same as in C++. Some C# Math functions. No Math.Random() function. Random class. Random class object is used to generate random integer, random floating number or random bytes.
E N D
Chapter 1 C# Data type
Major C# Data Type Most of them are same as in C++
Some C# Math functions No Math.Random() function
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
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
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
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 +)
Standard input and output Standard input is: Console.Read(); Console.ReadLine(); Standard output is: string s = "Hello"; Console.ReadLine(s);
MessageBox.Show 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
Other icons: MessageBoxIcon.Asterisk MessageBoxIcon.Error MessageBoxIcon.Exclamation MessageBoxIcon.Hand MessageBoxIcon.Information MessageBoxIcon.None MessageBoxIcon.Question MessageBoxIcon.Stop MessageBoxIcon.Warning
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
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); }
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)
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.
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); break; case "Kathy": Console.write(one); break; default: Console.write("Not found"); } }