C# Flashcards
What is a variable?
~ storage location in memory (RAM) that a programmer can use to store data
What parts does a variable have? (5)
- identifiers: labels to refer to storage location
- scope: availability of data while program executes / in scope if still in memory
- address: actual location in RAM
- data type: defines storage and proper use of data
- value: actual data that is stored in variable
List the 5 different .NET data types.
integer
floating-point
boolean
char
built-in reference
What are the 2 subtypes of .NET built-in reference data types?
string and object
List the 8 different .NET integer data types with their respective Visual C# name.
- System.Byte - byte
- System.Int16 - short
- System.Int32 - int
- System.Int64 - long
- System.SByte - sbyte
- System.UInt16 - ushort
- System.UInt32 - uint
- System.UInt64 - ulong
C# byte data type
8 bit, unsigned, 0 to 255
Give description (sign and storage) and range of the C# short data type.
signed, 16 bit, -32768 to 32767
Give description (sign and storage) and range of the C# int data type.
32 bit, signed, -2^31 to 2^31-1
Give description (sign and storage) and range of the C# long data type.
64 bit, signed, -2^63 to 2^63-1
Give description (sign and storage) and range of the C# sbyte data type.
8 bit, signed, -128 to 127
Give description (sign and storage) and range of the C# ushort data type.
16 bit, unsigned, 0 to 65535
Give description (sign and storage) and range of the C# uint data type.
unsigned, 32 bit, 0 to 2^32-1
Give description (sign and storage) and range of the C# ulong data type.
64 bit, unsigned, 0 to2^64-1
List the 3 floating point data types with their type name and their C# name.
- System.Single - float
- System.Double - double
- System.Decimal - decimal
Give description, precision and range of the float floating point data type.
- 32 bit - 7 significant digits - +/-1.4 x 10^-45 to +/-3.4 x 10^38
Give description, precision and range of the double floating point data type.
- 64 bit - 15 - 16 sign. digits - +/-5.0 x 10^-324 to +/-1.7 x 10^308
Give description, precision and range of the decimal floating point data type.
- 128 bit - 28 sign. digits - +/-1.0 x 10^-28 to +/-7.9 x 10^28
What is the System.Boolean type called in C#?
bool
What values can Boolean take on?
only true and false
What is the System.Char called in C# and what does it represent?
Char and it represents a single 16-bit Unicode character by enclosing it in single quotes.
What is the use of a string data type?
You can assign a series of char data, e.g. a word or a paragraph, to a string variable.
What difference is there between the assignment of a char literal in contrast to a string literal.
- Char values are enclosed in single quotes.
- String values are enclosed in double quotes.
What type of data can you assign to System.object?
It is the super type. All others derive from it and you can assign any object or value to it.
How do you assign an object to the object type?
object myObject;
myObject = 543
myObject = new System.Windows.Forms.Form()
When do implicit conversions happen in C#?
- automatically performed whenever conversion can be performed without loss of data
Code an implicit conversion of a variable storing the number 100 into 64 bit.
int SomeNumber = 100;
long LongVariable;
LongVariable = SomeNumber;
What is an explicit conversion also called?
a cast
Give a generic example of an explicit conversion of a chosen number.
int someInteger = 22
short someShort;
someShort = (short)someInteger;
What is Parse? (4)
- used to create a numeric value from a string
- can convert user data entries from text boxes into usable data
- results in an error if string cannot be read as a numeric value
- Parse is a static (Shared) and must be called from the type object rather than an instance
Give a simple example for a Parse conversion of a string ‘123456’.
int I;
string S;
S = “123456”;
I = int.Parse(S);
Give a list of the arithmetic operators under C#.
- addition, subtraction, multiplication
- division with integers
- division with floating points
- modulus
- unary (=, increment/decrement operators, shortcut operators (+=, -=, *=, /=, %=), cast operator)
Which comparison operators does C# know?
/* relational: < > <= >=
equality: == != */
What logical operators does C# know? (5)
- logical AND (“&”)
- logical OR (|)
- logical XOR (^)
- conditional AND (“&&”)
- conditional OR (||)
Give the rough order of precedence of operators in C# from highest to lowest? (14)
- primary
- unary
- multiplicative
- additive
- shift
- relational
- equality
- logical AND
- logical XOR
- logical OR
- conditional AND
- conditional OR
- ternary
- assignment
Microsoft Visual Studio is the main … (IDE) from Microsoft.
Integrated Development Environment
What is an Integrated Development Environment? (4)
- is a software suite that consolidates the basic tools developers need to write and test software
- contains a code editor, a compiler or interpreter and a debugger
- accessed through a single graphical user interface (GUI)
- may be a standalone application or it may be included as part of one or more existing and compatible applications
In which editions is Microsoft Visual Studio available? (4)
- Visual Studio Express Editions
- Visual Studio Standard Edition
- Visual Studio Professional Edition
- Visual Studio Team System
Can you mix and match Frameworks and Visual Studio Editions?
No.
List the .NET Framework editions and the complementing Visual Studio edition.
- .NET Framework 1.0 - Visual Studio 2002
- .NET Framework 1.1 - Visual Studio 2003
- .NET Framework 2.0 - Visual Studio 2005
- .NET Framework 3.5 - Visual Studio 2008
- .NET Framework 4 - Visual Studio 2020
What can you build in Visual Studio 2010? (6)
- rich Windows applications and libraries
- Windows services
- Console application
- Dynamic web applications and libraries
- XML web services (cross platform, cross language, cross firewall)
- smart device applications
Windows apps require … Windows apps provide a consistent … (…) for communicating with drivers, devices, and so on.
a Windows Runtime Environment, API (Application Programming Interface)
.NET apps require … .NET apps provide a … for working with Windows, data structures, types, and so on. … must be installed to run any .NET apps.
.NET Runtime Environment, consistent API, .NET runtime
What are the 2 components of .NET Framework?
Framework Class Library (FCL) [here Base Class Library]
and
Common Language Runtime (CLR)
What is CLR? (4)
- ~an application virtual machine that provides services such as security, memory management, and exception handling. (As such, computer code written using .NET Framework is called “managed code”.)
- it’s a software environment, programs written for .NET Framework execute in (in contrast to a hardware environment)
- process known as just-in-time compilation converts compiled code into machine instructions which the computer’s CPU then executes
What is the FCL? (3)
- a large class library named Framework Class Library
- provides language interoperability (each language can use code written in other languages) across several programming languages
- consists of an object-orientated collection of reusable classes (types) that we can use to develop applications
List the 5 layers of .NET Framework architecture.
- VB, C++, C#, JScript, …
- Web Forms (ASP.NET), Windows Forms
- Base Class Library
- Common Language Runtime
- Windows
What is “managed code”?
- ~ a computer program source code that executes only under management of a CLR virtual machine like .NET Framework or Mono
- written in 1 of over 20 high-level programming languages that are available for use with the Microsoft .NET Framework, including C#, J#, Microsoft Visual Basic .NET, Microsoft JScript and .NET
The CLR consists of … that load the … (IL code) of a program into the runtime, which compiles the IL code into …
class loader, intermediate language code, native code
What is IL code?
~ low level language designed to be read and understood by the CLR
What is a runtime?
~ the period of the program lifecycle phase during which a computer program is executing
What is “unmanaged code”?
~ refers to programs written in C, C++, and other languages that do not need a runtime to execute
Who developed C#?
Anders Hejlsberg and his team during the development of .Net Framework.
Give the most common advantages for using C#. (8)
- modern, general-purpose programming language
- object oriented
- component oriented
- easy to learn
- structured language
- produces efficient programs
- can be compiled on a variety of computer platforms
- part of .Net Framework
List important features of C#. (11)
Boolean Conditions
Automatic Garbage Collection
Standard Library
Assembly Versioning
Properties and Events
Delegates and Events Management
Easy-to-use Generics
Indexers
Conditional Compilation
Simple Multithreading
LINQ and Lambda Expressions
Integration with Windows
What is Mono?
- an open-source version of the .NET Framework which includes a C# compiler and runs on several operating systems
- runs on Android, BSD, iOS, Linux, OS X, Windows, Solaris, and UNIX
What is a namespace?
~a collection of classes
What does Console.ReadKey(); do?
- makes the program wait for a key press
- prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.
What is the entry point for all C# programs?
the Main method
What is computer decision making generally like?
- all computer decisions are yes-or-no decisions (boolean values: true and false)
- Decision structure is a logical structure that involves choosing between alternative courses of action based on some value within program
What do you use an if-statement for?
- use to make a single-alternative decision
- use it to determine whether an action will occur
What is the general structure of an if-statement?
if (testedExpression)
statement;
- testedExpression is any expression that evaluates as true or false
- statement represents the action that will take place if the expression evaluates as true
What kind of expressions can you test for in C# if-statements?
Must be boolean!
- comparison statements such as e.g. >= or ==
- boolean variables such as e.g. IsValidNumber
What is the format to execute more than one statement in an if-statement?
Use curly brackets!
if (hoursWorked > FULL_WEEK) {
regularPay = FULL_WEEK * rate;
overtime = (hoursWorked – FULL_WEEK) * OT_RATE * rate;
}
What happens if you declare a variable within a statement block?
It can be used only within that block!
Declare it outside the if-statement to be able to continue using it.
What is the format to code dual-alternative if decisions?
if (testedExpression)
statement;
else
statement;
Else without if is illegal!
What are nested if statements?
~statements in which if structure is contained inside of another if structure
When do you use nested if statements?
-when two conditions must be met before some action is taken
Explain the decision making process in the following code:
if (saleAmount > 1000)
if (saleAmount < 2000)
bonus = 50;
else
bonus = 100;
- If saleAmount is between $1000 and $2000, bonus is $50 because both evaluated expressions are true
- If saleAmount is $2000 or more, bonus is $100 because the first evaluated expression is true and the second one is false
- If saleAmount is $1000 or less, bonus is unassigned because the first evaluated expression is false and there is no corresponding else
What are common mistakes in writing up if statements?
- placing a ; right after the testedExpression
- using the assignment operator = instead of the equality operator == in a testedExpression
- not using curly brackets to group several action statements
What happens if you code several executable actions for an if statement, but forget to put them into curly brackets?
- they are not grouped, so only the first statement depends on the testedExpression
- after the if statement, instead of breaking out of the expression, the other action statements will be carried out regardless as independent expressions
What happens if you use the assignment operator in a testedExpression instead of the equality operator?
if (number = 3)
statement;
Illegal!
-will produce error
What happens if you place a semicolon right after the testedExpression in an if statement?
if (testedExpression) ;
statement;
- testedExpression has no action
- statement will execute independently of the testedExpression
What types of loops exist?
while
for
do..while
One execution of any loop is called an …
iteration
What are the characteristics of a while loop?
~executes body of statements continuously a long as the Boolean expression at the entry of the loop is true
What is the syntax of a while loop?
while (Boolean expression)
loop body;
What is a loop called that executes a specific number of times?
A definite loop or a counter controlled loop.
What is the general syntax of a counter controlled loop?
Loop control variable = a
Limiting value = B
int a;
const int B = 11;
a = 1;
while (a < B) {
Console.WriteLine(a)
a = a + 1;
}
What are the syntax rules for a definite loop?
- intialise control variable (determines whether execution continues) (a)
- while control variable doesn’t pass limiting value (B), loop continues to execute loop body
- loop body must include statement that alters loop control variable
When can you suspect you are dealing with an infinite loop?
- the same output is displayed repeatedly
- the screen remains idle for extended periods of time
How do you exit an infinite loop?
with Ctrl-C or Break
What coding errors can cause an infinite loop? (4)
- empty body caused by misplaced semicolons [while (a < B);]
- Boolean statement always evaluating to true [while (2 < 4)]
- not increasing control variable in loop body
- forgetting curly brackets for grouped statements in loop body
while (a < B)
Console.WriteLine(a);
a++;
Infinite loops are commonly used in …
… data input validation / testing.
When is a for loop commonly used?
Is used when a definite number of iterations is required.
Give the general syntax of a for loop.
for (initialise; evaluate; alter)
loop body;
e.g.:
for (int a = 1; a < 5; ++a)
Console.WriteLine(a);
Give common properties of a for loop. (4)
- variable can be declared outside the for loop
- a variable declared within a for loop is local, it goes out of scope outside of the loop
- you can leave a portion of the for loop empty: for(; j < 7; ++j)
- the same variable needs to turn up in all parts of the statement
What other tasks can you perform with a for loop? (5)
- can initialise more than one variable: for (i = 1, g = 4; i < 10; ++i)
- can perform more than one test: for (a = 0; a < 9 && u > 1; ++a)
- decrement instead: for (x = 0; x <= 9; –x)
- alter more than one variable: for (i = 1; i < 5; ++i, sum += k)
- can pause program by leaving out loop body: for (k = 1; k <= 10; ++k);
Why would you use a do-while loop?
A do-while loop the control variable at the bottow of the loop when one iteration has already occured. There will always be at least one iteration.