Skip to content

Operators

Assignment


Assignment ( = )

Description

The = is an assignment operator that stores the value of its right operand into its left operand.

It can be used with any type of operator (numeric, string, objects, complex and non-complex variables), as long as both operands are of the same type.

Attention

If the operands have not the same type, then the right operand must be implicitly convertible to the type of the left operand. If not, the assignment will fault, producing a Validation or a Build error.

Example

/* Simple assignment */
int anInt = 5; 
int anotherInt = 7; 
anInt = anotherInt; //Now, anInt is 7
Domain.ApplicationUser user = Domain.ApplicationUser.GetAll().First();

/* Assignment, with different types and implicit conversion */
decimal aDecimal = 0;
aDecimal = anotherInt; //Implicit conversion assignment. aDecimal is now 7.00

/* Assignment, with different types and casting */
double aDouble;
float aFloat = aDouble as float; //Cast

/* Assignment, with explicit conversion */
string aString = anInt.ToString(); //Explicit conversion
anInt = int.Parse("147"); //Explicit conversion

Arithmetic


Plus ( + )

Description

The + operator functions as a unary and binary operation.

It can be used with any numeric type to calculate the sum of its operands.

When the operands are of a string type, it concatenates (joins) the two strings.

If even one operand is a string, then the result of the addition is a string as well. Any non-string operand will be converted to its string representation

Attention

The + operator cannot be used as a unary operator on variables in Mamba. Thus the following are not supported:

  • +x
  • ++x
  • x++

Example

// Simple sum of two integer
int x = 5 + 5; 

// Adding two decimals
decimal y = 1.4 + 2.3; 

// Adding two numbers of a different type
decimal z = 5 + 1.1; 

//String concatenation
string hi = "Hello" + "!"; 

//String concatenation with a string and a number
string age = "My age is: " + 21; // 

//Concatenating more than 2 operands of different types
string greeting = "Hello, I'm " + "Queen" + ", born in " + 1985 + " and I've been coding since " + DateTime.Create(2004, 4, 1) + ".";

/*
Results:
    x = 10
    y = 3.7
    z = 6.1
    hi = "Hello!"
    age = "My Age is 21"
    greeting = Hello, I'm Queen, born in 1985 and I've been coding since 4/1/2004 12:00:00 AM.
*/  

Remarks

You can declare a number as a positive, however you cannot declare a variable as positive. Example:

    int x = +5; //Correct. x is now +5, aka 5
    int y = +x; //Wrong. A validation error will be thrown.

Minus ( - )

Description

The - operator functions as a unary or binary operator.

It can be used with any numeric type to calculate the subtraction of its operands. When used in front of a plain number, it functions as that number's negative.

Attention

The + operator cannot be used as a unary operator on variables in Mamba. Thus the following are not supported:

  • -x
  • --x
  • x--

Example

// Simple abstraction of two integers
int x = 5 - 5; 

// Subtracting two decimals
decimal y = 1.4 - 2.3; 

// Subtracting two numbers of a different type
decimal z = 5 - 1.1; 

//Numeric negation
int x = -2;

/*
Results:
    x = 0
    y = 0.9
    z = 3.9
    x = -2
*/  

Remarks

You can negate a number, however you cannot negate a variable. Example:

    int x = -5; //Correct. x is now -5
    int y = -x; //Wrong. y will not be -(-5), thus +5. A validation error will be thrown.

Multiplier ( * )

Description

The * operator functions a binary operation, calculating the product of its operands.

Example

// Simple multiplication of two integers
int x = 5 * 5; 

// Multiplying two decimals
decimal y = 1.4 * 2.3; 

// Multiplying multiple numbers of various types
var z = 5* 10 * 25 * 0.029;

/*
Results:
    x = 25
    y = 3.22
    z = 36.250
*/  

Remarks

