C# Flashcards

Category sponsor

C# is a versatile, object-oriented programming language created by Anders Hejlsberg at Microsoft. It is a key language of the .NET platform, designed for developing a wide range of applications, from desktop to web and mobile. C# is characterized by strong typing and a rich set of features, enabling efficient development of both simple scripts and complex enterprise systems. This language offers advanced mechanisms such as LINQ, asynchronous programming, and garbage collection, providing developers with tools to write safe, efficient, and maintainable code. C# also supports integration with various Microsoft technologies and continuous evolution, maintaining syntactic consistency and enabling the development of modern, scalable applications across different platforms.

Our flashcard app includes carefully selected C# interview questions with comprehensive answers that will effectively prepare you for any interview requiring C# knowledge. IT Flashcards is not just a tool for job seekers - it's a great way to reinforce and test your knowledge, regardless of your current career plans. Regular use of the app will help you stay up-to-date with the latest C# trends and keep your skills at a high level.

Sample C# flashcards from our app

Download our app from the App Store or Google Play to get more free flashcards or subscribe for access to all flashcards.

What is a namespace in C# and what is it used for?

A namespace in C# is a way of grouping related classes, structs, interfaces, as well as other namespaces. Thanks to namespaces, it is possible to avoid name conflicts between classes that might have the same name but are part of different namespaces.

Namespaces are declared using the keyword "namespace" and create a new scope where classes, structures, and interfaces reside:
namespace MyNamespace
{
    class MyClass
    {
    }
}

To refer to a class within a specific namespace, you have to use the full name of the class, that is the name of the namespace, a dot, and the name of the class:
MyNamespace.MyClass myObject = new MyNamespace.MyClass();

To shorten the notation and facilitate the use of classes, structures, or interfaces from a particular namespace, C# provides the "using" keyword, which allows importing namespaces into a given file:
using MyNamespace;

MyClass myObject = new MyClass();

In the above example, the use of the "using" keyword eliminated the need to use the full name of the `MyClass`. The namespace was imported, which allowed for the direct use of the `MyClass`.

How to define a variable in C#?

C# is a strongly typed programming language, which means that each variable must have a specified type. Here is how to define a variable in C#.

The first thing you need to do is to declare the variable's type. You can do this by writing the name of the type, followed by the variable's name. For example:
int numberOfApples;

In this case, `numberOfApples` is a variable that can store integer values (`int`). This variable is not yet initialized, meaning it does not yet have an assigned value.

You can also initialize a variable when declaring it, as shown below:
int numberOfApples = 5;

In this case, the `numberOfApples` variable is initialized with the value 5.

C# also introduced a `var` keyword, which allows the compiler to determine the variable's type based on the value assigned during initialization. You can initialize a variable in this way:
var numberOfApples = 5; // The compiler will determine that numberOfApples is of type int

However, remember that a variable declared using `var` must be initialized at the time of declaration.

Explain the difference between value types and reference types in C#

Value types and reference types in C# are two fundamental types that we can operate in this programming language.

Value types are those that store the actual value directly. This means that when you assign a value to a variable of this type, you create a physical copy of that value. Changing one copy does not affect the other. Value types are stored on the stack. Examples of value types include basic types (independent of structures) such as int, float, bool, structures, enumeration types.
int val1 = 10;
int val2 = val1;
val1 = 20;

// Output: 10, because changing the value of val1 does not affect val2.
Console.WriteLine(val2);

Reference types, on the other hand, store a reference to where the actual value is stored, not the value itself. Two variables can refer to the same object, so changing the value of one variable affects the other. Reference types are stored on the heap. Examples include classes, delegates, interfaces.
StringBuilder sb1 = new StringBuilder("Hello");
StringBuilder sb2 = sb1;
sb1.Append(" World");

// Output: "Hello World", because both variables refer to the same object.
Console.WriteLine(sb2);

What is Nullable in C# and how to use it

Nullable is a type in C#, which allows the application of null values for value types. Usually, value types, such as int, float, bool, struct, enum, etc., cannot be null. But thanks to Nullable types, we can assign null to these data types.

To create a Nullable variable, we can use the '?' operator after the value type. For example:
int? i = null;

We can also use the generic Nullable structure:
Nullable<int> i = null;

System.Nullable provides several important properties that aid in working with Nullable types:

- `HasValue`: The property returns true if the variable contains a value, otherwise false.
- `Value`: The property returns the value of the variable if it contains any value.
int? myNumber = null;
Console.WriteLine(myNumber.HasValue); // false
myNumber = 10;
Console.WriteLine(myNumber.HasValue); // true
Console.WriteLine(myNumber.Value); // 10

However, remember that attempting to access the Value property when the variable has no assigned value results in an `InvalidOperationException`. Therefore, always use `HasValue` before you access the value.

Download IT Flashcards App Now

Expand your C# knowledge with our flashcards.
From basic programming principles to mastering advanced technologies, IT Flashcards is your passport to IT excellence.
Download now and unlock your potential in today's competitive tech landscape.