Usage of [ ] and () Operators in C# (C Sharp)

C# provides a wide set of operators of which [ ] operator and ( ) operators will be discussed in this article. Each of these operators performs multiple operations as discussed below.




( ) Operator: ( ) operator serves two different purposes. They are listed below:

• Decides the order of precedence
• Performs Explicit Casting

As already mentioned, C# has a wide set of operators. When multiple operators are used on an expression, then in what order will they be evaluated? For that C# has predefined rules for the order of precedence. For example, in arithmetic operators *, / takes more precedence over +, - operators. When your expression has * and + operators, * takes more precedence over + operator. If you want to change the precedence, you can use ( ) operator. Here is an example to demonstrate it:

public class sampleClass {
public static void Main() {
int result1 = 10 + 5 * 40;
int result2 = (10+5) * 40;
Console.WriteLine(“result1 = {0}, result2 = {1}”, result1, result2);
}
}

Output of this code will be:

result1 = 210 result2 = 600

In the first expression, 5*40 will be evaluated first and 10 will be added to the result. But in second expression you have changed the order of precedence using ( ) operator. You evaluate 10+5 first and to the result 15 you multiply 40.

( ) operator is also used to perform explicit casting termed as type conversion. C# permits automatic type conversion from certain data type to another. For example you can convert integer to double. That doesn’t have any data loss and can happen without any casting.

But when you try to convert from double to integer, there are chances of data loss. Hence if you directly assign double to integer you will end up in error. If you want to perform such conversions without ending up in error, then you can do it using ( ) operator as shown below:

public class sampleClass {
public static void Main() {
double var1 = 100.5;
int var2 = (int) var1;
Consolw.WriteLine(“Value of var2 = {0}”, var2);
}
}

Output of this code will be:

Value of var2 = 100

[ ] Operator: [ ] operator is also used to perform different operations:

• In Arrays
• In Indexers
• To specify Attributes
• Used with Pointers

If you want to use arrays in your code, you can do it only using [ ] operator. Right from declaration, initialization and even for accessing arrays you have to use [ ] operator. Here is an example to demonstrate it:

class sampleClass {
public static void Main() {
int[ ] intArray = new int[500];
for(int index=0;index<500;index++) {
intArray[index] = index+1;
}
}
}

In this example, you use [ ] operator to do the following:

• To declare a one dimensional integer array: int[ ] intArray
• To initialize the size of the array: int[ ] intArray = new int[500]
• To access the array element: intArray[index]

[ ] operator can also be used with indexers. To know how [ ] operator is used in indexers, consider the following example:

public class sampleClass {
public string[,] sampleArray = new string[1,1];
public string this[int index1, int index2] {
get {
return sampleArray[index1,index2];
}
set {
sampleArray[index1,index2] = value;
}
}
}
public class testClass {
public static void Main( ) {
sampleClass obj = new sampleClass( );
obj[0,0] = “value1”;
obj[0,1] = “value2”;
console.WriteLine("{0},{1},{ obj[0,0],obj[0,1]);
}
}

Output of this code will be:

value1, value2

In this example, you create an alternative way of accessing two dimensional array elements using [ ] operator. You can also use [ ] operator to specify attributes as shown in the example below:


[AttributeUsage(AttributeTargets.Class, AllowMultiple = true) ]
public class sampleClass : Attribute {
}

It is also used in unsafe code context when you use pointers to access arrays, as shown in the example below:

class sampleClass {
public static void Main() {
int sampleArray = new int[100];
unsafe fixed ( int* ptr = sampleArray ) {
for(int index = 0; index < 100; index++) {
ptr[index] = index+1;
}
}
}

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| Design Guideline for C# Structs (C Sharp) | Design Patterns – Its Importance and Types | How to Implement Proxy Pattern Using C# (C Sharp)? | How to Implement Singleton Pattern Using C# (C Sharp)? | Illustration of Abstract Classes of C# (C Sharp) with Examples | Illustration of Sealed Classes of C# (C Sharp) with Examples | List of Overloadable Operators in C# (C Sharp) | Usage of [ ] and () Operators in C# (C Sharp) | What is C# (C Sharp) Nested Type? |


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.