Mamba does not support pointers. The * symbol is used merely for multiplication and not for Pointer declaration as seen in other languages (e.g. C, C++, C#)

Divider ( / )

Description

The / operator can be used to calculate the result when dividing its first operand by its second one.

Example

var x = 5/3; 
decimal y = 10.4 / 34.2; 
var z = 100/25.5;
var a = 4/-3.1;
var b = x/y;
var c = x/y/z;

DebugLib.Logger.WriteInfoLine("x = " + x + " with type: " + RuntimeType.GetTypeOf(x));
DebugLib.Logger.WriteInfoLine("y = " + y + " with type: " + RuntimeType.GetTypeOf(y));
DebugLib.Logger.WriteInfoLine("z = " + z + " with type: " + RuntimeType.GetTypeOf(z));
DebugLib.Logger.WriteInfoLine("a = " + a + " with type: " + RuntimeType.GetTypeOf(a));
DebugLib.Logger.WriteInfoLine("b = " + b + " with type: " + RuntimeType.GetTypeOf(b));
DebugLib.Logger.WriteInfoLine("c = " + c + " with type: " + RuntimeType.GetTypeOf(c));

/*
Outputs:
    x = 1 with type: System.Int32
    y = 0.3040935672514619883040935673 with type: System.Decimal    
    z = 3.9215686274509803921568627451 with type: System.Decimal    
    a = 1.2903225806451612903225806452 with type: System.Decimal
    b = 3.2884615384615384615384615379 with type: System.Decimal    
    c = 0.8385576923076923076923076922 with type: System.Decimal
*/

Remarks

When dividing two integers, the result will be an integer.

Persentage ( % )

Description

The % operator can be used to calculate the remainder of a division between two numeric operands.

Example

var x = 5 % 3; 
decimal y = 10.4 % 34.2; 
var z = 100 % 25.5;
var a = 4 % -3.1;
var b = x % y;
var c = x % y / z;

DebugLib.Logger.WriteInfoLine("x = " + x + " with type: " + RuntimeType.GetTypeOf(x));
DebugLib.Logger.WriteInfoLine("y = " + y + " with type: " + RuntimeType.GetTypeOf(y));
DebugLib.Logger.WriteInfoLine("z = " + z + " with type: " + RuntimeType.GetTypeOf(z));
DebugLib.Logger.WriteInfoLine("a = " + a + " with type: " + RuntimeType.GetTypeOf(a));
DebugLib.Logger.WriteInfoLine("b = " + b + " with type: " + RuntimeType.GetTypeOf(b));
DebugLib.Logger.WriteInfoLine("c = " + c + " with type: " + RuntimeType.GetTypeOf(c));

/*
Outputs:
    x = 2 with type: System.Int32
    y = 10.4 with type: System.Decimal  
    z = 23.5 with type: System.Decimal
    a = 0.9 with type: System.Decimal
    b = 2 with type: System.Decimal 
    c = 0.0851063829787234042553191489 with type: System.Decimal
*/

Remarks

When calculating the remainder of two integers, the result is also an integer

Power ( ^ )

Description

The ^ operator can be used to calculate the result when raising the first integral operand to the specified power.

Attention

Both operands (the base value and its power) must be integers If you want to raise a double number to a double power, use CommonLib.Math.Pow(double, double)

Example

var x = 5 ^ 3; 
var y = 10 ^ 3; 
var a = 4 ^ -3;


DebugLib.Logger.WriteInfoLine("x = " + x + " with type: " + RuntimeType.GetTypeOf(x));
DebugLib.Logger.WriteInfoLine("y = " + y + " with type: " + RuntimeType.GetTypeOf(y));
DebugLib.Logger.WriteInfoLine("a = " + a + " with type: " + RuntimeType.GetTypeOf(a));


/*
Outputs:
    x = 6 with type: System.Int32
    y = 9 with type: System.Int32
    a = -7 with type: System.Int32
*/

Remarks

The ^ operator is merely a Math Power operator and cannot be used as a logical XOR found in other languages (e.g. C#)

Comparison


Equal ( == )

Description

The == can be used to examine whether two operands are equal or not. If they are equal, the comparison returns true. Otherwise, it will return false.

For non-complex types (e.g. numeric, string), the == operator examines whether the two operands have the same value.

For complex types, the == operator examines whether the two operands refer to the same object.

Attention

For complex types (e.g. Objects), the == operator ignores the actual values of each property and merely checks the Object's Reference

Example

//Numeric (comparing values)
int intA = 5;
int intB = 5;
int intC = 10;

if(intA == intB){
    DebugLib.Logger.WriteInfoLine("intA and intB are equal -> "+intA+" and  "+intB+" are equal");
}
else{
    DebugLib.Logger.WriteInfoLine("intA and intB are not equal -> "+intA+" and  "+intB+" are not equal");
}

if(intA == intC){
    DebugLib.Logger.WriteInfoLine("intA and intC are equal -> "+intA+" and  "+intC+" are equal");
}
else{
    DebugLib.Logger.WriteInfoLine("intA and intC are not equal -> "+intA+" and  "+intC+" are not equal");
}

//String (comparing values)
string stringA = "Mamba";
string stringB = stringA;
string stringC = "mamba"; 

if(stringA == stringB){
    DebugLib.Logger.WriteInfoLine("stringA and stringB are equal -> "+stringA+" and " + stringB + " are equal");
}
else{
    DebugLib.Logger.WriteInfoLine("stringA and stringB are not equal -> "+stringA+" and " + stringB + " are not equal");
}

if(stringA == stringC){
    DebugLib.Logger.WriteInfoLine("stringA and stringC are equal -> "+stringA+" and " + stringC + " are equal");
}
else{
    DebugLib.Logger.WriteInfoLine("stringA and stringC are not equal -> "+stringA+" and " + stringC + " are not equal");
}

//Objects (comparing origins)
Domain.ApplicationUser userA = Domain.ApplicationUser.GetAll().First();
Domain.ApplicationUser userB = Domain.ApplicationUser.GetAll().First(); //Same reference
Domain.ApplicationUser userC = Domain.ApplicationUser.GetAll().First(x => x.Name == "Queen");//Different reference

if(userA == userB){
    DebugLib.Logger.WriteInfoLine("userA and userB are equal");
}
else{
    DebugLib.Logger.WriteInfoLine("userA and userB are not equal");
}

if(userA == userC){
    DebugLib.Logger.WriteInfoLine("userA and userC are equal");
}
else{
    DebugLib.Logger.WriteInfoLine("userA and userC are not equal");
}

userA.Email = "anemail@company.com";
if(userA == userB){
    DebugLib.Logger.WriteInfoLine("userA and userB are equal, even though their properties have different values");
}
else{
    DebugLib.Logger.WriteInfoLine("userA and userB are not equal");
}


/*
    Output:
    intA and intB are equal -> 5 and  5 are equal
    intA and intC are not equal -> 5 and  10 are not equal
    stringA and stringB are equal -> Mamba and Mamba are equal
    stringA and stringC are not equal -> Mamba and mamba are not equal
    userA and userB are equal
    userA and userC are not equal
    userA and userB are equal, even though their properties have different values
*/

Not Equal ( != )

Description

The != is an inequality operator. It returns false if its operands are equal, true otherwise.

For non-complex types (e.g. numeric, string), the != operator examines whether the two operands have different values.

For complex types, the != operator examines whether the two operands refer to different objects.

Attention

For complex types (e.g. Objects), the != operator ignores the actual values of each property and merely checks the Object's Reference

Example

//Numeric (comparing values)
int intA = 5;
int intB = 5;
int intC = 10;

if(intA != intB){
    DebugLib.Logger.WriteInfoLine("intA and intB are not equal -> "+intA+" and  "+intB+" are not equal");
}
else{
    DebugLib.Logger.WriteInfoLine("intA and intB are equal -> "+intA+" and  "+intB+" are equal");
}

if(intA != intC){
    DebugLib.Logger.WriteInfoLine("intA and intC are not equal -> "+intA+" and  "+intC+" are not equal");
}
else{
    DebugLib.Logger.WriteInfoLine("intA and intC are equal -> "+intA+" and  "+intC+" are equal");
}

//String (comparing values)
string stringA = "Mamba";
string stringB = stringA;
string stringC = "mamba"; 

if(stringA != stringB){
    DebugLib.Logger.WriteInfoLine("stringA and stringB are not equal -> "+stringA+" and " + stringB + " are not equal");
}
else{
    DebugLib.Logger.WriteInfoLine("stringA and stringB are equal -> "+stringA+" and " + stringB + " are equal");
}

if(stringA != stringC){
    DebugLib.Logger.WriteInfoLine("stringA and stringC are not equal -> "+stringA+" and " + stringC + " are not equal");
}
else{
    DebugLib.Logger.WriteInfoLine("stringA and stringC are equal -> "+stringA+" and " + stringC + " are equal");
}

//Objects (comparing origins)
Domain.ApplicationUser userA = Domain.ApplicationUser.GetAll().First();
Domain.ApplicationUser userB = Domain.ApplicationUser.GetAll().First(); //Same reference
Domain.ApplicationUser userC = Domain.ApplicationUser.GetAll().First(x => x.Name == "Queen");//Different reference

if(userA != userB){
    DebugLib.Logger.WriteInfoLine("userA and userB are not equal");
}
else{
    DebugLib.Logger.WriteInfoLine("userA and userB are equal");
}

if(userA != userC){
    DebugLib.Logger.WriteInfoLine("userA and userC are not equal");
}
else{
    DebugLib.Logger.WriteInfoLine("userA and userC are equal");
}

userA.Email = "anemail@company.com";
if(userA != userB){
    DebugLib.Logger.WriteInfoLine("userA and userB are not equal");
}
else{
    DebugLib.Logger.WriteInfoLine("userA and userB are equal");
}


/*
    Output:
    intA and intB are equal -> 5 and  5 are equal
    intA and intC are not equal -> 5 and  10 are not equal
    stringA and stringB are equal -> Mamba and Mamba are equal
    stringA and stringC are not equal -> Mamba and mamba are not equal
    userA and userB are equal
    userA and userC are not equal
    userA and userB are equal   
*/

Greater Than ( > )

Description

The > operator, used with any string, numeric or enumeration type, can be used to check whether the first operant is greater than the second operand.

If the first operand is greater than the second, the > operator returns true. Otherwise, false

For string types, the > operator compares the position of the two strings, in a sorted list from A to Z and a to z .

Example

DebugLib.Logger.WriteInfoLine(5 > 3); //true
DebugLib.Logger.WriteInfoLine(3.1 > 10.12); //false
DebugLib.Logger.WriteInfoLine("A" > "B"); //false
DebugLib.Logger.WriteInfoLine("A" > "a"); //true
DebugLib.Logger.WriteInfoLine("a" > "A"); //false

Less Than ( < )

Description

The > operator, used with any string, numeric or enumeration type, can be used to check whether the first operant is smaller than the second operand.

If the first operand is smaller than the second, the < operator returns true. Otherwise, false

For string types, the < operator compares the position of the two strings, in a sorted list from A to Z and a to z .

Example

DebugLib.Logger.WriteInfoLine(5 < 3); //false
DebugLib.Logger.WriteInfoLine(3.1 < 10.12); //true
DebugLib.Logger.WriteInfoLine("A" < "B"); //true
DebugLib.Logger.WriteInfoLine("A" < "a"); //false
DebugLib.Logger.WriteInfoLine("a" < "A"); //true

Greater Than Or Equal ( >= )

Description

The >= operator, used with any string, numeric or enumeration type, can be used to check whether the first operant is greater than, or equal to the second operand.

If the first operand is greater than the second, or they have the same value, the >= operator returns true. Otherwise, false

For string types, the >= operator compares the position of the two strings, in a sorted list from A to Z and a to z .

Example

DebugLib.Logger.WriteInfoLine(5 >= 3); //true
DebugLib.Logger.WriteInfoLine(5 >= 5); //true
DebugLib.Logger.WriteInfoLine(5 >= 10); //false

DebugLib.Logger.WriteInfoLine("A" >= "A"); //true
DebugLib.Logger.WriteInfoLine("B" >= "A"); //true
DebugLib.Logger.WriteInfoLine("A" >= "a"); //true
DebugLib.Logger.WriteInfoLine("a" >= "A"); //false
DebugLib.Logger.WriteInfoLine("abc" >= "a"); //true

Less Than Or Equal ( <= )

Description

The <= operator, used with any string, numeric or enumeration type, can be used to check whether the first operant is less than, or equal to the second operand.

If the first operand is less than the second, or they have the same value, the <= operator returns true. Otherwise, false

For string types, the <= operator compares the position of the two strings, in a sorted list from A to Z and a to z .

Example

DebugLib.Logger.WriteInfoLine(5 <= 3); //false
DebugLib.Logger.WriteInfoLine(5 <= 5); //true
DebugLib.Logger.WriteInfoLine(5 <= 10); //true

DebugLib.Logger.WriteInfoLine("A" <= "A"); //true
DebugLib.Logger.WriteInfoLine("B" <= "A"); //false
DebugLib.Logger.WriteInfoLine("A" <= "a"); //false
DebugLib.Logger.WriteInfoLine("a" <= "A"); //true
DebugLib.Logger.WriteInfoLine("abc" <= "a"); //false

Conditional


And ( && )

Description

The && operator is a conditional-AND operator, evaluating two boolean operands.

Imporant: The second operand will be evaluated only if the first one is false. Otherwise, it will be ignored.

A conditional-AND evaluation returns true when, and only when all operands are evaluated as true. Otherwise, it returns false (omitting all subsequent evaluations as soon as the first false is detected)

Example

bool a1 = true && true; //true
bool a2 = true && false; //false
bool a3 = false && true; //false
bool a4 = false && false; //false
bool a5 = true && true && false && true; //false


/* 
Here, the this.CanBeSaved() will be ran first. 
If it returns true, then the IsValid() will be ran as well. 
Otherwise, the IsValid() will be ignored
The Save() function will be when and only when 
both CanBeSaved() and IsValid() are true
*/
if(this.CanBeSaved() && this.IsValid()){
    this.Save();
}

Or ( || )

Description

The || operator is a conditional-OR operator, evaluating two boolean operands. Imporant: The second operand will be evaluated only if the first one is true. Otherwise, it will be ignored.

A conditional-OR evaluation returns true when, at least one operand is true , (omitting all subsequent evaluations as soon as the first true is detected). Otherwise, it returns false.

Example

bool a1 = true || true; //true
bool a2 = true || false; //true
bool a3 = false || true; //true
bool a4 = false || false; //false
bool a5 = true || true && false && true && false; //true


/* 
Here, is the IsNull() returns true, then the IsEmpty() will be ignored, letting the code to execute the ShowMessage() function.
If the IsNull() returns false, then the IsEmpty() will be evaluated as well. 
The ShowMessage() function will be avoided when, and only when, both IsNull() and IsEmpty() are false.
*/
if(aString.IsNull() || aString.IsEmpty()){
    ShowMessage("Please, provide a value.");
}

Not ( ! )

Description

The ! operator is a logical-negation operator that negates its operand. It functions only with boolean variables and values and returns true when the operand is false, or false otherwise.

Example

    bool a1 = !true; //false
    bool a2 = !false; //true

    //Will run Save() only if the string.IsNullOrWhiteSpace() is false
    if(!string.IsNullOrWhiteSpace(password)){
        user.Save();
    }