MT1 Flashcards
(43 cards)
Purpose of top-level statements?
write smaller programs without the use of the main method
How to write top-level statements
- “using” directives (optional)
ex. using System; using System.Collections.Generic;
(compiler automatically adds set of “using” directives” based on project type) - Statements (what we would write in main method)
ex. Console.Writeline… - More type / namespace declarations (optional)
- used when we have user-defined classes etc.
What happens to a top-level statement?
compiler will put statments (not written in main method) into an auto-generated method / class
if methods are used to write top-level statements, then they become local functions
at most one file in C# project can have top-level statements
What makes software high quality?
- correct (safe from bugs)
- comprehensible (readable, understandable)
- changeable (ready to change)
What is a type
set of values (data) and the operations permitted on the values
Two naming conventions
PascalCasing –> class + method
camelCasing –> local variables + fields
What is a constructor?
method used to initialize objects whenever an instance of the class (both have same name) is created
ex. when class Date is instantiated, constructor Date will set up an int type to create the numerical value of said Date
What does “private” mean
protects types from being manipulated outside of the class it is in.
Value vs Reference type
Value types –> need 1 allocated memory space to store data
ex. int x = 5;
Reference type –> stores reference to some other memory location where actual data is stored
ex string str = “hello” (str is immutable because it only references a memory locattion)
Explain the concept of Stack and Heap
Stack and Heap –> regions of memory to store data
Stack–> stores value types + reference type (only the reference), LIFO data structure
Heap –> stores actual data of reference type
What is a class?
type that stores logically related data + functions
a way to create + define new types and a set of values + operations associated with that type
contains:
data members –> variables
function members –> operations
What is a Class: Field?
stores data
a variable (of any type) that is a member of a class
PascalCasing for static public + protected fields
otherwise camelCasing
What is a Class: property?
member that represents data
(looks like a field but it does not necessarily allocate memory for data)
name set of two methods (accessors):
set + get
What is a method?
Block of code that can be executed / invoked
( do not use void to indicate that method doesn’t take parameters; simply use () )
What are local variables
holds temporary data within scope of a method (doesn’t exist outside of a method)
Difference between Private and Public Access Modifiers
Private members –> accessible within class in which they are declared (default access level)
Public members –> accessible to other objects in program
What does the following code do?
Console.WriteLine(message + “ “ + name)
Appends the data for message, appends a space, and appends the data for name
What happens to the string if a code below is compiled:
Console.WriteLine($”{message1.ToUpper()}”);
prints in ALL CAPS
BUT
b/c strings are immutable, a new string data is created that has the same string but modified.
What is String Builder
provides a mutable alternative to string
(uses namespace: System.Text)
ToString –> convert to string
Append –> adds value
Clear , Remove, Insert etc.
What is encapsulation?
information hiding
ex. declaring fields to be private
advantage: hiding details allows them to be changed later without changing the entire system
What is Inheritance?
types can be derived from other types (types can be subtypes)
ex. all types derived from System.Object type
Polymorphism
ability of object to take on many forms
ex. (supertype can refer to subtype object)
What is method overloading?
allows us to create multiple implementations of method with same name
ex.
public int Add(int a, int b, int c)
{
return a + b + c
}
public int Add(int a, int b)
{
return a + b
}
What is an array?
set of uniform data elements represented by a single name
- size cannot be changed
int[] array = {1, 2, 3} ;
char [] array = “string”