C# Programming Language Basics with Self-Test Quizzes

Lecture



1. What Is C#

C# (C Sharp) is a high-level object-oriented programming language from Microsoft, designed for the .NET platform. It was developed in 1998—2001 by a team of Microsoft engineers led by Anders Hejlsberg and Scott Wiltamuth[

It is used for:

  • web development (ASP.NET)
  • desktop applications (WPF, WinForms)
  • games (Unity)
  • services and APIs

Features of the C# language:

  • strongly typed
  • compiled
  • object-oriented
  • runs within the .NET ecosystem
  • high-level language

Unlike:

  • PHP / JS - dynamic
  • Python - interpreted
  • C++ - no managed runtime
  • Assembly - a low-level language

Comparison with other languages: JavaScript, C++, C, PHP

C# Programming Language Basics with Self-Test Quizzes

2. A Simple Program in C#:

using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}

Breakdown:

  • using System; — importing a library
  • class Program — declaring a class
  • Main() — the entry point of the program
  • Console.WriteLine() — output to the console

3. Variables in C#:

3.1 A variable is a named area of memory that stores a value of a particular type.

Basic types:

int a = 10; // integer
double b = 3.14; // fractional number
bool isTrue = true; // boolean type
string text = "Hi"; // string

Features:

  • C# is a strongly typed language
  • the type is specified explicitly (or via var)
var x = 5; // the compiler determines the type itself (int)

3.2 In C# there are two main kinds of storage (Stack vs Heap):

Stack

  • Fast memory
  • Stores:
    • Value types (int, double, struct)
    • References to objects
  • Lives within the scope of a method
int a = 5; // stored on the stack

Heap

  • Slower, but more flexible
  • Stores:
    • Objects (class)
  • Managed by the garbage collector
var obj = new MyClass(); // object on the heap

Important

  • Value types - are copied
  • Reference types - a reference is passed
int a = 5;
int b = a; // copy
MyClass o1 = new MyClass();
MyClass o2 = o1; // reference

3.3 Passing Parameters: value vs reference

By value (default)

void Foo(int x)
{
x = 100;
}

Changes do not affect the original

3.4. Keywords: ref, out, in

ref — passing by reference (read + write)

void Update(ref int x)
{
x = 50;
}
int a = 10;
Update(ref a); // a = 50

Requirements:

  • The variable must be initialized

out — output parameter

void GetValue(out int x)
{
x = 42;
}
int result;
GetValue(out result);

Features:

  • It does not need to be initialized before the call
  • It must be assigned inside the method

in — read-only (read-only reference)

void Print(in int x)
{
Console.WriteLine(x);
}
Features:
  • Passed by reference
  • Cannot be modified

Comparison

Modifier Read Write Requires initialization
value yes yes yes
ref yes yes yes
out no yes no
in yes no yes

3.5. Features of Value and Reference Types

struct (value type)

struct Point
{
public int X;
}
  • Stored on the stack
  • Copied in full

class (reference type)

class User
{
public string Name;
}
  • The object is on the heap
  • A reference is passed

3.6. Garbage Collector (GC)

In .NET there is an automatic garbage collector.

What the GC does:

  • Frees memory
  • Removes unused objects

Example

var obj = new MyClass();
obj = null; // the object is no longer used

The GC will remove it from memory later (not immediately). Memory is not freed when the method exits, nor with obj = null, immediately after the reference is removed

but rather

  • When the heap (Heap) does not have enough space for a new object
  • When the allocation threshold is reached
  • When system resources run low (little RAM, the system is under load)
  • On an explicit call (rarely needed) GC.Collect();
  • On a transition between generations (Generations)
  • In the background (Background GC)
  • The GC works by generations:
Generation What it stores
Gen 0 new objects
Gen 1 survived 1 collection
Gen 2 long-lived

When Gen 0 is full - a fast GC
When Gen 2 - a heavy full collection

Features:

  • The moment of deletion cannot be predicted exactly
  • Works by generations:
    • Gen 0 (young)
    • Gen 1
    • Gen 2 (long-lived)

3.7. C# Programming Language Basics with Self-Test Quizzes — Working with Memory Without Allocations

C# Programming Language Basics with Self-Test Quizzes— is a modern tool for working with memory without copying.

Example

C# Programming Language Basics with Self-Test Quizzes numbers = stackalloc int[3] {1, 2, 3};

Features:

  • Works with:
    • arrays
    • the stack
    • unmanaged memory
  • Does NOT allocate memory on the heap
  • Very fast

Limitations:

  • Only inside a method
  • Cannot be stored in class fields

ReadOnlySpan

ReadOnlySpan<char> text = "Hello";

3.8. Stackalloc (related to Span)

Span<int> data = stackalloc int[10];
  • Allocates memory on the stack
  • Very fast
  • No GC

3.9 The special discard variable.

_ is a discard. It means:

"Evaluate the expression on the right, but I don't need the result."

