Programming every day usage differences PART 2

Difference between name-spaces and classes?

Namespaces represent scopes {} where variables, classes, structs and other types reside. Classes are just types which also have to reside inside of a scope and this scope is called namespace. If you want to look at it as a hierarchy, namespaces go first here after that all the other elements/types go, in this case classes.

Other thing which should be mentioned are:

– You can reopen namespaces unlike classes

– You can create an instance of classes unlike with namespaces

– Namespaces can be unnamed unlike with classes which must have a name.

Difference between classes and objects?

Classes and objects are the “same thing”. Basically this is a parameter-argument problem. Classes can be considered as when writing a function. Once you call that class (instantiate) the class becomes an Object.

Difference between arrays and lists?

Both of them can store multiple values and that’s their purpose, to store multiple values. Arrays, however have a fixed storage, so once you declare them you cannot add more cells to it whereas lists are re-sizable.  The other important thing is that arrays are more efficient since they are primitive types and do not need a wrapper around them. List’s represents interfaces (not primitive types) and interfaces have therefore multiple implementations.

Difference between inheritance and interfaces?

Inheritance assumes having an existing class (let’s say a base class, general class like human) which will then be adopted by the newly created class which is related to the base (existing) class and therefore the new class as now access to all properties and methods from the inherited class. It is easier to build the new class or just extend it so it serves to its purpose.

Interface can be seen as blocks or scopes which contain unimplemented variables and methods. A class can inherit multiple interfaces whereas only one class can be inherited. There is no method implementation inside an interface. This is done inside the class once the interface is inherited by it.

Note: The reason why you can’t inherit multiple classes is because you could get a collision between two elements, for example methods. Let’s assume that you can inherit two classes and let the first be “Automobile” class and the second “Robot” class. Suppose both methods have a same method called Run(); When you try to call the method Run() which one should be called? Robots class method or Automobile class method. Because of such problem (which was quite expressed/conspicuous in c++ language) code engineers of languages C# and Java ect. decided not to allow multiple inheritance.

Difference between switch statement, if statements and ternary operators?

There isn’t any better performance in any of the above. Use whatever you like. However it is advised to use switch statement if there are more than 6 if’s. Ternary operators just give a concise syntax and it looks better.

Difference between multidimensional array and jagged array?

Multidimensional arrays are fixed arrays. Once you declare it you are done. Jagged arrays are arrays which contain arrays. This can be also said for the multidimensional array but every row has a same size as does column. Jagged arrays row and column do not have to have same sizes.

Jagged arrays are faster than multidimensional arrays.