C# Basics Flashcards

1
Q

What is the first word in a C# file?

A

The first word in the file is class.

All code is contained in a class.

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

How many classes can your program have?

A

As many as you need it to have.

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

How many classes are TYPICALLY in one file?

A

One.

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

If there is only one class in a file, what should you typically name the file?

A

The same thing that you named the class within the file.

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

What is a C# file extension?

A

.cs

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

What element is one step down in the hierarchy from a class that you use to further organize the code?

A

A method. You can add as many as you need.

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

What are the four basic parts of a method?

A

Name, body, parameters and type.

Name (of the methoid), body (inside curly braces - this is where you write the instructios), Parameters (info listed next to the method name in parenthesis - used when calling methods , tells other methods what info is needed if you use the method) AND finally TYPE. Tells what kind of info the method will return.

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

What is “calling a method”

A

You can make one of your methods use (or call upon) another method.

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

What are the three basic building blocks of a C# program?

A

(in order) Files, classes and methods.

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

How does the compiler know which code to run first in your program?

A

The file containing the class containing the method named “Main()”

All of the methods you write can be called whatever you want, but one has to be called Main.

Capital M

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

What is a keyword?

A

Keywords are special words that are reserved for the language itself.

This means that we can’t use them in names of things like classes and methods.

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

What are the 4 P’s of Problem Solving?

A

First, there’s preparation where we understand the problem and think of a solution.

Second, plan, where we plan out the solution.

Third, perform. Where we perform the actions required for our purposed solution.

And finally, fourth, we perfect the solution.

Perfecting doesn’t mean that the project that the project will be perfect

when we get to the end of the perfecting stage.

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

Which method in the .net framework can be used to print to the console?

A

System.Console.Write(“”);

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

Which method in the .net framework can be used to read from the console?

A

System.Console.ReadLine();

What this method does do is return what the user typed back to our program.

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

Variable:

A

A variable is a way to temporarily store information that our code can use.

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

First step in creating a variable:

A

Specifying the TYPE of info it needs to store. (e.g. string )

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

Second step in creating a variable:

A

Give it a name.

Variable names can contain letters, numbers, or an underscore character.
1:38
But they can’t start with a number.
1:39
The name should be descriptive of what it’s meant to store.

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

What is a variable assignment?

A

Taking a pre-existing declared variable and setting it equal to a value that agrees with its declared type.

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

What does Initializing a variable mean?

A

Declaring it and assigning in the same line.

aka - string firstName = “Ben”;

instead of

string firstName;
firstName = “Ben”;

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

Disect the following method from the .Net Framework: System.Console.Write

A

Write is the name of the method.

Console is the name of the CLASS that contains the METHOD “Write” within the framework.

System is the NAMESPACE within the framework. Classes are contained within namespaces.

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

What is the purpose of a namespace?

A

To make it so that we could potentially have multiple classes with the same name. Think of a namespace as the name of a town and the class is main street.

You cant just send a letter to mainstreet, you have to send it to main street in Grantsville.

Now, if there are multiple “mainstreet” classes in your program, the compiler can distinguish between the Classes “mainStreet” in the namespaces Grantsville and Tooele.

You can use more than one namespace and further organize your library of code.

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

How can you eliminate the use of specific namespaces throughout your file?

A

Include a “using directive”. Type “using” followed by the name of the namespace. You will then not have to type that out every time.

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

What does the System.Console.WriteLine method from the .Net Framework, do?

A

It prints text to the console and then brings the cursor to the next line.

This differs from theSystem.Console.Write method because the cursor remains on the same line once that method is called.

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

What is string concatenation?

A

Adding multiple strings together using the + symbol. (also called the concatenation operator.)

Console.WriteLine(“You’ve entered “ + entry + “ minutes.”);

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

What is a REPL?

A

Read Evaluate Print Loop

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

What does a REPL do?

A

Allows you to write a small bit of code and test weather it works or not without having to create a full class or file to go with it. The Mono compiler allows you to do this.

27
Q

How do you use the C# REPL in Mono?

A

type “csharp>” in the console, this will start the REPL. You can then add your code and test.

28
Q

Why is using a REPL valuable?

A

It allows you to experiment and learn the language one bit at a time.

29
Q

With the string and int types, which require the value to be enclosed in double quotes?

A

String does require, quotes are not used with int tho.

30
Q

Interger

A

In mathematics,
2:10
an integer is a number that can be written without a decimal component.

31
Q

What will happen if you try to add an int with a string?

A

String concatenation. This may not be the intended result. Be sure to pay attention to data types when trying to either add int values or combine strings with concatenation.

32
Q

Parsing

A

Extracting data out of text.

33
Q

How can you convert a string to an int?

A

Parse the int out of the string using int.Parse

34
Q

How do you compile your C# file using the Mono compiler?

A

In the console, type “mcs “ followed by the name of your .cs file.

35
Q

