C# Flashcards

(49 cards)

1
Q

How do you read all lines from a text file in C#?

A

string[] lines = File.ReadAllLines(“filename.txt”);

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

How do you write all lines to a text file in C#?

A

File.WriteAllLines(“filename.txt”, lines);

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

How do you use a foreach loop in C# to print every item in an array?

A

foreach (var item in array) { Console.WriteLine(item); }

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

How do you write a while loop that continues until x equals 10?

A

while (x != 10) { // code here }

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

How do you ask a user for input from the console in C#?

A

Console.WriteLine(“Enter your name:”); string name = Console.ReadLine();

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

How do you convert a string to an integer in C#?

A

int number = int.Parse(Console.ReadLine());

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

How do you split a string by spaces in C#?

A

string[] words = sentence.Split(‘ ‘);

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

How do you iterate over an array with a for loop in C#?

A

for (int i = 0; i < array.Length; i++) { Console.WriteLine(array[i]); }

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

How do you implement bubble sort in C#?

A

for (int i = 0; i < arr.Length - 1; i++) { for (int j = 0; j < arr.Length - i - 1; j++) { if (arr[j] > arr[j + 1]) { var temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; }}}

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

How do you implement merge sort in C# (overview)?

A

Split the array into halves, recursively sort each half, and merge them. See next card for code.

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

What is a simple example of merge sort in C#?

A

void MergeSort(int[] array) { if (array.Length <= 1) return; int mid = array.Length / 2; int[] left = array[..mid]; int[] right = array[mid..]; MergeSort(left); MergeSort(right); Merge(array, left, right); }

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

How do you define a class in C#?

A

public class Person { public string Name; public int Age; }

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

How do you create an object from a class in C#?

A

Person p = new Person(); p.Name = “Alice”; p.Age = 30;

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

What is a constructor in C#?

A

A constructor is a method that runs when an object is created: public Person(string name) { Name = name; }

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

How do you use inheritance in C#?

A

class Student : Person { public string School; }

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

What is polymorphism in C#?

A

Polymorphism allows methods to behave differently depending on the object. Use virtual and override keywords.

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

How do you declare a method in C#?

A

public int Add(int a, int b) { return a + b; }

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

What is the difference between == and .Equals() in C#?

A

== compares value or reference depending on the type; .Equals() is a method you can override for custom comparison.

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

How do you check if a string contains another string in C#?

A

if (text.Contains(“keyword”)) { /* code */ }

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

How do you convert a number to a string in C#?

A

string s = number.ToString();

21
Q

How do you parse a string into a double safely in C#?

A

double.TryParse(input, out double result);

22
Q

How do you define and use an array in C#?

A

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

23
Q

How do you define a list and add to it in C#?

A

List<int> nums = new List<int>(); nums.Add(5);</int></int>

24
Q

How do you use a switch statement in C#?

A

switch (value) { case 1: Console.WriteLine(“One”); break; default: Console.WriteLine(“Other”); break; }

25
How do you handle exceptions in C#?
try { /* code */ } catch (Exception ex) { Console.WriteLine(ex.Message); }
26
How do you define and access a tuple in C#?
var person = (25, "Alice"); Console.WriteLine(person.Item1);
27
How do you give names to tuple elements in C#?
var person = (age: 25, name: "Alice"); Console.WriteLine(person.name);
28
How do you declare and use a Dictionary in C#?
var dict = new Dictionary(); dict["age"] = 30;
29
How do you iterate over a Dictionary in C#?
foreach (var pair in dict) { Console.WriteLine(pair.Key + ": " + pair.Value); }
30
How do you cast a double to an int in C#?
int x = (int)3.7; // result: 3
31
How do you safely convert a string to an int in C#?
if (int.TryParse(input, out int result)) { /* use result */ }
32
How do you make a string uppercase in C#?
string upper = myString.ToUpper();
33
How do you replace part of a string in C#?
string newStr = str.Replace("old", "new");
34
How do you get a substring from a string in C#?
string part = str.Substring(0, 5);
35
How do you define an enum in C#?
enum Days { Monday, Tuesday, Wednesday }
36
How do you use an enum in C#?
Days today = Days.Monday; Console.WriteLine((int)today);
37
How do you create and use a Queue in C#?
Queue queue = new Queue(); queue.Enqueue(1); int first = queue.Dequeue();
38
How do you create and use a Stack in C#?
Stack stack = new Stack(); stack.Push(1); int last = stack.Pop();
39
What is recursion in C#?
A method that calls itself. Useful for problems like factorial, Fibonacci, tree traversal.
40
Write a simple recursive factorial method in C#
int Factorial(int n) { return n <= 1 ? 1 : n * Factorial(n - 1); }
41
What are access modifiers in C#?
They control visibility: public (anywhere), private (class only), protected (class + subclass), internal (same assembly)
42
When should you use 'private' in C#?
Use private to hide class internals from other parts of the program.
43
What is the purpose of 'public' in C#?
Use public for methods or properties you want accessible from other classes.
44
How do you generate a random number in C#?
Random rand = new Random(); int n = rand.Next(1, 101); // 1 to 100
45
How do you round a number to the nearest integer in C#?
double rounded = Math.Round(3.6);
46
How do you get the square root of a number in C#?
double root = Math.Sqrt(25); // 5
47
How do you get the current date and time in C#?
DateTime now = DateTime.Now;
48
How do you format a DateTime as a string in C#?
string formatted = now.ToString("dd/MM/yyyy HH:mm");
49
How do you add days to the current date in C#?
DateTime tomorrow = DateTime.Now.AddDays(1);