1 / 15

Chapter 1

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.

argus
Download Presentation

Chapter 1

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. Chapter 1 C# Data type

  2. Major C# Data Type Most of them are same as in C++

  3. Some C# Math functions 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 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); break; case "Kathy": Console.write(one); break; default: Console.write("Not found"); } }

More Related