code_junkie

Meaning of :: Operator in C# and PhP

Nov 13th, 2011
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. using MyNameSpace;
  2.  
  3. ...
  4.  
  5. global::System.Data
  6.  
  7. object->method();
  8. object->property;
  9.  
  10. class::method();
  11. class::property;
  12.  
  13. object.method();
  14. class.property;
  15.  
  16. <?php
  17. class Circle {
  18. public $radius;
  19. public $centerX;
  20. public $centerY;
  21. public static $PI = 3.1415;
  22. public function __construct($r,$x,$y) {
  23. $this->radius = $r;
  24. $this->centerX = $x;
  25. $this->centerY = $y;
  26. }
  27. public function getArea() {
  28. return self::$PI * $this->radius * $this->radius;
  29. }
  30. }
  31. $circle1 = new Circle(10,0,0);
  32. echo $circle1->getArea() . "<br>n";
  33. $circle2 = new Circle(15,10,12);
  34. echo $circle2->getArea() . "<br>n";
  35. Circle::$PI = 3.14;
  36. echo $circle1->getArea() . "<br>n";
  37. echo $circle2->getArea() . "<br>n";
  38. ?>
  39.  
  40. namespace Foo {
  41. namespace System {
  42. class DateTime {}
  43. class Example {
  44. object Example() {
  45. return System.DateTime.Now; // Returns Foo.System.DateTime.Now
  46. return global::System.DateTime.Now; // Returns mscorlib,System.DateTime.Now
  47. }
  48. }
  49. }
  50. }
  51.  
  52. using IOAlias = System.IO;
  53.  
  54. IOAlias::StreamReader sr = new StreamReader(path);
  55.  
  56. global::System.IO.StreamReader sr = new StreamReader(path);
  57.  
  58. public class MyClass { }
  59.  
  60. namespace namespaceB
  61. {
  62. public class A
  63. {
  64. public class MyClass { }
  65. }
  66.  
  67. public class OtherClass
  68. {
  69. public A.MyClass MyAClass;
  70. }
  71. }
  72.  
  73. Explanation
  74. In the example above, the compiler would always resolve the
  75. MyAClass to the type namespaceB.A.MyClass.
  76. If you wanted it to be A.MyClass, there was no way to do it until C# 2.0.
  77. In C# 2.0, you would use: public class MyOtherClass
  78. { public global::A.MyClass yAClass; }
  79. To indicate that you shouldn't use the local namespace scope,
  80. but rather, the root namespace.
Add Comment
Please, Sign In to add comment