Light copy of objects in C #

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Demo of a simple clone of a namespace object
  5. {
  6. // The object's shortest copy, only copies of the copy value type, and the reference type copy address
  7. Class Program
  8. {
  9. Static void main (string [] ARGs)
  10. {
  11. Student Stu = new student ();
  12. Stu. Age = 23;
  13. Stu. stuid = "20222222 ";
  14. Stu. Per = new person ();
  15. Stu. Per. Age = 800;
  16. Stu. Per. Name = "Aladdin ";
  17. Console. writeline ("before replication: Stu. age {0} Stu. stuid {1} Stu. per. age {2} Stu. per. name {3} ", Stu. age, Stu. stuid, Stu. per. age, Stu. per. name );
  18. Student stu2 = Stu. copyobj ();
  19. Stu2.age = 223333;
  20. Stu2.stuid = "201111111 ";
  21. Stu2.per. Age = 500;
  22. Stu2.per. Name = "zhaohaifu ";
  23. Console. writeline ("Copied: Stu. age {0} Stu. stuid {1} Stu. per. age {2} Stu. per. name {3} ", Stu. age, Stu. stuid, Stu. per. age, Stu. per. name );
  24. // It can be seen that the stu value type is not changed, and the reference type is the transfer address, so the original object can still be operated after the shortest copy.
  25. Console. Readline ();
  26. }
  27. }
  28. Class person
  29. {
  30. Private string _ name;
  31. Private int _ age;
  32. Public string name
  33. {
  34. Get
  35. {
  36. Return this. _ name;
  37. }
  38. Set
  39. {
  40. This. _ name = value;
  41. }
  42. }
  43. Public int age
  44. {
  45. Get
  46. {
  47. Return this. _ age;
  48. }
  49. Set
  50. {
  51. This. _ age = value;
  52. }
  53. }
  54. }
  55. Class student
  56. {
  57. Private string _ stuid;
  58. Private person _ per;
  59. Private int _ age;
  60. Public int age
  61. {
  62. Get
  63. {
  64. Return this. _ age;
  65. }
  66. Set
  67. {
  68. This. _ age = value;
  69. }
  70. }
  71. Public person per
  72. {
  73. Get
  74. {
  75. Return this. _ per;
  76. }
  77. Set
  78. {
  79. This. _ per = value;
  80. }
  81. }
  82. Public String stuid
  83. {
  84. Get
  85. {
  86. Return this. _ stuid;
  87. }
  88. Set
  89. {
  90. This. _ stuid = value;
  91. }
  92. }
  93. Public student copyobj ()
  94. {
  95. Return (student) This. memberwiseclone ();
  96. }
  97. }
  98. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.