Oct 30, 2011

Operators


An operator in C # is a symbol consisting of one or more characters which can perform a certain operation between one or more data and produces a result.

    The following describes what the operators included in the language classified by type of operations that allow, but we must keep in mind that C # allows you to redefine the meaning of most of the operators depending on the type of data on which apply, so that what is counted here corresponds to the most common uses of them:
  • Arithmetic: Arithmetic operators included in C # are typical of plus (+), subtraction (-), product (*),division (/) and modulus (%) also, operators of "unary minus" (-) and "more unary" (+)

    Related arithmetic operations are a pair of operators called checked and unchecked that control in order to capture overflows that may occur if to perform this type of operations the result is greater than the capacity of the data type of its operands. These operators are used as follows:

checked (<expresiónAritmética>)
unchecked (<expresiónAritmética>)
Both operators <expresiónAritmética> calculate the result and return it if during the calculation does not produce any overflow. However, in case of overflow each acts in a different way: checked causes a compilation error is a constant expression <expresiónAritmética> and System.OverflowExceptionexception if it is, while unchecked returns the result of the arithmetic expression truncated to fit the expected size.
By default, in the absence of operators checked and unchecked what is done is to evaluate the data arithmetic operations between constants and checked if they apply and operations between non-constant data as if they had applied unchecked.
  • Logical operations: This includes operators that allow typical logical operations "and" (& & and &),"Or" (| | y |), "Not" (!) And "xor" (^)
The & & and | | is different from & and | in the first perform lazy evaluation and the latter not. Lazy evaluation is that if the result of evaluating the first operand to deduce the result of the operation, then the second is not evaluated and the result is returned directly, while lazy evaluation is not always evaluate both operands. That is, if the first operand of an operation is false & & false is returned directly, without evaluating the second, and if the first operand of a | | true is true is returned directly, without evaluating the other.
  • Relational operations: We have included the traditional equality operators (==), inequality (=),"greater than" (>), "less than" (<), "greater than or equal to" (> =) and "less or equal than "(<=)
  • Bit manipulation operations, operators are included that allow bitwise operations "and" (&), "or" (|),"not" (~), "xor" (^), shift to left ( <<) and right shift (>>) The <<operator shifts left in zeros, while the type of filling by>> depends on the type of data on which it applies: if it is a signed data remains the sign, and otherwise filled with zeros.
  • Assignment operations: For assignments in C # using the = operator, the operator in addition to the assignment they are asked to return the assigned value. For example, the expression a = b assigns the variable to the value of the variable by returns that value, whereas the expression c = a = b assigns to the variables c and the value of b (the = operator is right associative )
Also included compound assignment operators that save typing when assignments are as common as:
 temperature = temperature + 15 / / Not using Compound assignment 
temperature + = 15; / / Using Compound Assignment
The two lines are equivalent, because the compound operator + = assigned to its first operand the value it had more of its second operand (ie, adds the second operand) As you see, allows very compact code.
Besides the compound assignment operator + =, we also offer compound assignment operators for most binary operators already seen. These are: + =, -=, *=, / =,% =, & =, | =, ^ =, <<= And>> =. Note that there are versions made ​​for the binary operators & & and | |.
Two other assignment operators are included increase (+ +) and decrement (-) Such operators, respectively, increase and decrease in unit value of the variable on which they apply. Thus, these lines of code are equivalent:
 temperature = temperature +1 / / Not using Compound assignment or increased 
temperature + = 1; / / Using Compound Assignment
temperature + + / / using increments

If the + + operator is placed after the variable name (as in the example) returns the value of the variable before you increase it, whereas if placed before it returns the value after increasing, and so does the - operator. For example:
 c = b + + / / c is assigned the value then increases by b 
c = + + b / / is increased by the value of c is then assigned
The advantage of using the + + and - is that many machines are more efficient than other ways to perform addition or subtraction of a unit, because the compiler can translate into a single machine code instruction [5] .
  • String operations: To perform string concatenation can use the same operator to perform addition, since C # has redefined its meaning so that when applied between operands to be strings or be a string and a character that does is concatenate . For example,? Hello? +? world? returns? Hello world?, and? Hello world? + 'Or' too.
  • Access operations tables: A table is an ordered set of objects of fixed size. To access any element of this set is applied postfix operator [] on the table to indicate in brackets the position of the object you want to access within the set. That is, this operator is used as follows:
[<PosiciónElemento>]
An example of its use in the assignment to the element at position 3 in a name table tablaPrueba the value of the element at position 18 of the table is as follows:
 tablaPrueba [3] = tablaPrueba [18]; 

The tables below are carefully
  • Conditional Operator: It is the only operator on C # that takes 3 operands, and is used like this:
 <condition>?  <expresión1>: <expresión2> 
The meaning of the address is as follows: <condition> evaluates true is returned if the result of evaluating <expresión1>, and if false is returned the result of evaluating <condición2>. An example of its use is:
 b = (a> 0)?  a: 0, / / assume a and b of integer types 
In this example, if the value of the variable is greater than 0 is assigned to ab value, while otherwise the assigned value is 0.
Keep in mind that this operator is right associative, so an expression like? B: c 'd: e is equivalent to a? B: (c? D: e)
Do not confuse this operator if the conditional statement that will be discussed in Item 8: Instructions, for although its usefulness is similar to this one,? Returns a value and if not.
  • Operations of delegates: delegate is an object that can store references to one or more methods and through which you can call these methods. To add objects to a delegate using the + and + =, while for operators used them off - and -=. These concepts are studied in detail in the corresponding topic.
  • Operations Object Access: To access the members of an object using the operator., Whose syntax is:
 <object>. <member> 
