Guest User

Untitled

a guest
May 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. public class Role
  2. {
  3. public string Name { get; set; }
  4. }
  5.  
  6. Role role = new Role();
  7. role.Name = "RoleName";
  8.  
  9. Role role = "RoleName";
  10.  
  11. public static implicit operator Role(string roleName)
  12. {
  13. return new Role() { Name = roleName };
  14. }
  15.  
  16. public static explicit operator Role(string roleName)
  17. {
  18. return new Role() { Name = roleName };
  19. }
  20.  
  21. Role r = (Role)"RoleName";
  22.  
  23. int x = 10;
  24. long y = x; // Implicit conversion from int to long
  25. int z = (int) y; // Explicit conversion from long to int
  26.  
  27. int x = 10;
  28. long y = (long) x; // Explicit use of implicit conversion!
  29.  
  30. myDigit = (Digit) myDouble
  31.  
  32. myDigit = myDouble;
  33.  
  34. internal class Explicit
  35. {
  36. public static explicit operator int (Explicit a)
  37. {
  38. return 5;
  39. }
  40. }
  41.  
  42.  
  43. internal class Implicit
  44. {
  45. public static implicit operator int(Implicit a)
  46. {
  47. return 5;
  48. }
  49. }
  50.  
  51. var obj1 = new Explicit();
  52. var obj2 = new Implicit();
  53.  
  54. int integer = obj2; // implicit conversion - you don't have to use (int)
  55.  
  56. int integer = (int)obj1; // explicit conversion
  57.  
  58. int integer = obj1; // WON'T WORK - explicit cast required
  59.  
  60. internal interface ITest
  61. {
  62. void Foo();
  63. }
  64.  
  65. class Implicit : ITest
  66. {
  67. public void Foo()
  68. {
  69. throw new NotImplementedException();
  70. }
  71. }
  72.  
  73. class Explicit : ITest
  74. {
  75. void ITest.Foo() // note there's no public keyword!
  76. {
  77. throw new NotImplementedException();
  78. }
  79. }
  80.  
  81. Implicit imp = new Implicit();
  82. imp.Foo();
  83. Explicit exp = new Explicit();
  84. // exp.Foo(); // won't work - Foo is not visible
  85. ITest interf = exp;
  86. interf.Foo(); // will work
  87.  
  88. class MyClass
  89. {
  90. string myField;
  91.  
  92. void MyMethod(int someNumber)
  93. {
  94.  
  95. }
  96. }
  97.  
  98. public class MyClass
  99. {
  100. private string myField;
  101.  
  102. public void MyMethod(int someNumber)
  103. {
  104. }
  105. }
Add Comment
Please, Sign In to add comment