Blog for Junior Developers C#/.NET

Thursday, June 20, 2024

It's time to analyze the next questions you may be asked during a job interview for a junior .NET developer position. Just like in previous articles, today we will go through 10 of the most popular questions again.

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

31) Explain the use of using keyword in C#


The using keyword has uses in C# 2.

Firstly, thanks to it we can include different namespaces. As a result, we do not have to provide their full name and namespace each time we create classes from other namespaces.

We can also use a using block, which makes sure that the object that is passed will be released correctly. That is, the Dispose method will be called on it every time after the statement completes. This using block is the same as a try finally block, assuming that we call the Dispose method in the finally block. We just need to remember that the object passed to the using statement must implement the IDisposable interface.

using System;
using System.Collections.Generic;

using (var student = new Student())
{
}


32) What are preprocessor directives in C#?


Preprocessor directives begin with a # character and are intended to tell the compiler where to first process information before compilation. In C# we have several such directives, including #if, #else, #elif, #endif, #define, #undef, #region, #endregion, #line, #error, #warning.

        static void Main(string[] args)
        {
#if DEBUG
            Console.WriteLine("DEBUG");
#else
            Console.WriteLine("RELEASE");
#endif
        }


33) What is the difference between a structure and a class?


There are several differences between structure and class. First, it is worth mentioning that a class is a reference type and a structure is a value type. Structures, unlike classes, do not support inheritance, they cannot have an explicitly defined constructor without parameters, they do not have destructors, and you cannot assign values ​​for fields at the time of declaration. The structure is e.g. Point and the class is e.g. object. Structs are, so to speak, limited classes that may be a better choice when the class contains a small amount of data. In this case, it may not be cost-effective to manage it on the heap.

public struct MyStruct
{
    public int Number1;
}

public class MyClass
{
    public int Number1 = 1;
}


34) What are partial classes in C#?


We can create partial classes, just like methods, thanks to the partial keyword. That is, we can divide a given class into several parts, in separate files. At compile time, all these partial classes are combined into one class. In order to create such a class, each part must be marked with the word partial, and all parts must have the same access modifier. It is enough that one of them inherits from another type and automatically all partial classes have access to the base type. Likewise, if any part is declared as sealed or abstract, this applies to all partial classes.

partial class Student : Person
{
}

partial class Student
{
}


35) What is the difference between int and Int32?


Int is a keyword in C# that refers to the System.Int32 type. These notations in C# mean the same thing. However, it is recommended to use the int keyword. All other simple types have keyword equivalents in C#.

int number1 = 1;
Int32 number2 = 1;


36) What is an enum in C#?


Enum is the so-called an enumeration type thanks to which we can define constant names for specific values. By default, enum values ​​are mapped to ints.

public enum Season
{
    Spring,
    Summer,
    Autumn,
    Winter
}

private static Season _actualSeason;

static void Main(string[] args)
{
    _actualSeason = Season.Winter;
}


37) What is tuple in C#?


The tuple function provides syntax for grouping multiple data items. This means that it allows us to create many types at once. One of the most common use cases for a tuple is the return type of a method. Instead of defining the parameters of the out or ref method, or creating a new class that we want to return from the method, we can group the results into a return type of tuple.

public (int Id, string Name) GetResult()
{
    return (0 , "100");
}


var result = GetResult();
Console.WriteLine(result.Id);
Console.WriteLine(result.Name);

(double Sum, int Count) = (1.2, 10);


38) What is a method signature?


A method signature optionally consists of an access level, a return value, a method name, and a parameter list. Importantly, the return type of a method is not part of the signature in the case of method overloading. However, it is part of it when determining the correspondence between the delegate and the method it points to.

public int GetResult();
public void Display();


39) What is method, constructor and operator overloading in C#?


Overloading occurs when there are more than one method with the same name but different signature. For example, each method has different parameters, number of parameters, or type.

In the case of constructors, this applies to situations where there is more than one constructor in one class. In such a situation, they must differ in parameters.

Operator overloading is a situation in which we create methods that will allow us to use these operators on objects. For example, we can overload the addition operator in the class, thanks to which we can define how given objects can be added to each other.

public class Student 
{
    public Student()
    {
    }

    public Student(int age)
    {
        Age = age;
    }

    public int Age { get; set; }

    public void DisplayResult()
    {
    }

    public void DisplayResult(int age)
    {
    }

    public void DisplayResult(string name)
    {
    }

    public void DisplayResult(int age, string name)
    {
    }

    public static Student operator +(Student student1, Student student2)
    {
        return new Student { Age = student1.Age + student2.Age };
    }
}

var student1 = new Student { Age = 1 };
var student2 = new Student { Age = 4 };
Console.WriteLine((student1 + student2).Age);//5


40) What is cyclic reference?


Cyclic reference occurs when two elements are dependent on each other, which consequently makes the resource unusable. In this case, most often you just need to create the interface and remove the circular reference.


TERMINATION


So many questions in this article. I invite you to the next ones in the next materials. 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 3/10)
Next article - 100 Interview Questions (and Answers!) for Junior Developers C#/.NET (Part 5/10)
Dodaj komentarz
© Copyright 2024 CodeWithKazik.com. All rights reserved. Privacy policy.
Design by Code With Kazik