(02) c sharp_tutorial

477 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
477
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

(02) c sharp_tutorial

  1. 1. C# Tutorial Jonas Tan [email_address]
  2. 2. Reference <ul><li>MSDN The C# Language </li></ul><ul><ul><li>http://msdn.microsoft.com/vcsharp/programming/language/ </li></ul></ul>
  3. 3. Hello World! <ul><li>using System; </li></ul><ul><li>class Form1 </li></ul><ul><li>{ </li></ul><ul><li>private System.Windows.Forms.TextBox textBox1; </li></ul><ul><li>//Constructor </li></ul><ul><li>public Form1() { </li></ul><ul><li>textbox1 = “Hello world!&quot;; </li></ul><ul><li>} </li></ul><ul><li>//Start the program </li></ul><ul><li>static void Main() { </li></ul><ul><li>Application.Run( new Form1() ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  4. 4. Type System <ul><li>Value Types: </li></ul><ul><ul><li>byte b = 120 (8-bit) </li></ul></ul><ul><ul><li>char c = ‘a’; (16-bit Unicode) </li></ul></ul><ul><ul><li>int i=7; (32-bit) </li></ul></ul><ul><ul><li>long l = 88; (64-bit) </li></ul></ul><ul><ul><li>string s = “a string”; (set of Unicode characters) </li></ul></ul><ul><li>Reference Types (arrays) </li></ul><ul><ul><li>string[] s = new string[10]; </li></ul></ul><ul><ul><li>int[] i = new int[5]; </li></ul></ul><ul><ul><li>char[] c = new char[16]; </li></ul></ul><ul><ul><li>byte[] b = new byte[22]; </li></ul></ul>
  5. 5. Indexers <ul><li>string s = “this is a string”; </li></ul><ul><li>char ch = s[3]; </li></ul><ul><li>s[6] = ‘x’; //Error! Read Only. </li></ul><ul><li>int[] i = new int[] {2, 4, 6, 8, 10}; </li></ul><ul><li>int b = i[2]; </li></ul><ul><li>i[3] = 98; </li></ul>
  6. 6. Basic Type casting <ul><li>int (32-bit) to long (64-bit) </li></ul><ul><ul><ul><ul><li>int a = 537; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>long b = 6473291; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>b = (long)a; </li></ul></ul></ul></ul><ul><li>char(16-bit) to int(32-bit) </li></ul><ul><ul><ul><ul><li>char c = ‘x’; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>int d = 8; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>d = (int)c; </li></ul></ul></ul></ul><ul><li>Dangerous Casting </li></ul><ul><ul><ul><ul><li>cast int to uint (possible sign error) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>long to short (possible overflow) </li></ul></ul></ul></ul>
  7. 7. Advanced Type Casting <ul><li>int to string </li></ul><ul><ul><li>int a = 3; </li></ul></ul><ul><ul><li>string s1 = a.ToString(); </li></ul></ul><ul><li>char array to string </li></ul><ul><ul><ul><li>char [ ] b2 = new char[3] {‘a’, ‘c’, ‘e’, ‘g’, ‘h’}; </li></ul></ul></ul><ul><ul><ul><li>String s2 = new string(b2); </li></ul></ul></ul><ul><li>string to int </li></ul><ul><ul><ul><li>sting s3 = “564”; </li></ul></ul></ul><ul><ul><ul><li>int c = int.Parse(s3); </li></ul></ul></ul><ul><li>string to byte array </li></ul><ul><ul><li>byte[ ] buf1 = Encoding.Unicode.GetBytes(s3); </li></ul></ul><ul><li>string to char array </li></ul><ul><ul><li>char [ ] buf2 = s3.ToCharArray(); </li></ul></ul>
  8. 8. Operators <ul><li>+, -, *, /, % </li></ul><ul><li>++, -- </li></ul><ul><li>==, !=, <, >, <=, >= </li></ul><ul><li>&, |, ^, ~, ! </li></ul><ul><li>true, false </li></ul>
  9. 9. Shift Operation <ul><li>Use two unsigned integers to simulate a unsigned long integer </li></ul><ul><ul><ul><li>ulong ul = 73654823; //64-bit </li></ul></ul></ul><ul><ul><ul><li>uint a[2] = new uint[]{0,0}; //2 32-bit unsigned integers </li></ul></ul></ul><ul><ul><ul><li>a[0] = (uint) ul; </li></ul></ul></ul><ul><ul><ul><li>a[1] = (uint) ul >> 32; </li></ul></ul></ul>
  10. 10. if and while statements <ul><li>if( x>3 && x<8 ) </li></ul><ul><li>{ </li></ul><ul><li>… </li></ul><ul><li>} </li></ul><ul><li>while( i>2 ) </li></ul><ul><li>{ </li></ul><ul><li>… </li></ul><ul><li>} </li></ul><ul><li>for( int i=0; i<x; ++i ) </li></ul><ul><li>{ </li></ul><ul><li>… </li></ul><ul><li>} </li></ul>

×