C# Basic (6)

1. An operator is a symbol that indicates an operation to be performed on one or more arguments.

The values being operated on are called operands.

The most basic C# operators include multiplication (*), division (/), addition and unary plus (+), subtraction and unary minus (-), modulus (%) and assignment (=).

C# defines the + and += operators for the String class and even allows for the use of increment operators (++) and (–) on unique language constructs such as delegates.

 

2. C# Operator Precedence

Category of Operator
Operators
Primary
(x), x.y, f(x), a[x], x++, x–, new, typeof, sizeof, checked, unchecked
Unary
+, -, !, ~, ++x, –x, (T)x
Multiplicative
*, /, %
Additive
+, –
Shift
<<, >>
Relational
<, >, <=, >=, is
Equality
==
Logical AND
&
Logical XOR
^
Logical OR
|
Conditional AND
&&
Conditional OR
||
Conditional
?:
Assignment
=, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=

a programming convention whereby spaces are used to indicate which operators they think will have precedence, a kind of self-documenting mechanism.                                                    a = b*c + d;

 

3.

// Retrieve the System.Type object from the class definition.
Type t1 = typeof(Apple);

// Retrieve the System.Type object from the object.
Apple apple = new Apple();
Type t2 = apple.GetType();

sizeof :                                                                                                                                                  First, you can use the sizeof operator with value types only. Therefore, although this operator can be used on class members, it cannot be used on the class type itself.                                                                                                                                       Second, this operator can be used only in a method or code block marked as unsafe.

To retrieve the positive form of an operand, use the Math.Abs function.

A compound assignment operator is a combination of a binary operator and the assignment (=) operator. Its syntax is                                                                                            x op = y

 

4.

using System;

class NumericTest
{
    public NumericTest(int i)
    {
        this.i = i;
    }

    protected int i;
}

class RelationalOps1App
{
    public static void Main()
    {
        NumericTest test1 = new NumericTest(42);
        NumericTest test2 = new NumericTest(42);

        Console.WriteLine("{0}", test1 == test2);
    }
}

This example prints a statement of false. Remember that when you instantiate an object, you get a reference to a heap-allocated object. Therefore, when you use a relational operator to compare two objects, the C# compiler doesn’t compare the contents of the objects. Instead, it compares the addresses of these two objects.

The System.Object class has a method called Equals designed for just this purpose. For example, the following code performs a comparison of the object contents as you would expect and returns a value of true:

using System;

class RelationalOps2App
{
    public static void Main()
    {
        Decimal test1 = new Decimal(42);
        Decimal test2 = new Decimal(42);

        Console.WriteLine("{0}", test1.Equals(test2));
    }
}

 

5. when you’re dealing with objects, you’re not dealing with simple stack allocated elements that are easily copied and moved around. When manipulating objects, you really have only a reference to a heap-allocated entity. Therefore, when you attempt to assign an object (or any reference type) to a variable, you’re not copying data as you are with value types. You’re simply copying a reference from one place to another.

using System;

class Foo
{
    public int i;
}

class RefTest2App
{
    public void ChangeValue(Foo f)
    {
        f.i = 42;
    }

    public static void Main()
    {
        RefTest2App app = new RefTest2App();

        Foo test = new Foo();
        test.i = 6;

        Console.WriteLine("BEFORE METHOD CALL");
        Console.WriteLine("test.i={0}", test.i);
        Console.WriteLine("\n");

        app.ChangeValue(test);

        Console.WriteLine("AFTER METHOD CALL");
        Console.WriteLine("test.i={0}", test.i);
        Console.WriteLine("\n");
    }
}

In most languages—Java excluded—this code would result in a copy of the test object being created on the local stack of the RefTest2App.ChangeValue method. If that were the case, the test object created in the Main method would never see any changes made to the f object within the ChangeValue method. However, once again, what’s happening here is that the Main method has passed a reference to the heap-allocated test object. When the ChangeValue method manipulates its local f.i variable, it’s also directly manipulating the Main method’s test object.

 

Advertisements
This entry was posted in C#. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s