Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C# what is the thing written in square brackets before a class or a function? [duplicate]

Tags:

c#

.net

wcf

wpf

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();
}
like image 891
odbhut.shei.chhele Avatar asked Feb 08 '15 10:02

odbhut.shei.chhele


People also ask

What is '~' in C language?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

What does += in C mean?

+= Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A. -=

What is operators in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.

What does the || mean in C?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.


1 Answers

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 read this tutorial about Attributes by msdn.

like image 75
Farhad Jabiyev Avatar answered Oct 22 '22 13:10

Farhad Jabiyev