Поделиться через


. Operator (C# Reference)

The dot operator (.) is used for member access. The dot operator specifies a member of a type or namespace. For example, the dot operator is used to access specific methods within the .NET Framework class libraries:

// The class Console in namespace System:
System.Console.WriteLine("hello");

For example, consider the following class:

class Simple
{
    public int a;
    public void b()
    {
    }
}
Simple s = new Simple();

The variable s has two members, a and b; to access them, use the dot operator:

s.a = 6;   // assign to field a;
s.b();     // invoke member function b;

The dot is also used to form qualified names, which are names that specify the namespace or interface, for example, to which they belong.

// The class Console in namespace System:
System.Console.WriteLine("hello");

The using directive makes some name qualification optional:


namespace ExampleNS
{
    using System;
    class C
    {
        void M()
        {
            System.Console.WriteLine("hello");
            Console.WriteLine("hello");   // Same as previous line.
        }
    }
}

But when an identifier is ambiguous, it must be qualified:

namespace Example2
{
    class Console
    {
        public static void WriteLine(string s){}
    }
}
namespace Example1
{
    using System;
    using Example2;
    class C
    {
        void M()
        {                
            // Console.WriteLine("hello");   // Compiler error. Ambiguous reference.
            System.Console.WriteLine("hello"); //OK
            Example2.Console.WriteLine("hello"); //OK
        }
    }
}

C# Language Specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See Also

Reference

C# Operators

Concepts

C# Programming Guide

Other Resources

C# Reference