C# Syntax Flashcards
What is the basic structure of a C# program?
A C# program typically includes using System;
, a namespace
, a class
, and a Main
method as the entry point.
What does using System;
do?
using System;
imports the System
namespace.
What is a namespace in C#?
A namespace
defines a scope that contains a set of related objects.
What is a class in C#?
class
defines a class.
What is the entry point of a C# program?
Main
is the entry point of a C# program.
What are the value types in C#?
Value types include int
, float
, double
, char
, bool
, struct
, and enum
.
What are the reference types in C#?
Reference types include string
, array
, class
, interface
, and delegate
.
How do you declare an integer variable in C#?
You can declare an integer variable like this: int number = 10;
How do you declare a constant in C#?
You can declare a constant like this: const double PI = 3.14159;
What are the arithmetic operators in C#?
Arithmetic operators include +
, -
, *
, /
, and %
.
What are the comparison operators in C#?
Comparison operators include ==
, !=
, >
, <
, >=
, and <=
.
What are the logical operators in C#?
Logical operators include &&
, ||
, and !
.
What are the assignment operators in C#?
Assignment operators include =
, +=
, -=
, *=
, and /=
.
How do you write an if-else statement in C#?
An if-else statement is written as follows:
```csharp
if (condition)
{
// code
}
else if (anotherCondition)
{
// code
}
else
{
// code
}
~~~
How do you write a switch statement in C#?
A switch statement is written as follows:
```csharp
switch (variable)
{
case value1:
// code
break;
case value2:
// code
break;
default:
// code
break;
}
~~~
How do you write a for loop in C#?
A for loop is written as follows:
```csharp
for (int i = 0; i < 10; i++)
{
// code
}
~~~
How do you write a while loop in C#?
A while loop is written as follows:
```csharp
while (condition)
{
// code
}
~~~
How do you write a do-while loop in C#?
A do-while loop is written as follows:
```csharp
do
{
// code
} while (condition);
~~~
How do you declare a single-dimensional array in C#?
A single-dimensional array is declared like this:
```csharp
int[] numbers = new int[5] {1, 2, 3, 4, 5};
~~~
How do you declare a multi-dimensional array in C#?
A multi-dimensional array is declared like this:
```csharp
int[,] matrix = new int[2, 2] { {1, 2}, {3, 4} };
~~~
How do you define a method in C#?
A method is defined as follows:
```csharp
public int Add(int a, int b)
{
return a + b;
}
~~~
How do you call a method in C#?
You can call a method like this:
```csharp
int result = Add(5, 10);
~~~
How do you define a class in C#?
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}"); } } ~~~
How do you create an object in C#?
An object is created like this:
```csharp
Person person = new Person { Name = “John”, Age = 30 };
person.Display();
~~~