Search This Blog

Q51-Q55

Q51. What is the difference between for loop and Foreach loop in C#? Which has better performance and why?
Q52. When to choose readonly and when to choose constant?
Q53. Good coding practices in C#?
Q54. What is cyclomatic complexity in programming?
Q55. What is Big O notation?

=======================================================================================
Q51. What is the difference between for loop and Foreach loop in C#? Which has better performance and why?

Answer:
For loop is just a iteration loop and can be used for anything while foreach loop is only designed to work on collections. 

Performance or speed wise for loop is almost similar to ForEach loop but theoretically for loop is better in performance. 
=======================================================================================
Q52. When to choose readonly and when to choose constant?

Answer:
When you want to make something static during compile time and you do not want this value get hampered anywhere in code make it Constant. 

Constants are static by default. Constant give better performance as they are defined in memory at compile time only. 

Readonly are required when you have your constants at run time only for eg. you get something related to claims during run time from the client whom initializes the class and once you have the data you do not want it to get changed in future than you can use read-only. 

Readonly has a little overhead of getting value during run time but has advantage of defining value at run time which is not possible in case of Constant. 

=======================================================================================
Q53. Good coding practices in C#?

Answer:
1. Code must have proper error handling. 
2. Proper logging
3. Good coding practices tool like Sonar must be integrated. 
4. Good naming convention must be used. 
5. Proper comments must be present. 
6. Null, empty, undefined check must be present where ever possible. 
7. While comparing string either bring both string into lowercase or into upper case.
8. Make proper usage of access modifiers. We must not make blindly everything public. 
9. Make use of Interface as it bring dependency injection in picture. 
10. When working on un managed code, we must remember to dispose un managed code. 
11. Make use of authentication filter or middleware

=======================================================================================
Q54. What is cyclomatic complexity in programming?

Answer:
software metric used to determine the complexity of a program. It is a count of the number of decisions in the source code. The higher the count, the more complex the code.
IF-else, is a great example of this. 

=======================================================================================
Q55. What is Big O  notation?

Answer:

Big O notation shows the number of operations

O(log n) Binary search
O(n) Simple search
O(n * log n) Quicksort
O(n2) Selection sort
O(n!) Traveling salesperson

No comments:

Post a Comment