Meaning :: Operator in C # and PhP

In my work I came across syntax like global :: (C #) or G: Load ($ value) in Php, what do they mean?

+1


source to share


8 answers


In C # ::

is the namespace alias qualifier operator and global

is the global namespace. This is necessary if you have overlapping classes and namespaces.

For example, if you have a class Foo.Bar

and Baz.Foo.Bar

, then the reference Foo.Bar

can be an ambiguous reference, since it Foo.Bar

exists in both the global namespace and the namespace Baz

. However, by saying global::Foo.Bar

you are explicitly looking at the global namespace reference and hence are no longer ambiguous.



More information here .

+9


source


In PHP :: is used to access static members or methods, see http://www.php.net/manual/en/language.oop5.static.php



+4


source


If C #, ::

called a namespace alias qualifier.

It qualifies which namespace you are in exactly should be ambiguous. For example:

using MyNameSpace;

...

global::System.Data 

      

distinguishes namespace System.Data

from MyNameSpace.System.Data

.

Look at the C # link on the operator ::

:

http://msdn.microsoft.com/en-us/library/htccxtad.aspx

and more helpful, MSDN article demonstrating its use

http://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx

+3


source


in class-based object oriented programming languages ​​(e.g. C ++, Java, PHP, ...) there are properties (also called fields) that are data and are just variables or constants, and methods that are the functionality of a class and are actually functions (routines). each class can be created as an object. each object has all the data and functionality of the base class at the beginning, but the data can change and differ from one object to another. there are 2 kinds of properties and 2 kinds of methods. some properties are shared among all objects of the class. this means that if you change this class data, then the same data of all objects that were created will be changed to the new value. such properties are called class properties or static properties (because,when you define these variables, in most languages ​​you will use the static keyword). some other properties are specific to each object. changing the data of an object does not affect the data of other objects. they are called object properties or instance properties. some methods are called from an object are called object methods. these are object methods and their use, you need to create a class first and use an object. some other methods are called using the class itself. these are class methods (or static methods), if you want one of these functions, there is no need for an object. to use properties / methods of an object, in PHP (like C ++) you have to use the β†’ operator:some other properties are specific to each object. changing the data of an object does not affect the data of other objects. they are called object properties or instance properties. some methods are called from an object are called object methods. these are object methods and their use, you need to create a class first and use an object. some other methods are called using the class itself. these are class methods (or static methods), if you want one of these functions, there is no need for an object. to use properties / methods of an object, in PHP (like C ++) you have to use the β†’ operator:some other properties are specific to each object. changing the data of an object does not affect the data of other objects. they are called object properties or instance properties. some methods are called from an object are called object methods. these are object methods and their use, you need to create a class first and use an object. some other methods are called using the class itself. these are class methods (or static methods), if you want one of these functions, there is no need for an object. to use properties / methods of an object, in PHP (like C ++) you have to use the β†’ operator:these are object methods and their use, you need to create a class first and use an object. some other methods are called using the class itself. these are class methods (or static methods), if you want one of these functions, there is no need for an object. to use properties / methods of an object, in PHP (like C ++) you have to use the β†’ operator:these are object methods and their use, you need to create a class first and use an object. some other methods are called using the class itself. these are class methods (or static methods), if you want one of these functions, there is no need for an object. to use properties / methods of an object, in PHP (like C ++) you have to use the β†’ operator:

object->method();
object->property;

      

to use properties / methods of a class in PHP (like C ++) you have to use the operator:

class::method();
class::property;

      

in languages ​​like Jave, operator. used for all properties / methods:

object.method();
class.property;

      

suppose you have a Circle class. this class defines some data, such as radius and center coordinates, and a constant for the PI value. also has a method for calculating the area of ​​a circle.

here is some PHP code to make it clear:

<?php
class Circle {
    public $radius;
    public $centerX;
    public $centerY;
    public static $PI = 3.1415;
    public function __construct($r,$x,$y) {
        $this->radius = $r;
        $this->centerX = $x;
        $this->centerY = $y;
    }
    public function getArea() {
        return self::$PI * $this->radius * $this->radius;
    }
}
$circle1 = new Circle(10,0,0);
echo $circle1->getArea() . "<br>\n";
$circle2 = new Circle(15,10,12);
echo $circle2->getArea() . "<br>\n";
Circle::$PI = 3.14;
echo $circle1->getArea() . "<br>\n";
echo $circle2->getArea() . "<br>\n";
?>

      

in this example, we define an environment class that has object properties (radius, centerX, CenterY), object methods (getArea ()), and a class property (PI). by changing the static PI property, the scope of both objects will be affected.

+2


source


Consider the following example:

namespace  A { 
    public class MyClass { }
} 

namespace namespaceB
{
    public class A
    {
        public class MyClass { }
    }

    public class OtherClass
    {
        public A.MyClass MyAClass;
    }
}

      

Explanation:

In the above example, the compiler always resolves MyAClass for the .BAMyClass type namespace.

If you want it to be A.MyClass, there was no way to do this before C # 2.0.

In C # 2.0, you have to use: public class MyOtherClass

{ public global::A.MyClass yAClass; }

      

To indicate that you should not use the local namespace scope, but rather the root namespace.

+2


source


In C #, the global :: operator is used to check that the next name is interpreted as a global name expression instead of a local one. The main use for this is when developers have a nested namespace where the name is the same as the global namespace. Without global :: operator, there is no access to the global version.

namespace Foo { 
  namespace System {
    class DateTime {} 
    class Example {
      object Example() { 
        return System.DateTime.Now; // Returns Foo.System.DateTime.Now
        return global::System.DateTime.Now; // Returns mscorlib,System.DateTime.Now
      }
    }
  }
}

      

+1


source


In PHP it is called the Scope Resolution Operator (Paamayim Nekudotayim) and allows access to members or constants within a class

+1


source


In C #, an operator ::

is a namespace alias specifier. It is used to resolve identifiers in the appropriate namespace, especially in situations where ambiguities exist.

Suppose you create a namespace alias via

using IOAlias = System.IO;

      

and want to declare an instance of the class StreamReader

. You can do it through

IOAlias::StreamReader sr = new StreamReader(path);

      

Alternatively, you can resolve the global namespace with

global::System.IO.StreamReader sr = new StreamReader(path);

      

0


source







All Articles