How do you run your compiled program .exe using Mono?

A

In the console, type “mono “ followed by the name of the .exe file you wish to run.

36
Q

What is the modulus operator?

A

% it returns the remainder.

37
Q

What is the order of arithmetic operations in C#?

A

Same as in regular math. mult, div and modulous first, then add and sub.

If there are multiple div , mult or mod in a row, they will be resolved from left to right.

The highest precidence goes to anything in parenthesis, so you can force specific items to go first.

38
Q

What is a loop?

A

A loop is used to repeat a section of code until something tells it to stop.

39
Q

How do you turn a block of code into a loop?

A
while ()
{
   // Your code
}

The code inside your loop will run as long as the parameters set in () remain true.

As soon as they are false, the code immediately following the curly braces will be ran.

40
Q

What is boolean logic?

A

The idea of a condition being true or false.

41
Q

What is the bool type?

A

Short for boolean. Can only store true or false. Returns 0 or 1.

42
Q

What is the comparison operator and what does it do?

A

== it compares two strings against eachother and returns a boolean value. If the thing on the left = the thing on the right, it will return true.

43
Q

What does != do?

A

Compares if two values are NOT equal and returns T or F`

44
Q

What is the dif between while and if?

A

Ifs dont loop. They run once.

45
Q

How does a program look at an if statement?

A

It checks to see if the condition (that you add in () next to if) is true. If it is, it will run the code inside of your if statement. If not, it will move on to the next line.

The condition in your if must resolve to a bool value.

46
Q

How do you include an else to an already declared if statement and how will it run?

A

You include “else ()
{
}

Immediately after the closing } of the if statement that you are working with.

The else will only run if the if returns false.

47
Q

How do you include double quotes into a string? (making sure that the compiler dosent think that the string ends when you add “ ?)

A

You have to escape the quotes using \ before the “

48
Q

How do you tell a loop to start over at it’s begining from the middle?

A

With a continue statement.

“continue;”

The loop will start over from its beginning.

49
Q

What is input validation?

A

A user can type anything they want in the console, but

not everything will make sense to our program so

we need to have some rules about what the program can and can’t accept.

And then enforce those rules somehow.

50
Q

When doing input validation, how do you tell your method to check and see if what the user entered was either a string or an int?

A

You can use a try catch construct. What this does is sets up two areas. A first area of code to TRY, but if the compiler throws an exeption, it will execute the instructions contained in the CATCH section.

try
{
// All of the code that you want to try but might throw an exeption if the user's input isnt valid.
}
catch(nameOfExeptionBeingThrown)
{
Console.WriteLine("Sorry, you entered something invalid.");
}
51
Q

If you want to make bothj caps and lower case valid input, what is an easy way to do this?

A

Use the name.ToLower method. So what you would do is compare the user’s input to the input that you are looking for (like the word quit , all lower case) but when you call the user’s input variable name , follow it with .ToLower which will convert all of the characters to lower case and force it to match the input you are looking for. This will make both lower and upper case input valid because it will all end up lower case.

52
Q

What is the data type for decimal numbers?

A

double

53
Q

What method can you use to have the console tell you what type of data a variable is?

A

.GetType

54
Q

If an int and a double are combined, what will the resulting data type be?

A

double

55
Q

What is a cast?

A

A cast is a way of explicitly informing the compiler that you intend to make the conversion and that you are aware that data loss might occur. To perform a cast, specify the type that you are casting to in parentheses in front of the value or variable to be converted.

(int)2.9

56
Q

What does it mean to truncate?

A

Truncate restricts the maximum length of strings. We truncate a string so that it has a substring of the first specified number of letters. This is useful for text in databases or in user interfaces where space is limited.

e.g. (int)2.9 will return 2. The .9 is trunkated.

57
Q

If the result of an arithmetic operation between two ints results in a decimal, what will the result’s data type be?

A

It will remain an int and be trunkated.

58
Q

What is refactoring code?

A

Its when you change the code without changing the apparent functionality.

59
Q

Name 6 reasons to refactor your code.

A

Readability, maintainability, Testability

Performance, extensibility, compliance

60
Q

how do you tell a loop to end?

A

You use a break statement.

61
Q

What is compile time inference?

A

It is when you allow the compiler to decide (or infer) the datatype of a variable as its being compiled.

What this means is that instead of specifically declaring a variable’s datatype, you just declare it as a “var” and allow the compiler to look at the data on the right side of the assignment operator to decide for its self what the type is.

var x = 5

int

var y = 6.3 
double

ver z = “hello!”
string

62
Q

What is syntactic sugar?

A

Its redundant but allows you to shorten the code a bit, making it more readable.

one example would be taking a variable and arithmetically combining it with another variable.

instead of x = x + y, you can do x += y
or
x -= y , x *= y, x /= y, x %= y

63
Q

What will += do?

A

it will take the var on the left and add its self to the var on the left.