Blog for Junior Developers C#/.NET

Friday, June 21, 2024

In previous entries, I have already presented you with 40 popular questions that you may encounter during a job interview as a junior C# programmer. It's high time to move on to the next ones. Today I will again present you 10 more questions with answers.

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

41) What is the difference between IList and List? What are their uses?


First of all, IList is an interface and List is a class. List is a concrete implementation of IList. Typically, if we make our class available via a library that will be used by others, it is better to make it available via an interface, rather than a specific implementation. This solution is more expandable and universal.

This will allow you to protect yourself against possible changes in the future. If you change the List class to another class in the future, users who implement your library will have to make many changes, and if you use IList, such a change will not be necessary.

private static IList<int> _listInterface;
private static List<int> _listClass;


42) What is the difference between an interface and an abstract class in C#?


The most important differences:
-We use the interface keyword in the interface declaration, and abstract class in the abstract class.
-A class can implement any number of interfaces and can only inherit from one abstract class.
-Interface members cannot be marked with access attributes (public is set by default), and access attributes can be marked in an abstract class.
-You cannot declare fields in an interface, but you can in an abstract class.
-An interface cannot have a constructor, and an abstract class can implement a default constructor.

As for use cases, it's worth using abstract classes and inheritance if you can make the statement "A to B". However, use interfaces if you can formulate the statement "A is able to do B".

public abstract class Shape
{
}

public interface IPaintable
{
}


43) What is a copy constructor?


A copy constructor is a constructor that takes instances of its class as a parameter and copies the values ​​of the object's fields. This means that the reference is not copied, only the field values.

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Student(Student student)
    {
        Id = student.Id;
        Name = student.Name;
    }
}


44) What are extended methods in C#?


Thanks to extension methods, we can extend the functionality of existing types, e.g. those whose definitions we do not have access to. A good example of such methods are LINQ methods, where we use extension methods, including Sum(), Min(), Max(), Take(), etc., these are all methods that extend IEnumerable.

The extension method must be static and declared in a static class and must contain the this keyword before the type that we want to extend before the first parameter.

public static class StringExtensions
{
     public static void Display(this string value)
     {
         Console.WriteLine(value);
     }
}
public class Program
{   
    static void Main(string[] args)
    {
         var title = "C# programming";
         title.Display();//C# programming
    }
}


45) What is the difference between throw and throw exception in a catch block?


In the throw notation, we do not lose the information contained in the stack trace (call stack). Also, if we use throw exception, we will not get full information about all previously thrown exceptions in this stack. This means we will not have full information about the errors that occurred in our application, which will make diagnosing it more difficult.

To sum up, the better practice is the first scenario, i.e. just throw.

try
{
}
catch (Exception exception)
{
    throw;
}

try
{
}
catch (Exception exception)
{
    throw exception;
}


46) What is the difference between string and StringBuilder?


The main difference is that the string is called an immutable, or unchangeable, object, while StringBuilder is mutable, or variable. That is, StringBuilder makes changes to an existing object rather than creating a new object each time. Due to the fact that a string is immutable, it is a less efficient class because it creates a completely new object each time.

If you need to create text that will not change, you can by all means use a string, otherwise it is better to use StringBuilder.

var simpleString = string.Empty;

for (int i = 0; i < 1000; i++)
{
    simpleString += i.ToString() + " ";
}

Here, 1000 objects will be created, of which 999 will not be used.

var simpleStringBuilder = new StringBuilder();

for (int i = 0; i < 1000; i++)
{
    simpleStringBuilder.Append($"{i} ");
}


47) What is the difference between the "is" and "as" keywords in C#?


The is operator is used to check the compliance of an object with a given type and returns a boolean.

In turn, the as operator is used to cast an object to a given type.

if (student is Person)
    (student as Person).DisplayName();


48) What is the difference between a value type and a reference type?


Value type are elements that extend System.ValueType and are value types such as int, long, double, decimal, bool. They are stored on the stack. When assigning, only the value itself is changed.

Reference type are elements inheriting from the System.Object and System.String classes, i.e. mainly classes, arrays, lists, strings. A memory reference is placed on the stack, but the area of ​​memory to which the reference points is on the heap. They are removed from memory using the Garbage Collector. When assigned, what actually happens is the copied reference, not the value itself, as is the case with value types. After such an operation, both objects will point to the same place in memory.


49) What is the IDisposable interface?


The primary use of the IDisposable interface is to release unmanaged resources. Garbage Collector automatically frees managed memory when objects are no longer in use. However, it is impossible to predict when this will happen.

Additionally, Garbage Collector has no knowledge of unmanaged resources such as files or streams. A class that implements the IDisposable interface must implement a Dispose method in which it can release these resources. If we want to pass an object to the using block, it must implement this interface.

public class Student : IDisposable
{
    public void Dispose()
    {
        //releasing resources, files, streams, etc
    }
}


50) What are synchronous and asynchronous operations?


Asynchronous programming reduces workload. We can perform given tasks without blocking the main thread.

In synchronous operations, tasks are performed one at a time and only after their completion are the next ones unlocked. This means you always have to wait for the task to be completed before moving on to the next one.

When it comes to asynchronous operations, here you can move on to another task before finishing the previous one. Thanks to this, in asynchronous programming we can handle many requests at the same time, and thus perform more tasks in less time.

In C#, to write an asynchronous method, you need to use the async keyword in the signature, return a Task, and mark the executed task that is performed inside the method and requires more time to execute with the word await.

public async Task<string> GetResult()
{
    return await Result();
}


TERMINATION


That's all the questions in this article. We will analyze the next ones in the next material. 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 4/10)
Next article - 100 Interview Questions (and Answers!) for Junior Developers C#/.NET (Part 6/10)
Dodaj komentarz

Search engine

© Copyright 2024 CodeWithKazik.com. All rights reserved. Privacy policy.
Design by Code With Kazik