Common Type System CTS Type Reference Type Value

  • Slides: 29
Download presentation

Common Type System (CTS) Type Reference Type Value Type Built-in Type User- defined Type

Common Type System (CTS) Type Reference Type Value Type Built-in Type User- defined Type C# Programming with Visual C# 2010 Express 5

����������� Int 32: 55 Char: A สญลกษณะทใ ช “Hello” Value Type Heap Reference Type

����������� Int 32: 55 Char: A สญลกษณะทใ ช “Hello” Value Type Heap Reference Type String: Int 16: 21 Stack C# Programming with Visual C# 2010 Express 6

����� 3. 2 ���������������� คยเวรด ชอทใชแทนใน sbyte System. SByte byte System. Byte short System.

����� 3. 2 ���������������� คยเวรด ชอทใชแทนใน sbyte System. SByte byte System. Byte short System. Int 16 ushort System. UInt 16 int System. UInt 32 uint System. Int 64 char System. Char float System. Single double System. Double bool System. Boolean decimal System. Decimal struct type C# Programming with Visual C# 2010 Express 7

����������� 1. 2. static void Main(string[] args) int _int = 0; { 3. string

����������� 1. 2. static void Main(string[] args) int _int = 0; { 3. string _str = null; 4. bool _bool= true; 5. decimal _dec = 0. 000001 M; 6. double _dou = 0. 00 d; 7. char _chr = 'a'; 8. Console. Write. Line("_int = {0}", _int); 9. Console. Write. Line("_str = {0}", _str); 10. Console. Write. Line("_bool = {0}", _bool); 11. Console. Write. Line("_dec = {0}", _dec); 12. Console. Write. Line("_dou = {0}", _dou); 13. Console. Write. Line("_chr = {0}", _chr); 14. Console. Read. Line(); 15. } C# Programming with Visual C# 2010 Express 8

Anonymous Type Value Type var my. Int = 2; Class var v = new

Anonymous Type Value Type var my. Int = 2; Class var v = new { Amount=108, Message="Hello" }; Array var anon. Array = new[] { new { name = “A"}, new { name = “B" }}; LINQ var product. Query = from prod in products select new { prod. Color, prod. Price }; C# Programming with Visual C# 2010 Express 11

�������������� Casting ����������� Boxing �������������������������� Implicit ��������� CLR �������� Explicit ��������� int a =

�������������� Casting ����������� Boxing �������������������������� Implicit ��������� CLR �������� Explicit ��������� int a = 4; ������������� double b; b = a //Implicit conversion of int to double b = (int) a; //Explicit conversion (casting) of int to double C# Programming with Visual C# 2010 Express 12

����� 3. 4 ��������� sbyte short, int, long, float, double, decimal byte short, ushort,

����� 3. 4 ��������� sbyte short, int, long, float, double, decimal byte short, ushort, int, uint, long, ulong, float, double, decimal int, long, float, double, decimal ushort int, uint, long, ulong, float, double, decimal int long, float, double, decimal uint long, ulong, float, double, decimal long, uling float, double, decimal float double char ushort, int, uint, long, ulong, float, double, decimal C# Programming with Visual C# 2010 Express 13

����� Try. Parse ( ) 1. int number = 0; 2. string number. String

����� Try. Parse ( ) 1. int number = 0; 2. string number. String = "1234"; 3. if (int. Try. Parse(number. String, out number)) 4. { 5. Console. Write. Line("Conversion succeeded; (" 6. Console. Write. Line(number; ( 7. { 8. else 9. } 10. 11. Console. Write. Line("failed ; (" { C# Programming with Visual C# 2010 Express 14

�������� 1. Block ����������� ����� if, while if (x > 10) { int x

�������� 1. Block ����������� ����� if, while if (x > 10) { int x = x * x; } 2. Procedure ������������������ void What. Your. Name( ) { string name = Console. Read( ); Consoel. Writeln("Hello " + name); } C# Programming with Visual C# 2010 Express 15

�������� (��� ( 3. Module �������������� �������������� private public class Person { private string

�������� (��� ( 3. Module �������������� �������������� private public class Person { private string name; public void Set. Name(string x) { name = x; } public string Get. Name( ) { return name; } } C# Programming with Visual C# 2010 Express 16

�������� (��� ( 4. Namespace ����������� ���������� public ��������������������� 1. namespace Console. Application 1}

�������� (��� ( 4. Namespace ����������� ���������� public ��������������������� 1. namespace Console. Application 1} ��� 2. 3. 4. class Program} static void Main(string[] args} ( Person p 1; // ����� Person ������ public ������ Person 5. { 6. { 7. public class Person} 8. private string name; 9. public void Set. Name(string x} ( 10. name = x; 11. { 12. { 13. { C# Programming with Visual C# 2010 Express 17

��������� ������� (Constant variable) ������������� � ����������������������� (Read-only variable)����������� � ���������������������� (constructor) class Program

��������� ������� (Constant variable) ������������� � ����������������������� (Read-only variable)����������� � ���������������������� (constructor) class Program { readonly int y; //read-only variable ������������� Program()// constructor { y=1; } static void Main(string[] args) { Program p = new Program(); const int x=2; //constant variable } } C# Programming with Visual C# 2010 Express 18

����������� var += expression; // var = var + expression var -= expression; //

����������� var += expression; // var = var + expression var -= expression; // var = var - expression var *= expression; // var = var * expression var /= expression; // var = var / expression var %= expression; // var = var % expression item[(index + 1) % 32] = item[( idex + 1) % 32 ] + 1 = item[(index + 1) % 32] += 1; C# Programming with Visual C# 2010 Express 20

����������� ทดสอบการใชตรรกะ OR bool a, b, c; a = true; b = true; c

����������� ทดสอบการใชตรรกะ OR bool a, b, c; a = true; b = true; c = false; Console. Write. Line(a | b | c); // Print True ทดสอบการใชตรรกะ Short OR bool a, b, c; a = true; b = true; c = false; Console. Write. Line(a || b || c); // Print True C# Programming with Visual C# 2010 Express 22

����������� ทดสอบการ bitwise AND int a, b; a = 7; //00000111 b = 85;

����������� ทดสอบการ bitwise AND int a, b; a = 7; //00000111 b = 85; //0101 Console. Write. Line(a & b); //print 5 //0000 0101 ทดสอบการ bitwise XOR int a, b; a = 7; //00000111 b = 85; //0101 Console. Write. Line(a ^ b); //print 82 //0101 0010 C# Programming with Visual C# 2010 Express 24

���������� 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. private enum Color

���������� 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. private enum Color {Blue, Yellow, Red} static void Main(string[] args)} Color 1, Color 2, Color 3; Color 1 = Color. Blue; Color 2 = (Color)1; //Casting to Color enum Color 3 = (Color)2; Console. Write. Line("my. Color: {0}", Color 1; ( Console. Write. Line("my. Color: {0}", Color 2; ( Console. Write. Line("my. Color: {0}", Color 3; ( { C# Programming with Visual C# 2010 Express 27

����������� 1. 2. 3. 4. 5. public struct Customer { public string name; public

����������� 1. 2. 3. 4. 5. public struct Customer { public string name; public string last. Name; public string tel ; { static void Main(string[] args} Customer Ae; Ae. name = "Vilasinee; " 6. 7. 8. Ae. last. Name = "Choti; " Ae. tel = "01 -234 -5678; " Console. Write. Line("Name: {0} {1} tel: {2 , "{ Ae. name, Ae. last. Name, Ae. tel; ( 9. 10. 11. 12. 13. ( { C# Programming with Visual C# 2010 Express 28