Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this syntax mean in c#?

Tags:

c#

.net

I may not have it correct, but I saw something like this above a WebMethod:

[return:(XmlElement("Class2"),IsNullable = false)]
public Class2 MEthod1()
{

}

I saw a vb version first and I used a converter to convert it to c#. I have never seen it before. It's in a vb 6 asmx file.

like image 440
Xaisoft Avatar asked Aug 19 '11 23:08

Xaisoft


People also ask

What Is syntax with example in C?

Syntax basically refers to the protocols to be followed while writing a program. It is very necessary to follow proper syntax while coding to get the desired set of output. The C basic syntax consists of header files, main function, and program code. This is the most fundamental structure in the C program.

What is syntax error in C?

Syntax errors: Errors that occur when you violate the rules of writing C/C++ syntax are known as syntax errors. This compiler error indicates something that must be fixed before the code can be compiled. All these errors are detected by compiler and thus are known as compile-time errors.


1 Answers

It's an attribute target, and it's being used in your example to disambiguate usage on the return value from other elements:

// default: applies to method
[SomeAttr]
int Method1() { return 0; } 

// applies to method
[method: SomeAttr]
int Method2() { return 0; } 

// applies to return value
[return: SomeAttr]
int Method3() { return 0; } 

When creating attributes you can specify which language elements an attribute can be applied to. This is illustrated in the example below.

For a list of available targets see here:
http://msdn.microsoft.com/en-us/library/system.attributetargets.aspx

namespace AttTargsCS 
{
    // This attribute is only valid on a class.
    [AttributeUsage(AttributeTargets.Class)]
    public class ClassTargetAttribute : Attribute {
    }

    // This attribute is only valid on a method.
    [AttributeUsage(AttributeTargets.Method)]
    public class MethodTargetAttribute : Attribute {
    }

    // This attribute is only valid on a constructor.
    [AttributeUsage(AttributeTargets.Constructor)]
    public class ConstructorTargetAttribute : Attribute {
    }

    // This attribute is only valid on a field.
    [AttributeUsage(AttributeTargets.Field)]
    public class FieldTargetAttribute : Attribute {
    }

    // This attribute is valid on a class or a method.
    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
    public class ClassMethodTargetAttribute : Attribute {
    }

    // This attribute is valid on any target.
    [AttributeUsage(AttributeTargets.All)]
    public class AllTargetsAttribute : Attribute {
    }

    [ClassTarget]
    [ClassMethodTarget]
    [AllTargets]
    public class TestClassAttribute {
        [ConstructorTarget]
        [AllTargets]
        TestClassAttribute() {
        }

        [MethodTarget]
        [ClassMethodTarget]
        [AllTargets]
        public void Method1() {
        }

        [FieldTarget]
        [AllTargets]
        public int myInt;

        static void Main(string[] args) {
        }
    }
}
like image 97
James Johnson Avatar answered Sep 25 '22 04:09

James Johnson