public void OnButtonClick()
{
_ = ShareInternal();
}
private async Task ShareInternal()
{
  try
  {
     await Share();
  }
  catch (Exception ex)
  {
     Debug.LogException(ex);
   }
}

That is,

_ = ShareInternal();

does not mean

Task _ = ShareInternal();

The compiler treats this specifically as a discard.

But many code analyzers and the compiler may issue a warning:

CS4014: Because this call is not awaited, execution of the current method continues before the call is completed...

That is, they remind you:

"You called a method that returns a Task, but you don't await it. Is that definitely not a mistake?"

Does it have a type? No. A discard has no type or value of its own.

Only the expression on the right has a type:

ShareInternal();   // Task

but the discard itself stores nothing.

Does it have a value? No.

You cannot do this:

Console.WriteLine(_); // Error

or

var x = _;

Because a discard cannot be read.

Then why is _ sometimes an ordinary variable?

Because _ is not a reserved word in C#.

You can write:

int _ = 5;
Console.WriteLine(_); // 5

Here _ is an ordinary variable.

The compiler works it out from context:

_ = ShareInternal();

if there is no variable _ → this is a discard.

But if a variable _ already exists:

Task _;
_ = ShareInternal();

then this is already an assignment to a variable, not a discard.

That is why declaring variables named _ is generally not recommended, so as not to create confusion.

This is exactly why the statement

_ = ShareInternal();

has become the generally accepted way to show: "I am starting a Task and deliberately ignoring its result."

3.9. Important Practical Takeaways

When to Use What

Situation Use
Small data value type
Large objects class
Need to modify an argument ref
Need to return several values out
Read-only without a copy in
High performance Span

3.10. Common Mistakes

Copying large structs
Incorrect use of ref/out
Memory leaks through references
Trying to store a Span in a class field

3.11. Summary

Variables in C# are not just "containers" but a mechanism for managing memory:

  • Stack vs Heap
  • Value vs Reference
  • ref / out / in
  • The GC manages memory
  • Span provides high performance
C# Programming Language Basics with Self-Test Quizzes

4. Data Types in C#:

In C#, data types can be divided into several groups.

4.1. Integers

byte //unsigned 8-bit type Range: 0 … 255
short //   signed 16-bit type Range: -32,768 … 32,767
int  // int — signed 32-bit type Range: -2,147,483,648 … 2,147,483,647
long //   signed 64-bit type Range: -9,223,372,036,854,775,808 …9,223,372,036,854,775,807

Example:

int age = 25;
long population = 8000000000;

4.2. Fractional Numbers

float // less precise
double // the standard fractional type
decimal // precise, often used for money

Example:

double price = 10.5;
float price = 10.5f;
decimal money = 99.99m;
The f is required for the float type. Without it, a number with a decimal point will be the double type by default.

The m is required for decimal:

decimal salary = 1200.50m;

4.3. Boolean Type

bool isActive = true;
bool isDeleted = false;

4.4. Character and String

char letter = 'A';
string name = "Ivan";

Important:

char // one character, single quotes
string // text, double quotes

4.5. Date and Time

DateTime now = DateTime.Now;
DateTime birthday = new DateTime(2000, 5, 10);

4.6. Arrays

int[] numbers = { 1, 2, 3 };
string[] names = { "Ivan", "Olga" };

4.7. Collections

List<string> users = new List<string>();
Dictionary<string, int> ages = new Dictionary<string, int>();

4.8. Objects and Classes

class User
{
public string Name { get; set; }
public int Age { get; set; }
}
User user = new User();

4.9. Nullable Types

An ordinary int cannot be null:

int age = null; // error

But you can do this:

int? age = null;
DateTime? deletedAt = null;

4.10. object

object can store a value of any type:

object value = 123;
object text = "Hello";

But it is better to use specific types rather than object.

4.11. Arrays in C#:

int[] numbers = {1, 2, 3, 4};
Console.WriteLine(numbers ); // 1

Iteration:

foreach (var n in numbers)
{
Console.WriteLine(n);
}

4.12 Delegates and Events in C#

Delegates — yes, they are a separate type.
In C#, a delegate is a reference type that stores a reference to a method.

public delegate void MyHandler(string message);

Example:

void Print(string text)
{
Console.WriteLine(text);
}
MyHandler handler = Print;
handler("Hello");

Events are not a separate data type.
event is a special class member that is based on a delegate.

public event MyHandler SomethingHappened;

That is:

event = subscription mechanism for delegate

In short:

Construct Is it a type? What it is
delegate yes a reference type for storing a method
event no a class member, a wrapper around a delegate
Action yes a ready-made delegate with no return value
Func yes a ready-made delegate with a return value

Example of the modern variant:

public event Action<string> MessageReceived;

Here Action is a delegate, and MessageReceived is an event.

4.13. C# tuples (Tuple and ValueTuple)

In C#, tuples (Tuple and the more modern ValueTuple) let you store several values of different types in a single structure. A simple usage example:

