IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesC#Operators  Print This     

Cross Ref > Operators

By Mike Prestwood

C# versus JavaScript: A side by side comparison between C# and JavaScript.

 
Operators
 

A language symbol used for assignment, comparison, computational, or as a logical.

Assignment

[Other Languages] 

Languages Focus

Common assignment operators for languages include =, ==, and :=. An assignment operator allows you to assign a value to a variable. The value can be a literal value like "Mike" or 42 or the value stored in another variable or returned by a function.

C#:   =

C# uses = for it's assignment operator.

Syntax Example:
int Age;
string FullName;
  
Age = 42;
FullName = "Randy Spitz";
JavaScript:   =

JavaScript uses = for it's assignment operator.

Syntax Example:
//Regular assignment.
LastName = 'Spitz';
  
//Variable declaration with assignment.
var FullName = 'Randy Spitz';
var Age = 38;




Comparison Operators

[Other Languages] 

General Info: Round Floating Point Numbers

When comparing floating point numbers, make sure you round to an acceptable level of rounding for the type of application you are using.

Languages Focus

A comparison operator compares two values either literals as in "Hello" and 3 or variables as in X and Counter. Most languages use the same operators for comparing both numbers and strings. Perl, for example, uses separate sets of comparison operators for numbers and strings.

C#:   ==, !=

Common comparison operators:

== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

Syntax Example:
//Does C# evaluate the math correctly? No!
if (.1 + .1 + .1 == .3)
MessageBox.Show("correct");
else
MessageBox.Show("not correct");
JavaScript:   ==, !=

Common comparison operators:

== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

Syntax Example:
//Does JavaScript evaluate the math correctly? No!
if (.1 + .1 + .1 == .3) {
document.write("correct");
}
else {
document.write("not correct");
}




Empty String Check

[Other Languages] 

Languages Focus

An empty string is a zero length string, a string that is equal to null (""), or not assigned. In some languages, you can check if a string is empty by comparing it to an empty string (""). Some languages distinguish between nil and null ("") so checking if the length is 0 is easier.

C#:   String.IsNullOrEmpty

The .Net framework offers a static method in the string class: String.IsNullOrEmpty.

Syntax Example:
String s;
 
s = "";
//s = null; //Uncomment to test 2nd case.
 
if (String.IsNullOrEmpty(s))
{
  MessageBox.Show("empty string");
}
[Not specified yet. Coming...]




Logical Operators

[Other Languages] 

Languages Focus

Logical operators perform conditional and, or, and not operations. Some languages support both binary logical operators that link two and unary logical operators negate (make opposite) the truth value of its argument. Finally, some languages short circuit logic. For example, with this or that, if this is an expression returning true, then that is never executed.

C#: 

Same as C++ and Java. C# logical operators:

& and, as in this and that No Short Circuit
&& and, as in this and that short circuits
| or, as in this or that No Short Circuit
|| or, as in this or that short circuits
! Not, as in Not This
^ either or, as in this or that but not both

Syntax Example:
//Given expressions a, b, c, and d:
if !((a && b) && (c || d)) 
{
  //Do something.
}
JavaScript: 

JavaScript logical operators:

&& and, as in this and that
|| or, as in this or that
! Not, as in Not This

JavaScript always short circuits. Given the expression this || that, if this evaluates to true, then that is never executed.

Syntax Example:
//Given expressions a, b, c, and d:
if !((a && b) && (c || d)) {
  //Do something.
}




String Concatenation

[Other Languages] 
C#:  "String Concatenation" +

C# performs implicit casting of numbers to strings. To concatenate two strings, a string to an integer, or a string to a floating point number, use the + operator. For example, to convert a floating point number to a string just concatenate an empty string to the number as in "" + 3.2.

Alternatively, you can use the System.Text.StringBuilder class which frequently but not always provides faster code.

Syntax Example:
String FirstName;
String LastName;
Int16 Age;
FirstName = "Mike";
LastName = "Prestwood";
Age = 43;
Console.WriteLine(FirstName + " " + LastName + " is " + Age + ".");
  
//Implicit casting of numbers.
//
//This fails:
//MessageBox.Show(3.3);
//
//This works:
MessageBox.Show("" + 3.3); 
JavaScript:  "String Concatenation" +

To concatenate two strings, a string to an integer, or a string to a floating point number, use the + operator. JavaScript performs implicit casting when concatenating a string and a number. For example, to convert a floating point number to a string just concatenate an empty string to the number as in "" + 3.2.

Syntax Example:
 
// -->




Unary Operators

[Other Languages] 

General Info: Unary Operator

An operation with only one operand (a single input). Common unary operators include + plus, - minus, and bitwise not. Some operators can function as both unary and binary operators. For example, + and - operators can serve as either.

Languages Focus

What unary operators are supported in additoin to the standard plus, minus, and bitwise not.

C#: 

An operation with only one operand (a single input). The following are the C# unary operators: +, -, !, ~, ++, --, true, or false. 

JavaScript: 

An operation with only one operand (a single input). JavaScript unary operators include ++ and --. They can be used either before or after a variable as in: a++, b--, and ++a, and --b.

Examples:

iCounter++;
iCounter--;
 
++iCounter;
--iCounter;
Syntax Example:
var iCounter=0;
 
for (iCounter=0;iCounter<=5;iCounter++)
{
document.write("Count is " + iCounter + "<br>");
}




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]