C# Basics / File I/O / Console App Flashcards
How would you display a line of text in the console?
Console.Write() - to continue on same line Console.WriteLine() - to start new line after displayed
What is string interpolation and how is it used?
String interpolation enables you to insert values in string literals to create formatted strings.

How would you read a sting literally or verbatim?
@”C:Windows\System32”
What is composite formatting of parameters?
{Index [, alignment][:formatString]
Console.WriteLine(“{0,-20} {1,5}\n”, “Name”, “Hours”);
// The example displays the following output:
// Name Hours
What is a list?
A list is similar to an array but provides additional functionality, such as dynamic resizing.
Define: Index
Index is a number starting from 0 that identifies a corresponding item in the list of objects.
Define: Alignment
Alignmet is a signed integer indicating the preferred formatted field width
Define: Format string
Format String is a format string that is appropriate for the type of object being formatted
What datatypes does C# read from the keyboard?
ONLY STRINGS, unline java, so the need for parsing is often used
How would you parse a readline to an integer?

What is the other way to convert a sting into an integer?
int myAge = Convert.ToInt32(input)
What is the naming convention for methods inside of a class in C#?
Pascal not camel case!
eg. SetName
How would you manually create a property to get and set the instance variable?

What would a Class diagram look like with a property?

How would you manually add validation to a property set method?
Private decimal balance: // instance variabl

How would you create an auto-implemented property?
public string Name { get; set; }
What is the format specifier for currency and how is it used?
Format Specifier :C

What are the main format specifiers?

What are the default values for the simple data types in C#?
Types char, byte, sbyte, short, ushort, int, uint, long, ulong, float, double, and decimal are all given the value 0 by default.
Type bool are given a value of false by default.
Reference-type instance variables are of type null by default.
What datatype do you use for monetary amounts?
decimal
What are the conversion types of datatypes?

What is an optional paramater and how is it used?
Methods can have optional parameters that allow the calling method to vary the number of arguments to pass. An optional parameter specifies a default value that’s assigned to the parameter if the optional argument is omitted. You can create methods with one or more optional parameters. All optional parameters must be placed to the right of the method’s non-optional parameters.
eg. static int Power(int baseValue, int exponentValue = 2);
What are names parameters and how are they used?
C# provides a feature called named parameters, which enable you to call methods that receive optional parameters by providing only the optional arguments you wish to specify.
Explicitly specify the parameter’s name and value—separated by a colon (:)—in the argument list of the method call.
For example:
// sets the time to 12:00:22
t.SetTime( hour: 12, second: 22 );
The compiler assigns parameter hour the argument 12 and parameter second the argument 22. The parameter minute is not specified, so the compiler assigns it the default value 0.
It’s also possible to specify the arguments out of order when using named parameters. The arguments for the required parameters must always be supplied.
What is the new syntax for methods that just return a value?
static int Cube(int x) => x * x * x;
(lambda expressions)
used to be:
static int Cube(int x)
{
return x * x * x;
}
How would you use a lambda expression (bodied method) to return true or false?
public bool IsNoFaultState => State == “MA” || State == “NJ” || State == “NY” || State == “PA”;
What are the out and ref keywords used for?
Normally functions are passed and return “Values” Meaning that the value passed on if changed does not effect the original variable/number. OUT and REF help pass variables by reference, so if they are passed into another method and changed, the original is changed too becuase its referencing back to the original object.
How are the ref and out keywords used?
The out can only send reference out of a method, not pull it in. Ref does both.
eg. out: send objVal 20 in, add objVal 10, send out objVal = 10;
ref: send objVal 20 in, add objVal 10, send out objVal = 20;
what does the code for ref keyword look like?

what does the code for out keyword look like?

How do you create a named constant?
const int ArrayLength = 5;
What is a foreach stament and how does it work?
The foreach statement iterates through the elements of an entire array or collection.

How would you create a two dimentional rectangular array?
A two-by-two rectangular array b can be declared and initialized with nested array initializers as follows:
int [,] b = {{1, 2}, {3, 4}};
The initializer values are grouped by row in braces. The compiler will generate an error if the number of initializers in each row is not the same, because every row of a rectangular array must have the same number of column
How would you create a two dimentional jagged array?
A jagged array with three rows of different lengths could be declared and initialized as follows:
int [][] jagged = {
new int [] {1, 2},
new int [] {3},
new int[] {4, 5, 6}
};
How would you create a variable-length argument list?
Variable-length argument lists allow you to create methods that receive an arbitrary number of arguments. The necessary params modifier can occur only in the parameter list’s last parameter.

What library is used for the main I/O funcionality?
using System.IO;
How do you create a new filestream?
FileStream file = new FileStream(“path.txt”, FileMode , FileAccess );
FileMode specifies what the FileStream should do with the file to begin with
The FileMode.option can be set to one of: Append, Create, CreateNew, Open, OpenOrCreate, or Truncate
FileAccess specifies the file privileges. The FileAccess.option can be set to one of: Read, ReadWrite, or Write
How do you write to a file?
StreamWriter data = new StreamWriter(file);
Two options for writing to a file:
Write, or WriteLine data.Write(“Hello file system world!”);
Remember to close the stream when done data.Close();
How do you read from a file?
StreamReader data = new StreamReader(file);
Four options for reading from a file:
Read, ReadBlock, ReadLine, or ReadToEnd
string line = data.ReadToEnd();
Remember to close the stream data.Close();
When done with the file make sure to close that as well file.Close();
How do you check for the end of a file?

How do you view file information?

How do you list Disk Drives?

How do you list Subfolders?

How do you list files?
