Operators and Expressions Guide
Several operators and expressions can be used to access a type member, including the dot ( .
), indexer ( []
), and invocation operators ( ()
). When working in the OneStream integrated development environment (IDE) or another IDE like Visual Studio or VS Code, typing the dot operator activates IntelliSense, providing a selection of available class members, methods, namespaces, and more.
Operators
- Member access (
.
)- To access a member of a namespace or type.
MyClass myObject = new MyClass();
myObject.MyMethod();
- Array element or indexer access (
[ ]
)- To access an array element or a type indexer.
int[] myArray = { 1, 2, 3 };
int element = myArray[0];
- Index from end (
^
)- To indicate that the element position is from the end of a sequence.
- Range (
..
)- To specify a range of indices that you can use to obtain a range of sequence elements.
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] subset = numbers[2..5]; // subset contains { 3, 4, 5 }
- Null-conditional operators (
?.
and?[ ]
)- To perform a member or element access operation only if an operand is non-null.
MyClass myObject = null;
int? length = myObject?.MyProperty.Length; // length is null
int[] myArray = null;
int? element = myArray?[0]; // element is null
- Method invocation (
( )
)- To call an accessed method or invoke a delegate.
public class MyClass
{
public void MyMethod()
{
Console.WriteLine("Hello from MyMethod!");
}
public int Add(int a, int b)
{
return a + b;
}
}
MyClass myObject = new MyClass();
// Invoking a method without arguments
myObject.MyMethod(); // Output: Hello from MyMethod!
// Invoking a method with arguments
int result = myObject.Add(5, 3); // result is 8
- Arithmetic Operators:
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulus)
- Comparison Operators:
== (Equal to)
!= (Not equal to)
> (Greater than)
< (Less than)
>= (Greater than or equal to)
<= (Less than or equal to)
- Logical Operators:
- C#
- VB.NET
&& (Logical AND)
|| (Logical OR)
! (Logical NOT)
And (Logical AND)
Or (Logical OR)
Not (Logical NOT)
- Assignment Operators:
= (Assignment)
+= (Addition assignment)
-= (Subtraction assignment)
*= (Multiplication assignment)
/= (Division assignment)
%= (Modulus assignment)
- Bitwise Operators:
& (AND)
| (OR)
^ (XOR)
~ (NOT)
<< (Left shift)
>> (Right shift)
Member access expressions
Member access expressions can be used to access members (fields, properties, methods, events) of a class, struct, or interface. They allow you to interact with the data and behavior encapsulated within objects.
Accessing Members of a Type
To access members of a type (class, struct, interface), you use the dot operator ( .
) followed by the member name as in the examples below:
public class MyClass
{
public int MyField;
public string MyProperty { get; set; }
public void MyMethod()
{
Console.WriteLine("Hello from MyMethod!");
}
}
MyClass myObject = new MyClass();
// Accessing a field
myObject.MyField = 10;
int fieldValue = myObject.MyField;
// Accessing a property
myObject.MyProperty = "Hello";
string propertyValue = myObject.MyProperty;
// Invoking a method
myObject.MyMethod();
Accessing Members of a Namespace
Namespaces are used to organize code and avoid naming conflicts. To access members within a namespace, you typically use the “using
” directive in C# or the “Imports
” statement in VB.NET.
- C#
- VB.NET
using System.Collections.Generic;
Imports System.Collections.Generic