Programming every day usage differences PART 1

Difference between methods and functions?

When talking about object oriented programming (OOP) then we are using methods. Methods are defined inside of classes. Every “method” defined outside of a class is called a function. Java is a typical example where functions do not exist since Java is OOP. In C++ we both have functions and methods.

Basically in OOP languages we talk about methods in languages which are not OOP we talk about functions.

Difference between parameters and arguments?

When defining a function/method we are talking about parameters if we want to use them. Once we pass data to these parameters (on function/method call) we talk about giving arguments to that function. It’s important to mention that parameters and later the arguments that we pass to the method are value types by default. In C# If we want the parameter, the method parameter to become a reference type then we have to write the keyword “ref” inside the method parameter and also when calling the method and giving arguments to it we have to specify the ref keyword.

Difference between variable post-increment and pre-increment?

Code sample:

int y;

int x = 2;

y = x++;// or y=x=(x+1) , y stores x <strong>immediately</strong> and THEN x is being incremented y=2 here

y=x;// if you would go now and write this line you would see that y=3;

// with pre-increment there is nothing much being explained

// first the value is incremented and thus it is stored in the variable y

int y;

y=2;

y=++x;// y=3

I think that everything is pretty much clear with this example especially with the little equations and the comments afterwards (I wouldn’t want to repeat myself).

Difference between foreach and for loop?

In terms of performance I must quote the following (taken from stackoverflow):

Patrick Smacchia blogged about this last month, with the following conclusions:

  • for loops on List are a bit more than 2 times cheaper than foreach loops on List.
  • Looping on array is around 2 times cheaper than looping on List.
  • As a consequence, looping on array using for is 5 times cheaper than looping on List using foreach (which I believe, is what we all do).

Concerning the usage of this loops. When iterating through objects it is better to use foreach loop (it’s nicer). When working with primitive value types then use for loop. In functional programming the for loop is “everything”. Other than that it entirely depends on you. Foreach loops are massively used when iterating through objects.

Difference between explicit and implicit cast?

Example:

int i = 2;

float a = i;        // Implicit

float b = (float)i; // Explicit

I believe that the “image” above is more than enough to describe the explicit and implicit in programming. If not, then you can add the following: Implicit automatically parses the other data type and stores in in the other one if possible. An explicit casts demands additional “functions” to parse the data to the other one if possible.

Difference between structures and classes?

The main difference between classes and structures are when assigning parameters dynamically to structures (example: modifying the struct variables through a method) we can see that the value does not change! From [2] we can conclude that structures are value types. Whereas if we do the same thing with a class (trying to change a class variable through a method) we will see that the class variable will change! From [2] we can conclude that classes are reference types. Apart from that structs can’t be inherited by a another class and are used to store small amount of data inside them in particular.

Difference between private, public and protected?

Since this is the far best quick explanation online, I decided to copy and paste it and not to evaluate further.

public

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private

The type or member can only be accessed by code in the same class or struct.

protected

The type or member can only be accessed by code in the same class or struct, or in a derived class.