Blog for Junior Developers C#/.NET

Monday, June 24, 2024

We're getting close to the end of all the questions. In the previous materials, you learned 70 popular technical interview questions for the position of a junior .NET developer. In this article you will learn about 10 more.

100-interview-questions-and-answers-for-junior-developers-csharp-dotnet-part-8-10.jpg

71) What is YAGNI?


The YAGNI principle, or You Arent Gonna Need It, tells us not to create software that we don't currently need. The point is that if we do not currently need a given functionality or code, and we believe that it may be useful someday, we should not create such code for now. Just don't create something just in case, because it may turn out to be unnecessary and redundant later.

According to the YAGNI principle, we should create code only when it is really needed. The same applies when we already have some code written, but we don't need it. This may apply, for example, to situations where the customer has opted out of a given functionality. In this case, this code should be removed. Not commented out just in case, but deleted. We use a version control system and if we want to return to this code in the future, it will not be a problem.


72) What is KISS?


The KISS principle, i.e. Keep It Simple Stupid, says not to unnecessarily complicate a task or code. Just try to keep it simple. If we need to create a simple application, we do not have to create a very advanced architecture to show that we can create it. If such an architecture will not be helpful here, then there is no reason to use it here. This can only make our project unnecessarily more complicated. Complicated code has many disadvantages, such as poorer readability, greater complexity, and lack of understanding.


73) What is DRY?


The DRY rule, i.e. Don't Repeat Yourself, tells us in particular not to repeat the same code in several places. Of course, if it is possible.

If we have the same code in several places in our project, in case of any changes, we will have to make all changes in all places. In such a case, it is worth separating the common code into a separate class or using inheritance.

In addition to repetitions in the code, we should also not repeat other activities that can be automated, e.g. by writing some scripts that will help us with this.


74) What is Singleton?


Singleton is a design pattern that guarantees the existence of only one object of a given type. It should also ensure that this object can be referenced anywhere in the application.

This is quite a controversial pattern that has both many advantages and disadvantages.

As for the advantages, we are sure that there is only one instance of a given class and we have access to it from anywhere in the application.

However, when it comes to disadvantages, singleton in many cases can make writing unit tests difficult. Because it is available throughout the application code, it can also be abused and create hidden dependencies. Creating such global objects also often makes writing code more difficult.

public class Singleton
{
    private static Singleton _instance;

    private Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            if (_instance == null)
                _instance = new Singleton();

            return _instance;
        }
    }
}


75) Explain the MVC pattern


MVC, or Model-View-Controller, is a popular architectural pattern used to organize the structure of applications with graphical user interfaces.

The main idea of ​​this pattern is to divide the application into 3 parts:

A model that represents the data. It contains the application logic.

View, i.e. user interface. Part of the application responsible for data presentation.

And a controller that controls the application and communicates with the user. It accepts data from the user and triggers appropriate actions that first update the model and then refresh the views.

In C#, in particular, we use the MVC pattern when creating an application in ASP.NET or ASP.NET Core.


76) Explain the MVVM pattern


MVVM, or Model-View-ViewModel, is also an architectural pattern that is used to organize the structure of applications that have a GUI. In C#, you can use this pattern primarily in WPF and Xamarin applications.

It divides the application into 3 parts:

A model that represents the data. It contains the application logic.

View, i.e. user interface. This layer is responsible for user interaction. In WPF, the view layer is created in XAML.

ViewModel, which allows us to associate view data with the model. You could say that it combines the view with the model. Its task is to provide data to the view and exchange information with the model. It is worth mentioning here that the ViewModel from the MVVM pattern is something completely different from the ViewModel in MVC.


77) What is Dependency Injection?


Dependency injection is a pattern of removing direct dependencies in the code.

Thanks to it, we can obtain the so-called loose connections, which is what we should strive for when writing applications. We want our applications to be more flexible and possible changes in the future to be as easy as possible to implement, and what is very important, we strive to make our application testable, that is, so that unit tests can be easily written for it. To do this, first of all, we must base our solution not on specific implementations, but on interfaces. Which means we have small dependencies in the code. Interfaces are stable. That is, if we make some change in an interface, this change is also associated with a change in the implementations of this interface. However, if we make a change in a specific implementation, we usually do not need to change our interface. As a result, interfaces are more stable than implementations.

The first step to using dependency injection is to create an abstraction for classes, e.g. by creating interfaces, and then operating in the application not on specific implementations, but on these interfaces. Specific implementations will be injected into our application thanks to dependency injection. We can say in one place that we want a specific implementation to be injected in all places where we use a given interface.


78) What is the use of the yield keyword in C#?


We can use the yield keywords inside a method that is an iterator. The way it works is that if we iterate through a given collection inside the method, a given element is immediately returned using yield in each iteration. This means that the code of the method in which the yield keyword is used is executed while iterating the returned collection, and not as standard during the method call.

Additionally, when using yield, we do not have to create a new instance of the collection, which is only used to store elements to be returned from the method.

public static IEnumerable<int> Power(int number, int exponent)
{
    int result = 1;

    for (int i = 0; i < exponent; i++)
    {
        result = result * number;
        yield return result;
    }
}


79) What are delegates in C#?


Delegates are compared to function pointers in C++, but are more secure and more powerful.

Delegates hold a reference to a method or even a group of methods. They have no body, only a signature. The signature must specify the delegate parameters and return type. To create a delegate definition, we use the delegate keyword.

Delegates can be primarily helpful when we want to pass methods as parameters to other methods. There are cases where you have a method that does something and calls another method. At compile time you don't know what the second method is because this information is only available at runtime and this is where delegates will help.

We have several ready-made delegates that we can use, including Func, Action, Predicate.

public delegate int GetSum(int number1, int number2);
public delegate TResult Func<out Tresult>();
public delegate void Action();

static void Main(string[] args)
{
    Log(Run);
}

public static void Run()
{
    Console.WriteLine("Run method");
}

public static void Log(Action action)
{
    //log 
    action.Invoke();
    //log
}


80) What are events in C#?


An event is a user action that generates a notification to the application. The user's action can be any, e.g. clicking a button, scrolling, etc.

The class that reports the event is called publisher, and the class that receives the event is called subscriber. For an event to be reported, it must have at least one subscriber.

We deal a lot with events in Windows Forms applications, where we can subscribe to various events on the form, such as: mouse click, mouse hover, text change, value change, window loading, window closing, etc. Of course, we can also create our own events. They can be useful when we want to inform other objects about a given behavior.

public event Action<object, EventArgs> ButtonClicked;

protected virtual void OnButtonClicked(EventArgs args)
{
    ButtonClicked?.Invoke(this, args);
}


TERMINATION


That's all the questions in this article. We will analyze the next ones in the next article. If you like this material, be sure to join my community - free registration, where you will also have access to additional materials.

That's all for today, see you in the next article.

Author of the article:
Kazimierz Szpin

KAZIMIERZ SZPIN
Software Developer C#/.NET, Freelancer. Specializes in ASP.NET Core, ASP.NET MVC, ASP.NET Web API, Blazor, WPF and Windows Forms.
Author of the blog CodeWithKazik.com

Previous article - 100 Interview Questions (and Answers!) for Junior Developers C#/.NET (Part 7/10)
Next article - 100 Interview Questions (and Answers!) for Junior Developers C#/.NET (Part 9/10)
Dodaj komentarz
© Copyright 2024 CodeWithKazik.com. All rights reserved. Privacy policy.
Design by Code With Kazik