o Operator Precedence Chart o Casing Styles in .NET o Decimal to Binary Conversion o Binary to Decimal Conversion o Converting Negative Numbers to Binary
List of Reserved Keywords
The C# language has many reserved keywords that you cannot use as identifiers. The following is the full list of those reserved key words:
abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using virtual void volatile while
The following are contextual keywords that are only treated as keywords at special events and locations. add alias ascending by descending equals from get global group into join let on orderby partial remove select set value var where yield
Operator Precedence Chart Operator precedence determines which operators and expressions should be caculated first. The following is the full operator precedence chart of C#. Operators in the same row have the same precedence. The associativity determines how to evaluate expressions having multiple operators with the same level of precedence. Operator Type Associativity . () [] ++ -- new typeof sizeof checked unchecked member access method call element access postfix increment postfix decrement object creation get the type of object get the size in bytes of a type checked evaluation unchecked evaluation left-to-right + - ! ~ ++ -- (type) unary plus unary minus logical negation bitwise not operator prefix increment prefix decrement casting right-to-left * / % multiplication division modulus/remainder left-to-right + - << >> addition subtraction left shift operator right shift operator left-to-right < > <= >= is as less than greater than less than or equal to greater than or equal to type comparison type conversion left-to-right != == is not equal to is equal to left-to-right & bitwise AND left-to-right ^ bitwise XOR left-to-right | bitwise OR left-to-right && logical AND left-to-right || logical OR left-to-right ?? null coalescing right-to-left ?: conditional right-to-left = *= /= %= += -= <<= >>= &= ^= |= assignment multiplication assignment division assignment modulus assignment addition assignment subtraction assignment left shift assignment right shift assignment bitwise AND assignment bitwise XOR assignment bitwise OR assignment right-to-left
Casing Styles in .NET Three capitalization styles are used in C# and .NET. The PascalCasing , camelCasing and UPPERCASE. Pascal Casing starts with a capital letter followed by small letters. All succeeding words in Pascal Casing should also start with capital letters. Examples are TheNumber, MyCar, AnimalFarm.Camel Casing is simillar to Pascal Casing only that it starts its name with a small letter. Examples are theNumber, myCar, animalFarm. Uppercase means every character is written in uppercase such as PI or PRICE. Pascal Casing is used for:
o Methods. All the methods in the .NET Framework are named using the Pascal Casing. If you are creating methods, use Pascal Casing as well. Although there is one exception. When naming event handlers for controls on a form in C#, the method usually starts with the name of the control (which starts with lower case) followed by an underscore and then the name of the event.
o Property. When naming a property, we usually follow the name of the private data member it references. For example, if you have a member called myName, we name the corresponding property with MyName.
o Class. Classes are also named using Pascal Casing. It is apparent on all the classes of the .NET Framework.
o Interface. When naming an interface, always precede it with a capital I followed by the name of the interface starting also with a capital letter. For example, If I want to make an interface named Clickable, I will name it IClickable.
o Namespaces. When you write your namespace, you should use Pascal Casing as well.
Camel Casing is used for: o Fields o Method Variables o Names of Controls in Windows Forms o Method Parameters Uppercase is used when writing constants.
Decimal to Binary Conversion Binary numbers in the computer world are often grouped by 8 digits or bits to form 1 byte. To start converting a decimal number to binary, the first step is to reserve 8 blank slots and we will put binary numbers on each slot as we progress. Suppose that we want to convert the number 54 to binary:
Now, we need to remember a simple sequence. Binaries are base 2, so we use 2 as a base and raise it to a power starting with 0.
By getting the answer for each 1, we get the following.
Notice that we start with 1 and the next one is double its value which is 2. The following ones are just doubled values of their preceding values. With this values we can now get the binary equivalent of our decimal number. To start, find the highest number that does not exceed our current value whic is 54. In this case, 32. We place a 1 in the position where the 32 is located.
We now subtract 32 from 54 (54 - 32 = 22). Our current value is now 22. Repeat the step of finding the highest value that does not exceed our current value which is 22. The number 16 qualifies our condition so again, place a 1 where 16 is located.
We then subtract 16 from 22 (22 - 16 = 6) making 6 our new current value. Find the largest number in the sequence that does not exceed six. 8 exceeds 6, so we should use 4 and place again a 1 in its position.
We subtract 4 from 6 (6 - 4 = 2) and we find the value that does not exceed our new current value 2. There's only one number in the sequence that does not exceed 2, and thats 2 itself. So place again 1 in its position. Subtracting 2 from our current value 2 results to 0. This time, we can now stop adding 1's.
The final process is placing 0's on all blank spaces.
We finally arrived to the binary equivalent of 54 which is 00110110. We can omit the leading 0's and write 110110 as the binary equivalent of 54. Please note that higher numbers require more bits (slots) for conversions. How can we know if a number fits 8 bits. Simply double the last number which is 128 and we get 256. All the number below 256 can be converted using 8 bits. If a decimal to convert is 256 or higher, then we need to add more bits when converting. For example, if we want to convert 312, then we will be needing 9 bits.
The value of the 9th bit is 2 8 or 256 which is simply a doubled value of its previous bit which is 128. 9 bits can only contain values below 512 which is twice the value of 256. Again, if you have a value equal or greater than 512, then you need to add again 1 more bits.
Binary to Decimal Conversion Converting from binary to decimal is easy. You first need to know the positional values of each bit. Suppose we have a binary number 10100111 that we need to convert to decimal.
Positional values starts from 2 0 which is 1 to 2 n-1 where n is the number of bits or digits. Since we have 8 bits, the last positional value is 2 8-1 = 2 7 = 128. We write the positional values from right to left. Once we have determined the positional values, we simply add all the positional values that has 1 as its bit.
We now arrived at 167 as the final decimal answer for our binary number 10100111.
Converting Negative Numbers to Binary Converting negative numbers to binary is not simple and can be confusing. C# and .NET usesTwo's Complement Notation. To show how the Two's Complement Notation works, let's convert -7 to its binary equivalent. Suppose that -7 has given a type of int so it has 32-bits (32 binary digits). int value = -7; The first step is to convert a negative value to its positive value so -7 becomes +7 or simple 7. We then convert 7 to its binary representation. 7 = 00000000000000000000000000000111 The value 7 is equivalent to 111 in binary but assuming that we stored the value in an int variable, then it will have 32-bits that's why we add leading 0's to fill all 32 slots. After converting the negative number's positive equivalent to binary, we use one's complement to each bits. In C#, this is equivalent to using the bitwise NOT operator (~). Doing this operation simply reverses the value of each digit. So the binary representation of 7 when one's complement is used will become: ~7 = 11111111111111111111111111111000 To perform two's complement, simply add one the the new binary value. 11111111111111111111111111111000 + 00000000000000000000000000000001 ---------------------------------- 11111111111111111111111111111001 The result is the binary representation of -7. At first look, it won't seem like it realy is negative 7. That's why I said earlier that it can be confusing. I mean, if you will convert the final binary result to decimal using the techniques we have learned, you will arrive at value 4,294,967,289. But C# and .NET uses the leftmost bit as an indication of whether a number is positive or negative. It means, that if the leftmost bit is 1, then the number is considered negative, if it is 0, the number is considered positive. Therefore, when using int as the type, the 32nd bit is not included when calculating a values decimal value. After all 4,294,967,289 will not fit in an int variable. You should use long or uint instead. To prove that the final binary result is really -7. We can add it to the binary value of 7. The result should be 0 (-7 + 7 = 0). 11111111111111111111111111111001 + 00000000000000000000000000000111 ---------------------------------- 00000000000000000000000000000000 If you don't know how to add binaries, the following is the three possible combinations and their results. 1 + 0 = 1 0 + 1 = 1 1 + 1 = 0 carry 1 Since the first pair of digit is both 1, the first digit of the answer is 0 and 1 is carry over to the next column and so on until we reach the final column. In the final column there should still be one being carried over from the last column so the result is still 0 and we discard the 1 to be carried.