using System;
class Program
{
    static void Main()
    {
        // Creating a tuple with two elements
        (string Name, int Age) person = ("Ivan", 30);
        Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
        // A tuple with three elements
        var point = (X: 10, Y: 20, Z: 30);
        Console.WriteLine($"Point: ({point.X}, {point.Y}, {point.Z})");
        // Returning a tuple from a method
        var result = GetMinMax(new int[] { 5, 2, 9, 1 });
        Console.WriteLine($"Min: {result.min}, Max: {result.max}");
    }
    static (int min, int max) GetMinMax(int[] numbers)
    {
        int min = int.MaxValue;
        int max = int.MinValue;
        foreach (var n in numbers)
        {
            if (n < min) min = n;
            if (n > max) max = n;
        }
        return (min, max);
    }
}

Key points:

  • (string Name, int Age) declares named tuple elements.

  • You can use var for automatic type inference.

  • Tuples are convenient for returning several values from a method without creating a separate class.

C# Programming Language Basics with Self-Test Quizzes

A short table of data types in C#

Type Purpose
int integers
long large integers
double floating-point numbers
decimal money, exact calculations
bool true / false
char a single character
string text
DateTime date and time
int[] an array of numbers
List a list
Dictionary key-value pairs
object any type
int? a nullable number

Category Type Is it a type? Description
Value Types int, double, bool, char + Store the value directly
struct + User-defined value types
enum + A set of constants
Nullable (int?) + A value type that can hold null
Reference Types class + The main OOP type
string + A string (a special reference type)
array + An array
object + The base type for all types
interface + A contract
List, Dictionary<,> + Collections
Delegates and events delegate + A reference type (a pointer to a method)
Action, Func + Ready-made delegates
event no A wrapper around a delegate (not a type)

4.14 Type conversion in C#: Casting, Boxing, Unboxing

Variables and classes of one type can be turned into entities of other types using casting, boxing and unboxing.

Casting in C# is simply the conversion of one type to another (explicit or implicit), whereas boxing and unboxing specifically involve moving between value types and reference types. Boxing wraps a value in an object on the heap, and unboxing extracts it back.

Main differences between type conversions

Mechanism What it does Example Notes
Casting

Converts a value of one type to another

(within compatible types).

double d = 3.14; int i = (int)d;

Can be explicit ((int))

or implicit (for example, int → double).

Does not create an object on the heap.

Boxing

Wraps a value type (for example, int)

in an object (System.Object or an interface).

int i = 5; object o = i;

An implicit process.

The CLR copies the value

from the stack to the heap and returns a reference.

Unboxing

Extracts the value from an object

back into a value type.

object o = 5; int i = (int)o;

An explicit process. Requires

the correct type,

otherwise an InvalidCastException is thrown.

Key points

  • Casting works within a single category of types (for example, int → double, Base → Derived).
  • Boxing always means a transition from a value type to a reference type.
  • Unboxing is the reverse process and always requires the type to be specified explicitly.
  • Performance: boxing/unboxing is more expensive than ordinary casting because it involves the heap and copying data.

5. Operators in C#:

int a = 5, b = 3;
Console.WriteLine(a + b); // 8
Console.WriteLine(a - b); // 2
Console.WriteLine(a * b); // 15
Console.WriteLine(a / b); // 1 (integer division!)

Logical:

if (a > b && b > 0)
{
Console.WriteLine("OK");
}

6. Conditions in C#:

int x = 10;
if (x > 5)
{
Console.WriteLine("Greater than 5");
}
else
{
Console.WriteLine("Less than or equal to 5");
}

switch:

int day = 1;
switch (day)
{
  case 1:
    Console.WriteLine("Monday");
    break;
 case 2:
    Console.WriteLine("Tuesday");
    break;
  default:
    Console.WriteLine("Another day");
    break;
}

7. Loops in C#:

for:

for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}

while:

int i = 0;
while (i < 5)
{
  Console.WriteLine(i);
  i++;
}
In C#, the foreach loop is used when you need to go through all the elements of a collection: an array, a list, a dictionary, and so on.

Inside a foreach loop you usually cannot change the loop variable itself:

int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}

The same thing using var. var means that C# determines the variable's type on its own. In this case number will be int.

foreach (var number in numbers)
{
Console.WriteLine(number);
}

An example of foreach with List

List<string> users = new List<string>
{
"Ivan",
"Olga",
"Maria"
};
foreach (var user in users)
{
Console.WriteLine(user);
}

An example of foreach with Dictionary in C#:

Dictionary<string, int> ages = new Dictionary<string, int>
{
{ "Ivan", 25 },
{ "Olga", 30 },
{ "Maria", 22 }
};
foreach (var item in ages)
{
Console.WriteLine(item.Key + " = " + item.Value);
}

Output:

Ivan = 25
Olga = 30
Maria = 22

8. Methods (functions) in C#:

static int Sum(int a, int b)
{
return a + b;
}

