C# Flashcards
What framework does C# run on?
The .NET Framework.
What method is used to print to console?
Console.WriteLine(“message”);
How do you use writeline placeholders?
Console.WriteLine(“My name is {0} and I am {1} years old”, x, y);
What method is used to get user input?
Console.ReadLine();
How do you convert value types?
Convert.ToXXX
int32, bool, int64
How do you implicitely type a variable?
var (instead of int or bool or whatever)
What is the difference between ++i and i++?
++i increments then proceeds with the expression, i++ does the opposite.
How do you use switch cases?
switch (num){ case 1: //code break; case 2: //code break; default: //code }
What does the continue keyword do?
Breaks the current iteration only
How do you use conditional operator?
msg = Exp1 ? Exp2 : Exp 3;
How do you set a default value for a method parameter?
void method(int x, int y=2);
How can you place your arguments in any order when calling a method?
By calling the parameter variables in the arguments
Ex: method(x: 5, y: 8);
How can you use references in methods and change variable values from a method?
with the ref keyword
void method(ref int y);
method(ref x)
What is similar to a ref, but does not require you to initialize a variable?
out
What is overloading?
Defining methods with the same names but with different parameters.
What is the protected access modifier limited to?
Classes and descendants
How do you use properties?
public string Name {get;set;} //default
public int Age {get{return age;}set{if(value == 0
age = 0}}
How do you use arrays?
int[] myArray = new int[5];
string[] names = new string[3] {“john”, “bob”, “chloe”};
What is a static member?
A member that only belongs to a class, not an object.
What does “this” refer to?
The current instance of the class
What does the readonly keyword do?
Makes it so a member can only be modified through a constructor.
What is operator overloading?
A way of redefining operators
b + b2 (the + now works with objects)
How do you use inheritance?
class Dog : Animal {}
What is polymorphism?
A way to re-implement the same method name through a derived class
How do you use polymorphism?
base class must have "virtual" keyword derived class must have "override" keyword
What are abstract classes?
A class that can’t be instantiated.
What is the advantage of using an interface?
A class can implement multiple interfaces
How do you use exceptions?
try{ //code } catch(Exception e){ //code } finally{}
How do you use generics?
void method(T a, T b){ //code } method(a, b); //could have been , , etc.