19

I am familiar to C and C++. I am using C# for the first time. I trying to learn about WCF and WPF. I using a tutorial in CodeProject. There the writer has given a sample code. There he has written something before an interface and methods in square brackets. What are those? Are they comments? Here is the given sample code.

[ServiceContract(SessionMode = SessionMode.Required, 
    CallbackContract = typeof(IChatCallback))]
interface IChat
{
    [OperationContract(IsOneWay = true, IsInitiating = false, 
        IsTerminating = false)]
    void Say(string msg);

    [OperationContract(IsOneWay = true, IsInitiating = false, 
        IsTerminating = false)]
    void Whisper(string to, string msg);

    [OperationContract(IsOneWay = false, IsInitiating = true, 
        IsTerminating = false)]
    Person[] Join(Person name);

    [OperationContract(IsOneWay = true, IsInitiating = false, 
        IsTerminating = true)]
    void Leave();
}
odbhut.shei.chhele
  • 5,834
  • 16
  • 69
  • 109

1 Answers1

30

These are attributes. An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc., in your program. You can add declarative information to a program by using an attribute. A declarative tag is depicted by square ([ ]) brackets placed above the element it is used for.
For example, attributes could be used to indicate whether a class is serializable, or which field in a database a particular property should be written to and so on...

For example, let's look at this attribute:

 [OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = false)]

The attribute is OperationContract. And IsOneWay, IsInitiating, IsTerminating are properties of this attribute.

OperationContract - Indicates that a method defines an operation that is part of a service contract in a Windows Communication Foundation (WCF) application.
IsOneWay - Gets or sets a value that indicates whether an operation returns a reply message.
IsInitiating - Gets or sets a value that indicates whether the method implements an operation that can initiate a session on the server (if such a session exists).
IsTerminating - Gets or sets a value that indicates whether the service operation causes the server to close the session after the reply message, if any, is sent.

You can use predefined attributes or create your own custom attribute.

You can find all predefined attributes and their description here.
You can download this tutorial about Attributes by msdn from the link at this webpage.

Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98