Usage:

int result = Sum(2, 3);

9. Classes and OOP in C#:

A class example:

class Person
{
  public string Name;
  public int Age;
  public void SayHello()
  {
  Console.WriteLine("Hello, I am " + Name);
  }
}

Usage:

Person p = new Person(); // creation, instantiation
p.Name = "Ivan"; // assignment
p.Age = 30; // assignment
  Console.WriteLine("Hello, I am " + p.Name );// reading a value
p.SayHello();// calling a method

9.1. Encapsulation (properties)

 
public class Person
{
    // A private field is hidden from external code
    private string _name;
    // A public property with validation logic
    public string Name
    {
        get { return _name; }
        set
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("The name cannot be empty");
            _name = value;
        }
    }
    // The constructor sets the initial state
    public Person(string name)
    {
        Name = name; // We use the property, not the field directly
    }
    // An additional method encapsulates behavior
    public void Rename(string newName)
    {
        Name = newName;
    }
}

To demonstrate the effect of encapsulation, you need to hide the internal fields and control access through methods or properties.

9.2. Inheritance

class Animal
{
  public void Speak()
  {
   Console.WriteLine("Sound");
  }
}
class Dog : Animal
{
 name  = "Dog";
}
The Dog class extends the parent class Animal (it adds to or overrides the parent's methods or properties)

9.3. Polymorphism

class Animal
{
  public virtual void Speak()
  {
  Console.WriteLine("Sound");
  }
}
class Dog : Animal
{
  public override void Speak()
  {
  Console.WriteLine("Woof");
  }
}
overriding parent methods, where permitted

10. Exceptions (errors) and handling them in C#:

try
{
int x = int.Parse("abc");
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}

In C#, memory management is usually handled by the Garbage Collector (GC). However, if your code uses system resources (files, database connections, network sockets), the GC may not release them promptly enough.

To solve this problem, there is the IDisposable interface with a single method, Dispose(). The using statement is "syntactic sugar" that guarantees this method will be called automatically.

Why is this needed?

Imagine you have opened a file for writing. If the program "crashes" because of an error before you close the file, it may remain locked by the operating system.

using creates a try-finally block in which the call to Dispose() happens in the finally section. This guarantees that the resource is released even if an exception occurs.

Ways to use it

The classic using block (before C# 8.0)

This form clearly limits the variable's scope to the curly braces. As soon as execution leaves the braces, the resource is released.

using (var stream = new FileStream("test.txt", FileMode.Create))
{
    // Work with the file
} // stream.Dispose() is called automatically here

using declaration (starting with C# 8.0)

A more modern and concise approach. You do not need curly braces: the resource will be released at the end of the current method or code block.

void WriteData()
{
    using var stream = new FileStream("test.txt", FileMode.Create);
    // Work with the file
    
    // Dispose() will be called automatically when the method exits
}

How it works "under the hood"

The compiler expands the using construct into a try-finally construct.

An example of the compiler's logic:

{
    var resource = new MyResource();
    try
    {
        // Using the resource
    }
    finally
    {
        if (resource != null)
            ((IDisposable)resource).Dispose();
    }
}

When should you use it?

The rule is simple: if a class implements IDisposable, you almost always need to wrap it in using.

Common examples:

  • Working with files: StreamReader, StreamWriter, FileStream.

  • Networking: HttpClient (although there are nuances with reusing it), TcpClient.

  • Databases: SqlConnection, DbContext in Entity Framework.

  • Graphics: Bitmap, Pen, Brush.

11. Generics in C#

Generics in C# are a way to write universal code that works with different data types without losing type safety.

A simple definition

A generic is a type template in which a specific type is substituted later.

An example without generics (bad)

class Box
{
public object Value;
}

Problems:

  • there is no type checking
  • you have to cast
Box box = new Box();
box.Value = 123;
int x = (int)box.Value; // cast

With generics (correct)

 C# Programming Language Basics with Self-Test Quizzes

Usage:

C# Programming Language Basics with Self-Test Quizzes

No casts, everything is strictly typed

Where generics are used

11.1. Generics in collections

C# Programming Language Basics with Self-Test Quizzes
  • C# Programming Language Basics with Self-Test Quizzes is a list that can store only int. Trying to add a string causes a compilation error.

  • C# Programming Language Basics with Self-Test Quizzes is a dictionary in which the keys are strictly string and the values are strictly int. This guarantees type safety.

List is a generic

11.2. Generics in methods (of a class)

C# Programming Language Basics with Self-Test Quizzes

Usage:

int x = GetFirst(new int[] {1, 2, 3});
string s = GetFirst(new string[] {"a", "b"});

11.3. Generics with constraints on a class type

You can specify which types are allowed:

C# Programming Language Basics with Self-Test Quizzes

Examples:

where T : class // reference types only
where T : struct // value types only
where T : new() // must have a constructor

The strength of generics

Without generics With generics
object a specific type
casts no casts
runtime errors errors at compile time
slower faster

The key idea

Generics = a type is passed as a parameter

C# Programming Language Basics with Self-Test Quizzes

Here T means "any type"

Summary

  • Generics make code:
    • universal
    • safe
    • fast
  • they are used everywhere (collections, LINQ, APIs)

Comparison with Java generics

Generics in the CLR are implemented not as "compiler sugar" but as a fundamental part of the virtual machine itself: they exist at the level of IL and metadata, and the JIT compiler performs real specialized expansion for value types and code sharing for reference types. This makes generics in .NET much more powerful and efficient than, for example, in Java, where they are implemented through type erasure.

Characteristic CLR (.NET) generics Java generics
Implementation Built into IL and the CLR Type erasure
Performance Almost like hand-written code Sometimes requires boxing
Reflection Full type information Limited (the type is erased)
Value types Specialization without boxing Not supported (objects only)
Code sharing For reference types None (every generic is erased to Object)

12. Extension methods in C#

An extension method in C# is a static method that lets you "add" new methods to an existing type (class, struct, interface) without changing its source code and without creating a derived class. Such methods look like ordinary instance methods, but they are implemented in a separate static class.

Main characteristics

  • They are defined in a static class.
  • The method must be static.
  • The first parameter of the method has the this keyword and specifies the type being extended.
  • They are called like ordinary instance methods: obj.MethodName().
  • They work even for sealed classes (for example, string) that cannot be inherited.

Example 1: Extending the built-in string type in C#

using System;
public static class StringExtensions
{
    public static int WordCount(this string str)
    {
        return str.Split(new char[] { ' ', '.', '?' },
            StringSplitOptions.RemoveEmptyEntries).Length;
    }
}
class Program
{
    static void Main()
    {
        string sentence = "C# extension methods are powerful";
        Console.WriteLine("Word Count: " + sentence.WordCount());
    }
}

Result: Word Count: 5 The WordCount() method looks like a built-in string method.

Example 2: Extending a user-defined class in C#

class Student
{
    public string Name { get; set; }
    public int Marks { get; set; }
}
public static class StudentExtensions
{
    public static void PrintResult(this Student s)
    {
        Console.WriteLine(s.Marks >= 40
            ? $"{s.Name} Passed"
            : $"{s.Name} Failed");
    }
}
class Program
{
    static void Main()
    {
        var s1 = new Student { Name = "Alex", Marks = 75 };
        var s2 = new Student { Name = "Maria", Marks = 35 };
        s1.PrintResult(); // Alex Passed
        s2.PrintResult(); // Maria Failed
    }
}

Example 3: Extending an interface in C#

public interface ILogger
{
    void Log(string message);
}
public static class LoggerExtensions
{
    public static void LogError(this ILogger logger, string error)
    {
        logger.Log("ERROR: " + error);
    }
}

Now any class that implements ILogger automatically gets the LogError() method.

Where extensions are used

  • LINQ: the standard methods (OrderBy, GroupBy, Average) are implemented as extension methods for IEnumerable.
  • Convenient for adding utility methods to types that cannot be changed (for example, string, DateTime).
  • Improves code readability and reuse.

13. Working with .NET in C#:

C# runs on the .NET platform, which:

  • manages memory (Garbage Collector)
  • provides libraries
  • lets you write cross-platform applications

Summary

C# is:

  • strong typing
  • a powerful OOP model
  • tight integration with .NET
  • versatility (web, desktop, game dev)

Self-test quizzes

1. Which data type is used to store integers in C#?

  • string
  • double
  • int *
  • bool

Hint: C# has different numeric types: integer types (without a fractional part) and floating-point types. They differ in size and range of values. Think about the basic numeric types: there are separate ones for whole and for fractional values. You need the one without a fractional part.

2. What denotes the entry point of a program?

  • Start()
  • Run()
  • Main() *
  • Init()

Hint:Every console application in C# must have a method that serves as the start of execution. It has a fixed name and signature. This is a special method where program execution begins. It is always called first.

3. Which data type is used to store text?

  • char
  • string *
  • int
  • bool

Hint: Text in C# is a sequence of characters. A separate reference data type is used for it. Think: a single character is one type, and a string of characters is another.

4. How do you declare a variable with automatic type inference?

  • auto x = 5;
  • var x = 5; *
  • dynamic x = 5;
  • let x = 5;

Hint: C# supports type inference, in which the compiler determines the variable's type by itself at compile time. There is a keyword that lets the compiler determine the type from the value.

5. Which operator is used to compare variables?

  • =
  • == *
  • var
  • ===

Hint:Programming languages have separate operators for assigning a value and for comparing values. Do not confuse it with the assignment operator: these are two different operations.

6. How do you declare an array in C#?

  • int numbers[];
  • array int numbers;
  • int[] numbers; *
  • numbers = int[];

Hint: An array is a data structure that contains elements of the same type arranged sequentially in memory. In C#, you first specify the element type, and then indicate that it is a fixed-size collection.

7. Which loop is used to iterate over a collection?

  • for
  • while
  • foreach *
  • loop

Hint:C# has several kinds of loops, but one of them is optimized specifically for iterating over the elements of collections. There is a loop designed specifically for working with collections, without indexes.

8. What does the class keyword mean?

  • Declare a classification of variables
  • Begin a classification of tuples
  • Declare a class *
  • Declare an interface

Hint: A class is a template (a description) from which objects are created. It can contain fields, methods and properties. It is the foundation of object-oriented programming.

9. Which data type takes the values true or false?

  • checkbox
  • bool *
  • boolean
  • binary

Hint:C# has a special type for storing logical values: true or false. This is the logical type used in conditions.

10. How do you declare a method with no return value?

  • null
  • empty
  • void *
  • none

Hint:Methods in C# can return a value or return nothing. A separate keyword is used for the second case.

11. Which type is used to store fractional numbers?

  • nonint
  • fraction
  • double *
  • char

Hint: Numbers with a fractional part use floating-point types, which let you store values with precision.

12. Which data type is a reference type?

  • int
  • double
  • string *
  • bool

Hint: C# has two main kinds of types: value types and reference types. Think: does the variable store the value itself or a reference to an object?

13. Which operator is used for logical AND?

  • and
  • && *
  • union
  • &

Hint:Logical operators are used to combine conditions. They work with Boolean values. The operator must return true only if both conditions are true.

14. How do you create an object of a class, and what is this process called (instantiation)?

  • create
  • new *
  • init
  • make

Hint:An object is created from a class using an operator that allocates memory and calls the constructor. A special keyword is used to create an instance.

15. What is a delegate?

  • A data type for storing methods *
  • A class
  • An interface
  • A variable

Hint: Delegates are types that can store references to methods and call them. This is related to passing methods as parameters.

16. What is an event in C#?

  • A data type
  • A method
  • A wrapper around a delegate *
  • A class

Hint:Events in C# are built on delegates and make it possible to implement the "observer" pattern. They are used to implement a mechanism of subscriptions and notifications.

17. What are generics in C#?

  • A type of loop
  • A type template *
  • A method
  • An interface

Hint:Generics let you create universal classes and methods that are not tied to a specific type. They let you write code that works with different types.

18. Which type is used for key-value pairs in C#?

  • List
  • Array
  • Dictionary *
  • Queue

Hint:C# has collections that implement associative arrays (key - value). A structure in which each key corresponds to a value.

19. What does the static keyword do in C#?

  • Makes a method private
  • Binds a member to the class *
  • Deletes an object
  • Creates an object

Hint: Static members belong to the class, not to a specific instance. It is related not to an object but to the class itself.

20. Which type can store any type of data?

  • int
  • string
  • object *
  • bool

Hint:C# has a universal type from which all other types inherit. It is the base type for all objects.

21. How do you declare a nullable type?

  • int!
  • int? *
  • ?int
  • int null

Hint:Nullable types allow value types to take the value null. There is a way to indicate that a value may be absent.

22. What does try-catch do?

  • Starts a loop
  • Handles errors *
  • Creates an object
  • Compares values

Hint:The exception-handling construct lets you catch errors while the program is running. It is used when working with potentially dangerous code.

23. Which data type stores a single character?

  • string
  • char *
  • symbol
  • byte

Hint:C# has a separate type for storing a single Unicode character. Do not confuse it with a string, which can contain many characters.

24. Which type is used for date and time?

  • Time
  • Date
  • DateTime *
  • Calendar

Hint:C# provides a special type for storing a date and time, with the ability to perform operations on them. It is the standard built-in type for working with dates.

25. Which method prints text to the console?

  • print()
  • echo()
  • Console.WriteLine() *
  • log()

Hint: .NET has a class for console input/output that contains methods for displaying text. It belongs to the standard class for working with the console.

26. What does `where T : class` mean in a generic constraint?

  • T must be an enum
  • T must be a reference type *
  • T must be a number
  • T must be a static class

Hint: In generics, you can constrain types. The class constraint means the parameter must support the behavior of reference types: passing by reference and the ability to be null. It is not a value, but a reference.

27. What does the `??` operator do?

  • Checks the type of an object
  • Returns the left value if it is not null, otherwise the right one *
  • Creates a nullable variable
  • Compares two strings

Hint: This is the null-coalescing operator. It checks a value and substitutes a fallback if the value is missing (null).
"If the left side is empty, take the right."

28. Which modifier allows a method to be overridden in a derived class?

  • sealed
  • virtual *
  • static
  • readonly

Hint: For a method to be changeable (overridable) in a descendant, it must be marked in the base class.
Clue: it is like "permission to change the behavior".

29. Which modifier is used when overriding a method?

  • override *
  • overload
  • rewrite
  • extends

Hint: In the derived class, a special keyword is used to replace the implementation of the base method.
Clue: "rewrite over the existing one".

30. What does `IEnumerable` do?

  • Allows an object to be enumerated with foreach *
  • Creates a dictionary
  • Starts a thread
  • Saves data to a file

Hint: This is an interface that lets you walk through a collection sequentially (for example, in a loop).
It works with foreach.

31. How does `IQueryable` differ from `IEnumerable`?

  • IQueryable is used only for arrays
  • IQueryable can build a query expression for a remote data source *
  • IQueryable does not support LINQ
  • IQueryable is always faster than IEnumerable

Hint: One works with data that is already loaded, the other can build a query (for example, SQL) and execute it later.
"Lazy execution and a remote source."

32. What does `async` on a method mean?

  • The method automatically runs in a separate process
  • The method can use await inside itself *
  • The method becomes static
  • The method blocks the thread

Hint: It lets a method use asynchronous operations and write code as if it were synchronous.
It is needed to work with await.

If a function needs to wait for something to complete with await, the function must have async so that code execution as a whole is not blocked.

33. What does `Task` return?

  • Only an error
  • An asynchronous operation with a result of type T *
  • A new thread
  • A reference to a class

Hint: It represents an asynchronous operation. It can return a result or simply a completion signal.
A wrapper around a future result.

34. What does `await` do?

  • Removes the task from memory
  • Waits for an asynchronous operation to complete without ordinary thread blocking *
  • Creates a new class
  • Terminates the program

Hint: It suspends the method until the task completes, but does not block the thread.
"Wait, but don't freeze the thread."

35. What is boxing?

  • Converting a reference type to a value type
  • Converting a value type to object or an interface *
  • Removal of an object by the garbage collector
  • Creating an array

Hint: When a value (value type) is packed into an object (object or an interface).
A value turns into a reference.

36. What is unboxing?

  • Extracting a value type from object *
  • Converting a string to a number
  • Creating a generic class
  • Copying an array

Hint: The reverse process: extracting the value from the object. Getting the "pure value" back.

37. How does `struct` differ from `class`?

  • struct is a reference type
  • struct is a value type *
  • class cannot be inherited
  • struct is always stored only on the heap

Hint: One is stored as a value (copied), the other as a reference. struct is a value type.

38. What does `readonly` mean for a field?

  • The field can be changed only in its declaration or in a constructor *
  • The field can be changed in any method
  • The field becomes nullable
  • The field is deleted after the method runs

Hint: The field can be set only once, at declaration or in a constructor. It cannot be changed after creation.

39. What does `const` mean?

  • The value is computed at run time
  • The value is known at compile time and does not change *
  • The value can be changed in a constructor
  • The value is stored only in a database

Hint: The value is fixed before the program even starts (at compile time). It never changes at all.

40. What does `yield return` do?

  • Returns all elements at once
  • Allows returning the elements of a sequence one at a time *
  • Terminates the application
  • Creates a new thread

Hint: It lets you return elements one at a time, creating a lazy sequence. "Not everything at once, but in parts."

41. What is an extension method?

  • A method that extends an existing type without changing its code *
  • A method that works only inside a constructor
  • A method that cannot be called
  • A method for deleting a class

Hint: It lets you add a method to an existing type without changing its code. As if you had "added a method to someone else's class".

42. How must an extension method be declared?

  • In a non-static class and without parameters
  • In a static class, as a static method, with `this` on the first parameter *
  • Only inside an enum
  • Only as a private method

Hint: There are strict rules: the method and the class must be special, and the first parameter indicates what is being extended. The this keyword in the parameter.

43. What does the LINQ method `Select` do?

  • Filters elements
  • Transforms each element of a sequence *
  • Sorts elements
  • Removes elements

Hint: It transforms each element into a new form. "map" in other languages.

44. What does the LINQ method `Where` do?

  • Filters elements by a condition *
  • Creates a new thread
  • Converts elements to a string
  • Terminates the program

Hint: It selects elements by a condition. A filter.

45. What does `sealed class` mean?

  • Such a class cannot be inherited from *
  • Such a class must be abstract
  • Such a class is an enum
  • Such a class can be used only once

Hint: It forbids inheriting from the class. "You can't go further." (the equivalent of final in other programming languages)

46. What are nullable reference types?

  • The ability to explicitly control null for reference types at the compiler analysis level *
  • A new database type
  • Only a shorthand for `int?`
  • A way to disable exceptions

Hint: They let the compiler track where null is possible and warn about errors. Null control at compile time.

47. What does the `?.` operator do?

  • Safely accesses a member of an object if the object is not null *
  • Creates a new nullable type
  • Compares two objects
  • Converts object to string

Hint: Safe access to an object: if it is null, there will be no error. "Check before accessing."

48. How does `abstract class` differ from `interface`?

  • abstract class can contain state and a base implementation *
  • interface always stores instance fields
  • abstract class cannot be inherited
  • interface can be created directly via new

Hint: One can store state and implementation, the other only a contract (in the classical sense). The presence of fields and code inside.

49. What does `using` do when working with `IDisposable`?

  • Automatically calls Dispose after leaving the block *
  • Creates a new namespace
  • Disables the Garbage Collector
  • Makes the object static

Hint: It automatically releases resources after the object is used. A call to Dispose() without your involvement.

50. What is a delegate in C#?

  • A type that stores a reference to a method with a specific signature *
  • Only a keyword for a loop
  • A way to create an enum
  • A method for serializing an object

51. What happens with covariance in delegates?

  • Parameters can be more derived types
  • The return type can be more derived *
  • The delegate cannot be called
  • All parameters become nullable

52. What does contravariance mean?

  • The return type is more general
  • Parameters can be more general types *
  • The delegate becomes async
  • The type cannot be changed

53. What is an expression tree (`Expression`)?

  • Compiled code
  • A representation of code as a tree of expressions *
  • Only an SQL string
  • An array type

54. How does `Func` differ from `Expression>`?

  • Func is a string
  • Expression stores the structure of an expression, not executable code *
  • Func cannot be called
  • Expression is always faster

55. What does `Span` do?

  • Creates a list
  • Represents a contiguous region of memory without allocation *
  • Saves data to a DB
  • Creates a thread

56. Why can't `Span` be stored in a class field?

  • It is too large
  • It is a stack-only type (ref struct) *
  • It does not support generics
  • It works only with int

57. What does the `ref` keyword do when passing a parameter?

  • Passes a copy
  • Passes a reference to the variable *
  • Makes the parameter nullable
  • Makes the method async

58. How does `ref` differ from `out`?

  • ref requires the variable to be initialized before it is passed *
  • out requires initialization
  • out returns a list
  • ref works only with strings

59. What is an `in` parameter?

  • Pass by value
  • Pass by read-only reference *
  • Passing an array
  • Passing a delegate

60. What does `readonly struct` do?

  • Forbids changing fields after creation *
  • Makes the struct a reference type
  • Makes the struct nullable
  • Allows inheritance

61. What does `stackalloc` do?

  • Allocates memory on the heap
  • Allocates memory on the stack *
  • Frees memory
  • Creates an object

62. What does `await using` mean?

  • An asynchronous using for IDisposable
  • An asynchronous using for IAsyncDisposable *
  • An ordinary using
  • Starting a thread

63. What does `IAsyncEnumerable` represent?

  • A synchronous collection
  • An asynchronous sequence of elements *
  • A data stream
  • A delegate

64. What does `yield break` mean?

  • Returns a value
  • Ends the iteration of a generator *
  • Interrupts a thread
  • Creates a list

65. What does the `ConfigureAwait(false)` method do?

  • Speeds up the CPU
  • Does not return execution to the original synchronization context *
  • Creates a new thread
  • Completes the task

66. What is boxing cost?

  • The cost of compilation
  • The additional memory and performance overhead of boxing a value *
  • Removal of an object
  • Creating an array

67. What does `lock` do?

  • Blocks a thread forever
  • Provides thread-safe access to a resource *
  • Creates a task
  • Deletes a variable

68. What is a deadlock?

  • A compilation error
  • Mutual blocking of threads *
  • A memory leak
  • A stack overflow

69. What does `volatile` do?

  • Makes a variable immutable
  • Guarantees the value is up to date across threads *
  • Deletes the variable
  • Makes the variable static

70. What is the GC (Garbage Collector)?

  • A thread
  • An automatic memory management system *
  • A compiler
  • An interpreter

71. What does `weak reference` mean?

  • A strong reference
  • A reference that does not prevent garbage collection *
  • A reference to a struct
  • A nullable type

72. What does `Activator.CreateInstance` do?

  • Deletes an object
  • Creates an object via reflection *
  • Compares types
  • Creates a thread

73. What is reflection?

  • Compilation
  • Obtaining information about types at run time *
  • Creating an array
  • Working with files

74. What does `nameof()` do?

  • Returns the string name of a variable/member *
  • Creates a variable
  • Deletes a method
  • Executes a method

75. What is JIT compilation?

  • Compilation at run time *
  • Compilation only at installation
  • Interpretation of code
  • Removal of IL code

76. What does the statement _ = ShareInternal(); mean if ShareInternal() returns a Task?

  • A. The ShareInternal() method will not be executed because the result is discarded.
  • B. The ShareInternal() method will run synchronously to completion, and then its result will be discarded.
  • C. The ShareInternal() method will be started, but the returned Task is intentionally ignored (fire-and-forget).*
  • D. The _ = statement automatically awaits the completion of the Task, but does not store its result.

Explanation: the _ = statement uses a discard and shows that the returned Task is deliberately ignored. The method is started, but the calling code does not wait for it (await is not performed), so this is a typical fire-and-forget pattern.

created: 2026-04-25
updated: 2026-09-29
1



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Programming Languages and Methods / Translation Theory"

Terms: Programming Languages and Methods / Translation Theory