Microsoft Official Silent lab Video Solution: Different Types of Operators
Time to Complete: 10 Minutes
Solution Silent Video:
Assignment operators
// An example of assignment operators and their output.
static void Example1()
{
int i = 1;
// Using the = operator. i is assigned the value of i, plus 1. i = 2.
i = i + 1;
info(strFmt(“Example 1: The result is “), i); // The result is 2.
}
static void Example2()
{
int i = 1;
// Using the += operator. i is assigned the value of i, plus 1.
// i = 2 (i = i + 1).
i += 1;
info(strFmt(“Example 2: The result is “), i); // The result is 2.
}
static void Example3()
{
int i = 1;
// Using the ++ operator. i is incremented by 1, and then
// by 1 again in the second statement. The final value of i is 3.
i++;
++i;
info(strFmt(“Example 3: The result is “), i); // The result is 3.
}
static void Example4()
{
int i = 1;
// Using the -= operator. i is assigned the value of i minus 1.
// i = 0 (i = i – 1).
i -= 1;
info(strFmt(“Example 4: The result is “), i); // The result is 0.
}
static void Example5()
{
int i = 1;
// Using the — operator. i is decremented by 1, and then by
// 1 again in the second statement. The final value of i is -1.
i–;
–i;
info(strFmt(“Example 5: The result is “), i); // The result is -1.
}
// Arithmetic operators
int a = 1 << 4; // Perform four left shifts on 1 (1*2*2*2*2). a=16.
int b = 16 >> 4; // Perform four right shifts on 16 (16/2/2/2/2). b=1.
int c = 4 * 5; // Multiply 4 by 5. c=20.
int d = 20 / 5; // Divide 20 by 5. d=4.
int e = 100 div 21; // Return the integer division of 100 by 21. e=4 (4*21 = 84, remainder 16).
int f = 100 mod 21; // Return the remainder of the integer division of 100 by 21. f=16.
int g = ~1; // Binary negate 1 (all bits are reversed). g=-2.
int h = 1 & 3; // Binary AND. Return the bits that are in common in the two integers. h=1.
int i = 1 | 3; // Binary OR. Return the bits that are set in either 1 or 3. i=3.
int j = 1 ^ 3; // Binary XOR. Return the bits that are set in 1 and NOT set in 3, and vice versa. j=2.
int k = 1 + 3; // Add 1 and 3. k=4.
int l = 3 – 1; // Subtract 1 from 3. l=2.
int m = (400 > 4) ? 1 : 5; // If 400>4, 1 is returned. Otherwise, 5 is returned. Because 400>4, 1 is returned. m=1.
Expression operators
The as keyword
static void AsKeywordExample()
{
// DerivedClass extends BaseClass.
BaseClass basec;
DerivedClass derivedc;
// BottomClass extends DerivedClass.
BottomClass bottomc;
derivedc = new DerivedClass();
// AS is not required for an upcast assignment like this.
basec = derivedc;
// AS is required for a downcast assignment like this.
derivedc = basec as DerivedClass;
bottomc = new BottomClass();
// AS causes this invalid downcast to assign null.
bottomc = basec as DerivedClass;
}
Code examples for the is keyword
// The compiler issues an error for the following code.
// The compiler ascertains that the Form class and the Query class are not
// part of the same inheritance hierarchy. Both the Form class and the Query class
// extend the TreeNode class, but neither Form nor Query is a subtype of the other.
Form myForm = new Form();
info(strFmt(“%1”, (myForm is Query)));
// The Infolog displays 0 during run time, where 0 means false. No supertype
// object can be considered to also be of its subtype class.
TreeNode myTreeNode = new TreeNode();
info(strFmt(“%1”, (myTreeNode is Form)));
// The Infolog displays 0 during run time, where 0 means false. A null
// reference causes the is expression to return false.
Form myForm;
info(strFmt(“%1”, (myForm is Form)));
// The Infolog displays 1 during run time, where 1 means true.
// An object is an instance of its own class type.
Form myForm = new Form();
info(strFmt(“%1”, (myForm is Form)));
// The Infolog displays 1 during run time, where 1 means true.
// Every subtype is also of its supertype.
Form myForm = new Form();
info(strFmt(“%1”, (myForm is TreeNode)));
// The Infolog displays 1 during run time, where 1 means true.
// The type of the underlying object matters in the is expression,
// not the type of the variable that references the object.
Form myForm = new Form();
TreeNode myTreeNode;
myTreeNode = myForm; // Upcast.
info(strFmt(“%1”, (myTreeNode is Form)));
Code examples for relational operators
“Jones” like “Jo?es” // Returns true, because the ? is equal to any single character.
“Fabrikam, Inc.” like “Fa*” // Returns true, because the * is equal to zero or more characters.
(( 42 * 2) == 84) // Returns true, because 42*2 is equal to 84.
today() >= 1\1\1980 // Returns true, because today is later than January 1, 1980.
((11 div 10) >= 1) // Returns true, because 11 div 10 is 1 (therefore, >= 1 is true).
(11<= 12) // Returns true, because 11 is less than 12.
((11 div 10) > 1) // Returns false, because 11 div 10 is 1.
(11 div 10) < 1) // Returns false, because 11 div 10 is 1.
(11 != 12) // Returns true, because 11 is not equal to 12.
(1 == 1) && (3 > 1) // Returns true, because both expressions are true.
