CSF1 Flashcards
How do you create a single line comment?
//
Where does a program START to RUN?
** the Main() method**
Why can’t you break a string into 2 lines?
C# ignores whitespace except inside a string
What should you do with code that does not work?
do NOT delete code that does NOT work; comment out code that does NOT work
What is a variable?
a container;
contents can be changed;
must use some date type
Since a data type cannot be changed programatically…that means what?
It’s TYPE SAFE
Define…
Declaration vs. Initialization vs. Assignment
- Declaring means creating it: datatype & name
- Initializing means giving it a value for the first time
- Assigning means giving it a value
What’s missing here?
int thereCanBe
What’s the difference between the two lines of code?
thereCanBe = 1;
thereCanBe = 2
thereCanBe = 1; is the initiailization of the variable
thereCanBe = 2; is reassignment of the variable
string jedi = “Luke Skywalker”;
** Console.WriteLine(jedi);**
int bigNbr;
bigNbr = 55321;
//declare
//initialization //don't use commas!! they are for hoomans.
What’s the difference here?
Console.WriteLine(bigNbr);
Console.WriteLine(55321);
Console.WriteLine(“55321”)
ConsoleWriteLine() is an output to the screen
Line 1: is output the content of the varialbe
Line 2: is outputing a literal number
Line 3: is output as a string…the letters that look like numbers
What’s the difference between a string and an int?
int deadGoblins = 57460;
int deadOrcs = 42540;
Console.WriteLine(“Orcs Killed: “ + deadOrcs);
Console.WriteLine(“Total Monsters Killed: “
\+ (deadGoblins + deadOrcs));
**What is contatenation? **
adding strings and objects together (b/c everything is an object)
the () around the numeric calcuation trumps the order
of operations and makes sure the concatenation happens AFTER the calculation
What are the rules to naming a variable?
- can only begin with alpha characters
*or underscores _
-After the first character you can use alpha, numberic, or underscore
- CANNOT contain spaces.
- MUST contain at least 1 alpha or numeric
- CANNOT be a C# reserved keyword (pg.19)
-MUST be unique within its scope {}
* */
What’s the difference?
if deadOrcs has already been declared and initialized
int deadOrcs = 43540;
** deadOrcs = 43540;**
**What are constants? **
they are variables that MUST be assigned when declared; the value cannot be reassigned
What does a constant variable look like?
constant datatype variableName = value
Do multiple variables, of the same datatype, have to be declared and initialized individually?
No, they can all be declared together…and initialized individually
What makes up a variable?
datatype variableName = value
it can be declared and initialized at the same time or declared then initialized later
How would declare multiple varialbes, but only initialize one variable?
int blasters, spears, lightsabers = 10;
only lightsabers was initialized
What’s happening here?
** int coaches = 2, players = 30, cheerleaders = 15;**
variables of the same type are being declared and initialized all at the same time
What’s wrong here?
int jedis = 25, name = “Anakin”;
How should this code be written?
variables of different types cannot be declared or initialized together
int jedis = 25;
string name = “Anakin”;
When creating a new cs file, that has a Main() method, what’s the first thing you should do?
//end Main
//end class
//end namespace
Console.WriteLine(“add 2 ints”);
Console.WriteLine(17 + 23);
********************************************
Console.WriteLine(“add 2 strings”);
Console.WriteLine(“17” + “23”);
How do you create a section in a cs file that can be collapsed/expanded?
#region
#enderegion
What are naming convetions?
Rules on how to name things
When is lowercase used?
lowercase
HAS TO BE USED with keywords
This convention is not used anywhere else
What is Hungarian or Lezinski convetion?
btnsomeVariable
ses a lowercase prefix for the first few letters before
* the variable name starts. All “words” in a name after the
* prefix have capitalized 1st letter.
What naming convetion is this?
btnClick
lblDisplay
Hungarian/Lezinski
button variable named Click
label name Display
When is UPPERCASE naming used?
- *UPPERCASE
- Used rarely, most typically with constants to make them** - *stand out, use an _ for a space**
** const int ONE_RING = 1;**
What type of convention is used to name variables?
…Typically this is used for variables and parameters
camelCase
What naming convetion is this?
Uses lowercase letters for the first “word” in the variable
* name, and capitalized 1st letter for all “subsequent” words
camelCase
How is pascal case written?
PascalCase
* - Uses capitalized 1st letter for every “word”
When is PascalCase used?
Typically PascalCase is used for “everything else”:
* namespace, class, method, properties, etc.
Declare and initialize 10 different data types
Use at least:
* 1 string
* 1 integer type
* 1 floating point type
* 1 bool
* 1 char
What is a bool?
**true/false datatype **
How many bits in a byte?
8
Describe a float datatype?
can NOT use actual fractions
float myDay = 1 / 2;
float myDay = .5;
above needs “F” suffix to create a literal type
How many bits in a short?
16
How many bits in an int?
32
How many bits in a long?
64
What’s going on here?
byte byteNbr;
byteNbr = 0;
byteNbr = 255;
Line 1: the variable byteNbr is a byte datatype and is being declared
Line 2: the variable is being initialized
Line 3: the variable is being reassigned
Write an output for your first and last name as…
a variable initialized
a string of text
What is the value range for:
byte
short
int
long
8
16
32
64
What are the floating points?
float
double
decimal
What’s needed for a float value?
…a decimal value?
float requires f or F at the end of the value to fix the decimal point
decimal requires m or M at the end of the value; decimal is used for money
What is this code and what datatype is it?
** isTheDoctor = true;**
a bool variable being initialized
What does string concatenation do?
it adds multiple lines of string together when output
How is char datatype different from int?
a char only accepts 1 character as a value and must be contained in single quotes ‘ ‘
char someVariable = ‘A’;
char symbol = “$”
What can be in a string variable?
any number of characters in double quotes
…letters, numbers, symbols
What is EXPLICIT CASTING?
going from a larger to a smaller container requires extra work.
You have to explicitly state the data you’re casting to. Can be messy if the value won’t fit.
What’s happend here?
decimal dec1 = 4.3m;
** decimal dec2 = (decimal)4.3;**
**they are the same…explicitly cast a double to a decimal **
What are the two main options for output to the console?
WriteLine() …adds a line break after the output
**Write() …does not add a line break **
What’s the difference between
Write()
and
WriteLine()
Write() has no line break after the output
WriteLine() does have a line break
What are 3 options for Input in the console?
Read() – only takes 1 keystroke of input.
ReadyKey() – Similar idea (the book uses this to halt there programs)
ReadLine() – Most common to be used, it allows the user to input something, and reads the user’s input AFTER they hit enter.
What do you do first when using a ReadLine()?
Explain to the user what to type
What do you want to do with a ReadLine()?
capture the input in a variable
Mini Lab:
//ask the user for their favorite color
** //and then tell them the color back and what //you think of it, for example //"COLOR is great!"**
What’s the input datatype for a ReadLine()?
string
How do you change a ReadLine() input to something other than a string?
parsing…
parses the data into a different datatype
string greyHairString = Console.ReadLine();
int greyNbr = int.Parse(greyHairString);
Write out examples of ReadLine()’s parsed into the following datatypes:
int
decimal
How can you invoke the Pares() method for the following
byte
short
int
long
decimal
double
float
byte.Parse() or Byte.Parse()
* short.Parse() or Int16.Parse()
* int.Parse() or Int32.Parse()
* long.Parse() or Int64.Parse()
* decimal.Parse() or Decimal.Parse()
* double.Parse() or Double.Parse()
* float.Parse() or Single.Parse
What are the two ways to change the ReadLine() input string to another datatype
Parsing or Converting
How do is a value converted?
Convert.To32()
What are the relational operators?
> greater than
< less than
>= greater than or equal to
<= less than or equal to
How is = and == used?
= is for assignments
== is to test equality
What’s the operator for not equal?
!=
How do you explore something in intellisens?
with a period
What are the logical operators and what do they do?
&& is used for AND
|| is used for OR
…they compare two values
What is the ToString()
a method to get the string version of any variable of any datatype
“Call” this method after the variable
decimal someDecimal = 1253.6542m;
string nbrString = someDecimal.ToString();
What are escape sequences?
**Special codes used INSIDE a string, which begins with **
****
What does this do?
\n
adds a line break in an output string
How do you show a verbatim string?
@ at the begin of the double quote
“Julia says
"”hello”” and that….”
What does concatenation do?
combines strings with other objects…must use + symbol to put them together
What are two ways to add a line break to the console window?
\n within a string
or an empty
Console.WriteLine();
What are the characteristics of an Array?
have a set size (cant be change programatically)
the size is calld the length (1-based counting)
type safe
use the new keyword to construct
indexes are 0-based counting
How many ways can an Array be created?
There is only one way
string[] dresser = new string[4]
the array has a length of 4
How is an array initialized?
values are initialized individually
and in any order
variable[index] = new variable[length]
How can an array’s values be used?
indicate the index # with the variable name
dress[3]
Can arrays be used in calculations?
YES
Write out a calculation with an array with the length of 5
**decimal totalSale = **
prices[0] + prices[1]…
How do you put an array in an order?
Array.Sort(arrayname);
What’s the difference…
.Sort()
**and **
.Revers()
Array.Sort is used to sort an array ascending
How do you get a descending sort?
First, you must .Sort() the array, then Array.Reverse() to make descending;
if you only use .Reverse…it only flips the contents of the array
Is there a short to sort an array in descending order?
No…you must first .Sort(), then .Reverse() the array
What’s the shortcut to declare and initialize an array?
datatype[] variable = {value comma separated}
When you declare and initialize an array what does .Net do?
it figures what the length should be
Write a Console.WriteLine() using an array with a length of 5
double[] lotsOfSomething = { 34, 45, something };
double something = 12.5;
Console.WriteLine(“The cows we have in the 3rd box is: “
+ cows[2]);
int[] cows = { 0, 80, 20, 40, 15, 42, 99 };
string[] dresser2 = {“socks”, “underwear”, “shirts”,
“shorts”};
decimal totalSale =
(prices[0] + prices[1] + prices[2] + prices[3] + prices[4]);