Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

. 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
        }
    }
}

For more information, see the following sections in the C# Language Specification:

  • 7.5.4 member access

Community Additions

ADD
Show:
© 2015 Microsoft