You are on page 1of 7

Reference

The following page contains quick references and general informations about the C# and .NET framework.

o o o o o o

List of Reserved Keywords Operator Precedence Chart Casing Styles in .NET Decimal to Binary Conversion Binary to Decimal Conversion 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 byte class delegate event fixed if internal new override readonly short struct try unsafe volatile
locations.

as case const do explicit float implicit is null params ref sizeof switch typeof ushort while

base catch continue double extern for in lock object private return stackalloc this uint using

bool char decimal else false foreach int long operator protected sbyte static throw ulong virtual

break checked default enum finally goto interface namespace out public sealed string true unchecked void

The following are contextual keywords that are only treated as keywords at special events and

add equals

alias from

ascending get

by global

descending group

into partial var

join remove where

let select yield

on set

orderby value

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 . () [] ++ new typeof sizeof checked unchecked + ! ~ ++ (type) * / % + << >> < > <= >= is as

Type 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 unary plus unary minus logical negation bitwise not operator prefix increment prefix decrement casting multiplication division modulus/remainder addition subtraction left shift operator right shift operator less than greater than less than or equal to greater than or equal to type comparison type conversion

Associativity

left-to-right

right-to-left

left-to-right

left-to-right

left-to-right

Operator != == & ^ | && || ?? ?: = *= /= %= += -= <<= >>= &= ^= |=

Type is not equal to is equal to bitwise AND bitwise XOR bitwise OR logical AND logical OR null coalescing conditional 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

Associativity left-to-right left-to-right left-to-right left-to-right left-to-right left-to-right right-to-left right-to-left

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:

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.

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 o

Class. Classes are also named using Pascal Casing. It is apparent on all the classes of the .NET Framework. 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.

Namespaces. When you write your namespace, you should use Pascal Casing as well.

Camel Casing is used for:

o o o o

Fields Method Variables Names of Controls in Windows Forms 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. Theres 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 1s.

The final process is placing 0s on all blank spaces.

We finally arrived to the binary equivalent of 54 which is 00110110. We can omit the leading 0s 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 28 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 20 which is 1 to 2n-1 where n is the number of bits or digits. Since we have 8 bits, the last positional value is 28-1 = 27 = 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 uses Twos Complement Notation. To show how the Twos Complement Notation works, lets 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 thats why we add leading 0s to fill all 32 slots. After converting the negative numbers positive equivalent to binary, we use ones 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 ones complement is used will become: ~7 = 11111111111111111111111111111000 To perform twos complement, simply add one the the new binary value. 11111111111111111111111111111000 + 00000000000000000000000000000001 ---------------------------------11111111111111111111111111111001 The result is the binary representation of -7. At first look, it wont seem like it realy is negative 7. Thats 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 dont 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.

You might also like