If an object is, examples of how to call different members of it are:
 ab = 2 / / assign your property to the value 2 
f () / / We call the method f ()
ag (2) / / call your method g () passing as parameter
/ / The integer value 2
Do not worry if you do not know the concepts of methods, properties, events and delegates in this example is based, as is explained in detail in subsequent topics.
  • Operations with pointers: A pointer is a variable that stores a reference to a memory address. To obtain the memory address of an object using the & operator to access the contents of the memory address stored in a pointer * operator is used to access a member of an object whose address is stored in a pointer use ->, and to reference a memory address of a pointer on how we apply the operator []as pointer [offset]. All these concepts are explained more fully in Topic 18: Code insecure.
  • Operations of obtaining information about types: Of all the operators that allow us to obtain information about data types the most important is typeof, whose manner of use is:

typeof (<nombreTipo>)
This operator returns an object of type System.Type with information about the type of name that we can consult <nombreTipo> through members offered by the object. This information includes details such as what are its members, what is your kind father or what namespace it belongs.
If we want to determine whether a given expression is of one kind or another, then the operator is using it, whose syntax is:
 <expression> is <nombreTipo> 
The meaning of this operator is: <expression> evaluated. If the result of this is the type whose name is indicated in <nombreTipo> returns true, and if not, it returns false. As discussed in Topic 5: Classes,this operator is often used in polymorphic methods.
Finally, C # includes a third operator that allows information about a data type: sizeof This operator giving the number of bytes that will occupy in memory objects of a type, and is used like this:

sizeof (<nombreTipo>)
sizeof can only be used within unsafe code, which for now just consider that they are areas where you can use code pointers. Only in the Item 18: Unsafe Code when they are discussed at length. 
In addition, sizeof can only be applied on data type names which objects can be stored directly in stack.This means that structures are (will be in Issue 13) or enumerated types (will be in Issue 14)
  • Object creation operations: The operator most typically used to create objects is new, used like this:
 new <nombreTipo> (<parameters>) 
This operator creates an object of <nombreTipo> your constructor passing the parameters listed in <parameters> and returns a reference to it. Depending on the type and number of these parameters will be called either of the constructors of the object. Thus, assuming that a1 and a2 are variables of type plane, examples of using the new operator are:
 A1 = new Airplane Airplane () / / constructor is called with no parameters 
/ / Plane
Plane a2 = new Airplane ("Hunting") / / constructor is called Plane that
/ / Takes a string as a parameter
If the type has been called for the creation of the object is a class, it will be created on the heap, and what new will return a reference to the address stack that stores a reference to the direction of the object on the heap. However, if the object to create a structure or part of an enumerated type, then it will be created directly on the stack and the reference returned by the new will refer directly to the object created. For these reasons, classes are called reference types because of its objects on the stack only stores a reference to the dynamic memory address where we really are, while the structures and enumerated types are known as value types and its objects are stored directly on battery.
C # provides another operator also allows us to create objects. This is stackalloc, and is used like this:

stackalloc <nombreTipo> [<nElementos>]

Operator T his does is create a table stack as many items as directed <nombreTipo> type <nElementos> and return the memory address that it has been created. For example:

int * p = stackalloc [100] / / p points to a table of 100 integers.

stackalloc only be used to initialize pointers to objects of value types declared as local variables.
  • Conversion Instructions: To convert a few other objects using the cast operator, which consists not only in turn precede the expression in parentheses the name of the type you want to convert the result of evaluating it. For example, if l is a variable of type long and you want to store its value into a variable of type int call i would have to convert prior to int value as follows:
 i = (int) l / / assign i the result of converting the value of l 
/ / A int
The types int and long are predefined in C # and can store signed integer values. The ability to int is 32 bits, while the long is 64 bits. Therefore, unless we use the conversion operator, the compiler will not let us do the assignment as being larger the capacity of long, not every value that can be stored in a long is why being able to store in an int. That is, is not valid:
 i = l / / ERROR: The value of l does not have to fit in i 
This restriction is imposed on the allocation because the compiler errors may occur without it very difficult to detect unexpected truncation due to the value of the source variable is greater than the capacity of the destination variable.
There is another operator which perform conversion very similar to that already seen. This is the asoperator, which is used as follows:  
 <expression> as <tipoDestino> 
What it does is return the result of converting the result of evaluating the rate indicated in <expression><tipoDestino> For example, to store in a variable p the result of converting an object type Person rate t would:
 p = t as Person; 

The only differences between using one or another conversion operator are:
    • as only applies to reference types and only those cases where there are predefined in the language conversions. As discussed below, this includes only one type conversions between types and their parents and between type and types sons.
One consequence of this is that the programmer can define how to make type conversions defined by him and others by the operator (), but not by as. This is because as you want only indicates that a reference to an object in memory dynamics are treated as if the object were otherwise, but does not involve any conversion. However, () yes that means if the <tipoDestino> conversion is not compatible with the type of object referenced. Obviously, the operator will be applied much faster in cases where it is not necessary to convert.
    • Where conversion is requested to make returns as invalid null while () throws aSystem.InvalidCastException.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes