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)
How do you convert a string of length 1 to a character in C#?
//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.
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?
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.
Show an example of getting data from the console and using string interpolation to print it in C#.
Console.WriteLine(“How old are you?”);
string input = Console.ReadLine();
Console.WriteLine($”You are {input} years old!”);
What are the 5 most common (primitive) data types in C#?
BONUS: What are the 4 less common primitive data types of C#?
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 to you convert a string to an int in C#?
string to double?
string to float?
string to decimal (currency)?
//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.
Use LINQ to compare two arrays for equality.
bool isEqual = Enumerable.SequenceEqual(a1,a2);
How do you loop over the characters of a string in C#?
//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 do you take the Maximum of two integers in C#? Use it in a method.
//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; }