static void Main(string[] args) { var person = new { Name = "A", Age = 30 }; Console.WriteLine(person.Age); Console.WriteLine(person.Name); Console.WriteLine(person.GetType().Name); var student = new { Name = "B", Age = 20, Score = 100 }; Console.WriteLine(student.GetType().Name); uint x = uint.MaxValue; Console.WriteLine(x); String binStr = Convert.ToString(x, 2); Console.WriteLine(binStr); try { uint y = unchecked (x + 1); Console.WriteLine(y); binStr = Convert.ToString(y, 2); Console.WriteLine(binStr); } catch (OverflowException ex) { Console.WriteLine("error:" + ex.Message); } Console.WriteLine("int: " + sizeof(int)); Console.WriteLine("double: " + sizeof(double)); Console.WriteLine("long" + sizeof(long)); unsafe { Console.WriteLine("Student: " + sizeof(Student)); Student stu; stu.Id = 1; stu.Score = 100; Student *pStu = &stu; pStu->Id = 2; pStu->Score = 59; Console.WriteLine(stu.Id); Console.WriteLine(pStu->Score); } int max = int.MaxValue; int min = int.MinValue; Console.WriteLine("max int:" + max); Console.WriteLine(Convert.ToString(max, 2).PadLeft(32, '0')); Console.WriteLine("min int:" + min); Console.WriteLine(Convert.ToString(min, 2).PadLeft(32, '0')); int _min = checked (-min); Console.WriteLine("-min:" + _min); }
StudentWrapper(String ^ fullname, double gpa) { _stu = new Student((char *) System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi( fullname).ToPointer(), gpa); }
/// <summary> /// 指针操作。 /// </summary> static void PointerOperation() { Student student; //声明Student结构体的实例并初始化。 student.ID = 1; student.Score = 99; Console.WriteLine($"student-{student.ID}: {student.Score}"); //输出student-1: 99。 unsafe { Student *pStu = &student; //&操作符,获取实例student的内存地址。 pStu->Score = 100; //通过指针对字段的值进行修改。 (*pStu).ID = 2; //*操作符,引用指针pStu指向的实例,但由于“.”的运算优先级较高,故应当加上括号。 Console.WriteLine($"student-{student.ID}: {student.Score}"); //输出student-2: 100。 } }
static void Main(string[] args) { unsafe { Student stu; stu.ID = 1; stu.Score = 100; Student *pStu = &stu; //指针 pStu->Score = 99; //箭头符号 (*pStu).Score = 1000; //取地址符号和取引用符号 Console.WriteLine(stu.Score); //凡是用.访问的,都是直接访问,用就>-访问的是通过指针的间接访问 int x = sizeof(Student); Console.WriteLine(x); //输出16,8+8 Console.ReadKey(); } }
static void Main(string[] args) { unsafe { Student stu = new Student(); stu.ID = 0; //Using x.y to access the member is the direct access. stu.Score = 99; //"&" is to get the memory address of stu. Student *pStu = &stu; //Using x->y to access the member is the indirect access. pStu->Score = 100; //"*"(point indrection) could obtain the variable pointed by a pointer. //But it is inferior to the operator ".". That's why there is "()"; (*pStu).Score = 101; Console.WriteLine(stu.Score); } }
static void Main(string[] args) { //unsafe so as to use pointers unsafe { Student s1 = new Student(1, 95.0); Student s2 = new Student(2, 79.5); //declaring two student pointer and initializing them with addresses Student *s1_ptr = &s1; Student *s2_ptr = &s2; //using arrow operator Console.WriteLine("Details os Student1"); Console.WriteLine("Roll No: {0} & Marks: {1}", s1_ptr->rollno, s1_ptr->marks); Console.WriteLine("Roll No: {0} & Marks: {1}", s2_ptr->rollno, s2_ptr->marks); Console.ReadLine(); } }
static void Main(string[] args) { var person = new { Name = "Mr.Okay", Age = 34 }; Console.WriteLine(person.Age); uint x = uint.MaxValue; checked { try { //uint y = checked(x + 1); uint y = x + 1; Console.WriteLine(y); } catch (OverflowException ex) { Console.WriteLine("There is overflow!"); } } unsafe { Student stu; stu.ID = 1; stu.Score = 99; Student *pStu = &stu; pStu->Score = 100; (*pStu).Score = 101; Console.WriteLine(stu.Score); } Stone stone = new Stone(); stone.Age = 5000; //Monkey wukongSun = (Monkey)stone; for explicit Monkey wukongSun = stone; //implict Console.WriteLine(wukongSun.Age); int x1 = 7; int y1 = 28; int z1 = x1 ^ y1; String strX = Convert.ToString(x1, 2).PadLeft(32, '0'); String strY = Convert.ToString(y1, 2).PadLeft(32, '0'); String strZ = Convert.ToString(z1, 2).PadLeft(32, '0'); Console.WriteLine(strX); Console.WriteLine(strY); Console.WriteLine(strZ); if (x1 > y1 && x1++ > 8) //skip x1++, better avoid { Console.WriteLine("Hello"); } Console.WriteLine(x1); Nullable <int> x2 = null; //int? x2 = null; int y2 = x2 ?? 1; string str = (x2 >= 60) ? "Pass" : "Failed"; Console.WriteLine(x2); }