C# Syntax Flashcards

1
Q

What is the basic structure of a C# program?

A

A C# program typically includes using System;, a namespace, a class, and a Main method as the entry point.

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

What does using System; do?

A

using System; imports the System namespace.

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

What is a namespace in C#?

A

A namespace defines a scope that contains a set of related objects.

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

What is a class in C#?

A

class defines a class.

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

What is the entry point of a C# program?

A

Main is the entry point of a C# program.

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

What are the value types in C#?

A

Value types include int, float, double, char, bool, struct, and enum.

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

What are the reference types in C#?

A

Reference types include string, array, class, interface, and delegate.

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

How do you declare an integer variable in C#?

A

You can declare an integer variable like this: int number = 10;

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

How do you declare a constant in C#?

A

You can declare a constant like this: const double PI = 3.14159;

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

What are the arithmetic operators in C#?

A

Arithmetic operators include +, -, *, /, and %.

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

What are the comparison operators in C#?

A

Comparison operators include ==, !=, >, <, >=, and <=.

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

What are the logical operators in C#?

A

Logical operators include &&, ||, and !.

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

What are the assignment operators in C#?

A

Assignment operators include =, +=, -=, *=, and /=.

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

How do you write an if-else statement in C#?

A

An if-else statement is written as follows:

```csharp
if (condition)
{
// code
}
else if (anotherCondition)
{
// code
}
else
{
// code
}
~~~

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

How do you write a switch statement in C#?

A

A switch statement is written as follows:

```csharp
switch (variable)
{
case value1:
// code
break;
case value2:
// code
break;
default:
// code
break;
}
~~~

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

How do you write a for loop in C#?

A

A for loop is written as follows:

```csharp
for (int i = 0; i < 10; i++)
{
// code
}
~~~

17
Q

How do you write a while loop in C#?

A

A while loop is written as follows:

```csharp
while (condition)
{
// code
}
~~~

18
Q

How do you write a do-while loop in C#?

A

A do-while loop is written as follows:

```csharp
do
{
// code
} while (condition);
~~~

19
Q

How do you declare a single-dimensional array in C#?

A

A single-dimensional array is declared like this:

```csharp
int[] numbers = new int[5] {1, 2, 3, 4, 5};
~~~

20
Q

How do you declare a multi-dimensional array in C#?

A

A multi-dimensional array is declared like this:

```csharp
int[,] matrix = new int[2, 2] { {1, 2}, {3, 4} };
~~~

21
Q

How do you define a method in C#?

A

A method is defined as follows:

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

22
Q

How do you call a method in C#?

A

You can call a method like this:

```csharp
int result = Add(5, 10);
~~~

23
Q

How do you define a class in C#?

A

A class is defined as follows:

```csharp
public class Person
{
public string Name { get; set; }
public int Age { get; set; }

public void Display()
{
    Console.WriteLine($"Name: {Name}, Age: {Age}");
} } ~~~
24
Q

How do you create an object in C#?

A

An object is created like this:

```csharp
Person person = new Person { Name = “John”, Age = 30 };
person.Display();
~~~

25
What is inheritance in C#?
Inheritance allows a class (derived class) to inherit members from another class (base class).
26
How do you define a base class in C#?
A base class is defined as follows: ```csharp public class Animal { public void Eat() { Console.WriteLine("Eating..."); } } ```
27
How do you define a derived class in C#?
A derived class is defined as follows: ```csharp public class Dog : Animal { public void Bark() { Console.WriteLine("Barking..."); } } ```
28
How do you use a derived class in C#?
You can use a derived class like this: ```csharp Dog dog = new Dog(); dog.Eat(); dog.Bark(); ```
29
How do you define an interface in C#?
An interface is defined as follows: ```csharp public interface IShape { void Draw(); } ```
30
How do you implement an interface in C#?
An interface is implemented like this: ```csharp public class Circle : IShape { public void Draw() { Console.WriteLine("Drawing a circle"); } } ```
31
How do you handle exceptions in C#?
Exceptions are handled using try-catch blocks: ```csharp try { // code that may throw an exception } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { // code that will always execute } ```
32
How do you define a delegate in C#?
A delegate is defined as follows: ```csharp public delegate void MyDelegate(string message); ```
33
How do you use a delegate in C#?
You can use a delegate like this: ```csharp public class MyClass { public static void DisplayMessage(string message) { Console.WriteLine(message); } } MyDelegate del = new MyDelegate(MyClass.DisplayMessage); del("Hello, Delegate!"); ```
34
What is LINQ in C#?
LINQ (Language Integrated Query) allows querying collections in a concise way.
35
How do you use LINQ in C#?
An example of using LINQ is: ```csharp var numbers = new List { 1, 2, 3, 4, 5 }; var evenNumbers = from num in numbers where num % 2 == 0 select num; ```
36
What are attributes in C#?
Attributes provide metadata about classes, methods, or other entities.
37
How do you use attributes in C#?
An example of using an attribute is: ```csharp [Obsolete("This method is obsolete. Use NewMethod instead.")] public void OldMethod() { // code } ```
38
What is asynchronous programming in C#?
Asynchronous programming allows methods to run without blocking the main thread.
39
How do you use async and await in C#?
You can use async and await like this: ```csharp public async Task GetDataAsync() { await Task.Delay(1000); // Simulate a delay return "Data"; } ```