Skip to main content

C# - Operators


Operators


In this session we are going to discuss about various types of operators in use in C#


  • Arithmetic Operator
  • Relational Operator
  • Logical Operator
  • Bitwise Operator
  • Assignment Operator
  • Misc Operator
  • Conditional Operator


Arithmetic Operator

+ - * / % ++ -- 

Use Arithmetic Operator 





Relational Operator

== != > < >= <=

Use Relational Operator





Logical Operator

 && || !

Use Logical Operator





Bitwise Operator

~ ^ | & << >>

Use Bitwise Operator

In case of bitwise operator we need to discuss about what operations these symbols perform.

suppose we have 2 variables A and B. 

if A = 9 and B = 12
then A in binary is 1001, B in binary is 1100

Then A | B = A OR B = 00001001 OR 00001100 =0 0 0 0 1 1 0 1 = 13
Then A & B = A AND B = 00001001 AND 00001100 = 0 0 0 0 1 0 0 0 = 8 
Then A^B = A XOR B = 00001001 XOR 00001100 = 0 0 0 0 0 1 0 1 = 5

Then A<<2 = Shift of A bitwise by 2 digits on leftside = (00001001)<< 2 = (00100100) = 36

Then A>>2 = Shift of A bitwise by 2 digits on rightside = 2>>(00001001) = (00000010) = 2

Now lets test these examples in C#






Assignment Operator


 = += -= *= /= %= <<= >>= &= ^= |=

UsAssignment Operator







Miscillaneous Operator


sizeof(), typeof(),  *,  &,  is, as


Conditional Operator

? :
UsConditional Operator
In the below example the term A>B ? 0:1; needs to be explained 

Here it means 

          if(A>B)
          val = 0;
          else
          val=1;







Comments

Popular posts from this blog

MVC - Introduction

What is MVC? Model-View-Controller (MVC) has been an important architectural pattern in computer science for many years.It was originally named Thing-Model-View-Editor in 1979, it was later simplifi ed to Model-View-Controller.Although originally developed for desktop computing, model–view–controller has been widely adopted as an architecture for World Wide Web applications in major programming languages. Difference between MVC, MVP and MVVM MVC The input is directed at the Controller first, not the view. That input might be coming from a user interacting with a page, but it could also be from simply entering a specific url into a browser. In either case, its a Controller that is interfaced with to kick off some functionality. There is a many-to-one relationship between the Controller and the View. That’s because a single controller may select different views to be rendered based on the operation being executed. Note the one way arrow from Controller to View. This ...

C# - DataTypes

                                                                                                      <<PreviousPage      NextPage>> Data Types Value Type  Variable contains value directly  Derived implicitly from the System.ValueType Cannot derive a new type from a value type. example of value type - int, float, enum. Builtin data types apart from the above mentioned built in data types we need to discuss two special type of data types - Struct and Enum Struct -  A struct type is a value type that is typically used to encapsulate small groups of related variables In the below example we are using a struct variable Book which has one decimal value price an...

C - Overview

What is C? It is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It is designed and written by Dennis Ritchie. C is a general-purpose language which has been closely associated with the UNIX operating system for which it was developed - since the system and most of the programs that run it are written in C. The evolution of C took place in the following way: Why should we use C? 1.C language is a building block for many other currently known languages. 2.There are only 32 keywords in ANSI C and its strength lies in its built-in functions. Several standard functions are available which can be used for developing programs. 3.C language is a structured programming language. This makes user to think of a problem in terms of function modules or blocks. Collection of these modules makes a complete program. This modular structure makes program debugging, testing and maintenance easier. What is ANSI C? 1.ANSI C, ISO...