How do you tell the C # compiler that a symbol is a type and not a variable when they have the same name?

If you have a local variable named the same as a type, is there a way to tell the compiler that the symbol you specified is a type or a variable? For example, consider (and ignore all return type errors):

public class sometype { public static sometype DoSomething() {} }

public string sometype { get { return sometype.DoSomething(); } } //A
public string sometype { get { return sometype.Trim(); } } //B
public sometype sometype { get { return sometype.DoSomething(); } } //C
public sometype sometype { get { return sometype.Trim(); } } //D

      

  • A -> Error (no DoSomething () method)
  • B โ†’ Works
  • C โ†’ Works
  • D -> error (no Trim () method)

From a more applied point of view

(you can skip this if XSD annoys you):

I am currently trying to get LINQ to work with XSD. My XSD file has xs: elements like this:

<xs:element name="car" type="car">

      

Where the type "car" is defined as a simple type, like this one
(maybe a few more restrictions, but this is essentially):

<xs:simpleType name="car">
 <xs:restriction base="xs:string" />
</xs:simpleType>

      

So LINQ to XSD generates code that looks like this:

public string car {
    get {
        XElement x = this.GetElement(XName.Get("car", ""));
        return XTypedServices.ParseValue<string>(x, XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).Datatype);
    }
    set {
        this.SetElementWithValidation(XName.Get("car", ""), value, "car", car.TypeDefinition);
    }
}

      

But it won't compile due to the above issue.

+2


source to share


2 answers


You must fully qualify a namespace of this type.



If the type does not have a namespace, you can prefix it with global::

(C # anyway).

+5


source


You can study the C # specification for more information on this behavior. Here is the beginning of a chapter that describes it:



7.3 User
lookup Member lookup is the process by which the value of a name in the context of a type is determined. Member lookups may occur as part of simple name evaluation (ยง7.5.2) or member access (ยง7.5.4) in an expression. If a simple name or member-access occurs as a simple call-expression (ยง7.5.5.1), the member is considered called. If the member is a method or event, or if it is a constant, field, or property of a delegate type (ยง15), then the member is called invocable. member lookup takes into account not only the name of the member, but also the number of parameter types that the member has and the member is available. For the purpose of searching for members, common methods and nested common types have a number of type parameters specified in their respective declarations and all other members have type zero parameters.

+2


source







All Articles