C# and .NET Cheatsheet and Common Int. Questions Flashcards

- Cover C# syntax and concept questions - Cover common C# interview questions - Cover basic thru intermediate .NET Framework knowledge (.NET CLI, ASP.NET + MVC + Razor, LINQ, Entity Framework, Dapper)

1
Q

How do you convert a string of length 1 to a character in C#?

A
//Convert string of length 1 to a character:
string s = "a";
char c = Char.Parse(s); //Works but ONLY if you are guaranteed the string was of length 1.
//Alternative:
string s2 = "b";
char c2 = s2.ToCharArray()[0]; //Takes the first character from the string, even if it was longer than length 1.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

LINQ to SQL supports transactions, views, and stored procedures. It also provides you with compile time error checking for better debugging support.

What is one key very important feature that LINQ to SQL does NOT support?

A

LINQ to SQL does NOT support other database providers other than MS SQL. It will not work for PostGRES SQL, MySQL, etc.

LINQ to SQL has fallen out of favor and now tools such as Entity Framework are preferred.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Show an example of getting data from the console and using string interpolation to print it in C#.

A

Console.WriteLine(“How old are you?”);
string input = Console.ReadLine();
Console.WriteLine($”You are {input} years old!”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the 5 most common (primitive) data types in C#?

BONUS: What are the 4 less common primitive data types of C#?

A

int - whole numbers, like: 1, -56, 948
double - decimal numbers, like: 239.43909, -660.01
char - single characters, like: “a”, “&”, “£”
string - string of characters, like: “dog”, “hello world”
bool - boolean values, like: true or false

BONUS:
long
float
decimal
DateTime
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to you convert a string to an int in C#?

string to double?

string to float?

string to decimal (currency)?

A
//string to int
int something = Convert.ToInt32("233300");
//string to double
double something2 = Convert.ToDouble("1234.5678");
//string to float:
float something3 = Convert.ToSingle("1234.123456789");
//string to decimal:
Convert.ToDecimal("65.95");

//NOTE: Using Convert assumes that the strings are formatted correctly. Otherwise, the best way would be to use TryParse or you are risking an exception.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Use LINQ to compare two arrays for equality.

A

bool isEqual = Enumerable.SequenceEqual(a1,a2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you loop over the characters of a string in C#?

A
//Way 1: Convert to char array then loop over with foreach
string s = "somestring";
foreach (var item in s.ToCharArray()) 
{
    // ...
}
//Way 2: Regular foreach
foreach(char c in s) {

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you take the Maximum of two integers in C#? Use it in a method.

A
//Math.Max(a,b) function demonstrated below in Max Depth of Binary Tree:
public int MaxDepth(TreeNode root) {
        if (root == null)
            return 0;
        return Math.Max(MaxDepth(root.left), MaxDepth(root.right)) + 1;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly