C# Flashcards
Convert an int to a string.
Convert.ToString(myInt);
Convert an int to double.
Convert.ToDouble(myInt);
Convert double to int.
Convert.ToInt32(myDouble);
Convert bool to string.
Convert.ToString(myBool);
What is the ternary operator syntax?
variable = (condition) ? expressionTrue ; expressionFalse;
What is the syntax for a switch statement?
switch(expression) { case x: //code block ; break; base y: //code block ; break; default: //code block ; break; }
How does a while loop work?
It loops through a block of code as long as a specified condition is true.
What is the syntax for a while loop?
while (condition) { //code block to be executed; }
How does a do/while loop work?
The loop will execute the code block once before checking the condition is true. Will repeat as long as the condition is true.
What is the syntax for a basic do/while loop?
do { //code block to be executed; } while (condition);
Why would you use a for loop?
If you know exactly how many times you want to loop through a block of code.
Explain each part of the basic for loop below.
for (statement 1; statement 2; statement 3) { // code block to be executed ; }
Statement 1: executed one time before the execution of the code block. Sets up the variable before the loop starts (int i=0).
Statement 2: defines the condition for executing the code block. Defines the condition for the loop to run (i <5)
Statement 3: is executed every time after the code block has been executed. Increases the value each time the loop has been executed (i++)
What is the foreach loop used on?
Used to loop through elements in an array.
How would you set up a basic foreach loop?
foreach (type variableName in arrayName) { // code block to be executed; }
Example:
string[] cars = {"Volvo", "BMW", "Ford"}; foreach (string i in cars) { Console.WriteLine(i); }
What are break statements used in and what do they do?
They terminate the closest enclosing loop or switch statement.
What does the continue statement do and where would you use it?
Continue breaks one iteration in a loop.
What is an Array?
A data structure that can store multiple variables of the same type.
Declare an array.
datatype [] variableName;
Call the Sort function on the following array.
string[ ] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”};
Array.Sort(cars);
What namespace is needed for array methods Min, Max, and Sum?
System.Linq
Create and array of 4 elements without adding any elements.
string[] variable name = new string[4];
How do you set a default parameter?
MyMethod(string country = “Norway”)
What is method overloading?
Multiple methods with same name and different parameters.
What is a class?
A template for objects. An object constructor, or a blueprint for creating objects.
What is an object?
An instance of a class. Inherits all variables and methods from class.
Create and object from the Car class.
class Car { string color ="red"; static void Main(string[] args) { Car variableName = new Car(); }
Initialize the following array with any number of elements.
int[] array3;
array3 = new int[numberOfElements]{1,2,3,4,5,6};
Declare and initialize an array without the new keyword.
int[] array1 = {1,2,3,4,5};
What does CLR stand for and what does it do?
CLR = Common Language Runtime.
It translates source code into a form of bytecode known as CIL (Common Intermediate Language). Part of the .Net framework.
What is an access modifier?
A keyword that sets the access level/visibility for classes, fields, methods and properties.
What does the public keyword do?
Makes the code accessible for all classes.