Blog for Junior Developers C#/.NET

Sunday, June 23, 2024

I hope that you have already analyzed and learned the previous questions and their answers, because in today's article we will move on to the next ones. I will present to you again 10 more questions that you may often hear in a job interview when you are applying for a .NET developer position.

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

61) What is the use of the dynamic keyword?


This is a type that can be changed while the program is running. Static types such as int, double, bool are compiled by the CLR (Common Language Runtime), while dynamic types are bypassed by the compiler and captured by the DLR (Dynamic Lanuage Runtime) and handled while the program is running. If a dynamic type error occurs, it will only be detected while the application is running.

The dynamic type is often used when using external data sources that use dynamic types, e.g. XML.

dynamic something;
something = 1;
Console.WriteLine(something.GetType()); //Int32
something = "2";
Console.WriteLine(something.GetType()); //String
something++; //error while the program is running


62) What are anonymous types in C#?


Thanks to anonymous types, we can conveniently create a new object without having to explicitly define the type. In this case, the type name is generated by the compiler and is not available from the source code.

To create an anonymous type, use the new keyword along with the object initializer. Anonymous types are often combined with LINQ queries.

var anonimType = new { Id = 1, Name = "Name" };
var students = new List<Student>();
var studentsFullName = students
    .Select(x => new { Name = $"{x.FirstName} {x.LastName}" });


63) What is the difference between the equality operator (==) and the Equals() method in C#?


Both methods are used to compare 2 objects.

The equality operator compares the references of both objects. If they are the same, it will return true, otherwise false. If we compare value types, the equality operator will return true if both values ​​are identical.

The Equals() method is a virtual method whose comparison logic we can define ourselves. By default, the method is used to compare object values. If both values ​​are identical, it will return true.

object object1 = 1;
object object2 = object1;

if (object1 == object2) { Console.WriteLine("=="); }

if (object1.Equals(object2)) { Console.WriteLine("Equals"); }

//result:
//==
//Equals

object object1 = 1;
object object2 = 1;

if (object1 == object2) { Console.WriteLine("=="); }

if (object1.Equals(object2)) { Console.WriteLine("Equals"); }

//result:
//Equals


64) What is the use of the @ sign in C#?


The @ sign has 2 uses in C#.

First of all, we can precede the name of the variable we want to create with this character, if this name is also a keyword in C# (e.g. @params, @this).

The second use is to place this sign before the string value in order to eliminate the need to use the so-called escape characters. This ensures that the entire value between the two quotation marks is assigned to the string.

var text1 = "C:\folder\test.txt";
Console.WriteLine(text1); //C:♀older        est.txt

var text2 = @"C:\folder\test.txt";
Console.WriteLine(text2); //C:\folder\test.txt

var @params = 123;


65) What is the difference between IEnumerable and IQueryable?


IEnumerable comes from the System.Collections namespace and IQueryable comes from System.Linq. IQueryable implements the IEnumerable interface.

The basic difference between them is that IQueryable supports query building and filters the entire query on the server side. This means we can add various filters to the collection before calling them all at once in one query on the database. In IEnumerable, however, the entire query created the first time will be executed on the server side, and all subsequent filters will be performed in memory on the entire downloaded collection.

public interface IEnumerable
public interface IQueryable : IEnumerable


66) What is the difference between Dispose() and Finalize() in C#?


The Dispose() method is a regular method that a user can call to release resources. This is a method that must be implemented if we use the IDisposable interface.

However, the Finalize() method is called by the Garbage Collector when the object is no longer in use.


67) What is the difference between Lazy Loading and Eager Loading?


If we execute a query using the Lazy Loading mechanism, all records are downloaded first, without related records. If we want to refer to these related records, only then are they loaded. A new query is performed here, only for these related records. That is, Lazy Loading loads related data only when it is needed.

Its main advantage is that we can first load all the required data, making this first load faster, and only then query for related objects. This mechanism in Entity Framework Core is blocked by default, it also has its drawbacks, because it causes many problems, such as the N+1 problem.

When it comes to Eager Loading (i.e. greedy loading), unlike Lazy Loading, it immediately creates a larger query that retrieves all information about related entities. To download objects related using Eager Loading, use the Include() method, in the parameter it passes the object that we want to include via Lambda Expression. It is worth paying attention here to download only those elements that we actually need.

var students = _context.Students
    .Include(x => x.Address);


68) What does the sealed keyword mean in C#?


If we mark a class with the sealed keyword, it means that no one can inherit from that class. This means that this class cannot be a base class for other classes.

public sealed class Person
{
}

public class Student : Person // error, you cannot inherit from this class
{
}


69) What is a version control system?


This is software that allows us to track all changes to the source code. We can use the version control system not only for programming work, but also to track changes in plain text files, e.g. Word, although this software was mainly created for programmers and tracking source code. Additionally, it also helps developers to combine all the changes made by multiple people at different times.

The most popular version control system is Git, slightly less common is SVN, and TFS. The most important advantages of Git are: support for a branched software development process, the ability to work in off-line mode, efficient work with larger projects, support for existing network protocols and good code revisions.


70) What is the difference between Git and GitHub?


Git is a version control system thanks to which, among other things, we can manage and track the history of source code. GitHub is a hosting service that allows you to manage Git repositories.


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 6/10)
Next article - 100 Interview Questions (and Answers!) for Junior Developers C#/.NET (Part 8/10)
Dodaj komentarz

Search engine

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