C# Flashcards
The concept of organizing your code such that logic that follows a certain theme or have some dedicated functionality are grouped together
In a nutshell: Leveraging classes and other grouping mechanisms to group data and logic that belong with each other
We want to avoid having spaghetti code! We want lasagna!
This is the first step in writing readable, extendable, and maintainable code
Separation Of Concerns
Building blocks of your program
They are the blueprints to the actual objects that you process in your program
Besides being used to structure data, you also use classes to encapsulate logic and the data that go together.
Classes
Logical grouping of types that follow a certain theme of functionality.
To utilize the classes located in a different namespace, use the using keyword
Analogous to Java packages.
Note: If namespaces are the logical grouping of types, the physical grouping are called assemblies.
Namespace
a container for one or more related projects, along with build information, Visual Studio window settings, and any miscellaneous files that aren’t associated with a particular project.
Final packaging of your application
Solution
contains all files that are compiled into an executable, library, or website.
Those files can include source code, icons, images, data files, and so on.
contains compiler settings and other configuration files that might be needed by various services or components that your program communicates with.
Project
A typical C# program uses types from the class library and user-defined types that model the concepts that are specific to the program’s problem domain.
You use data types to structure the data that you process in your program. They’re very important in app design!
Setting a data type is also a form of validation that leverages the strong typing of C#.
Fun Fact! All types inherit from the System.Object base class. (Which is just a fancy way of saying all types are objects!)
Data Types
What are the two types of data types?
Value and Reference
What does CTS stand for?
Common Type System
What is CTS?
The CTS is a standard definition of the types in .NET compliant languages.
Analogous to java’s primitives
Types that derive from System.Value.
Stored in the stack and not the heap.
This means that when accessing the value of a variable set to a value type, you get the value directly and not a reference to where the value is stored in heap.
There’s no separate heap allocation or garbage collection overhead for value-type variables.
Two categories:
Structs – used to create custom value types
Enums - defines a set of named integral constants
Value Types
A type that is defined as a class, string, delegate, array, interface, or record is a reference type.
Record is a new reference type introduced in .NET 5/C#9
At run time, when you declare a variable of a this type, the variable contains the value null until you explicitly create an object by using the new operator or assign it an object that has been created elsewhere.
Reference Types
Where are Reference Types stored?
Note that reference types are stored in the heap. The stack holds the reference to a place in heap that contains the actual value of the object.
What is language interoperability?
Basically, in one solution, your projects can be written in multiple .NET compliant language.
You can have a project written in VB.NET and reference that same project in a project written in C#
What is .NET?
.NET is a free, open-source development platform for building many kinds of apps.
Development platform means it’s a collection of libraries and languages that you can use to develop applications as well as runtime implementations to run your applications on
What does SDK stand for? What does it do?
The software development kit (SDK) includes everything you need to build and run .NET Core applications, using command line tools and any editor (including Visual Studio).
what does the runtime do?
Theruntimeincludes just the resources required to run existing .NET Core applications. The runtime is included in the SDK.
What does BCL stand for? What does it do?
The base class library (BCL) provides classes and types that are helpful in performing day to day operation e.g. dealing with string and primitive types, database connection, IO operations.
What does CLR stand for? what does it do?
.NET provides a run-time environment, called the common language runtime, that runs the code and provides services that make the development process easier.
Some of those services include:
Memory management
JIT compilation
Exception handling support
Code whose execution is managed by a runtime.
The CLR is in charge of taking the managed code, compiling it into machine code and then executing it. On top of that, runtime provides several important services such as automatic memory management, security boundaries, type safety etc.
Managed Code
Code that is developed outside .NET is unmanaged code.
Code that isn’t managed by the CLR
Does not leverage CLR features such as memory management
Executed by the help of wrapper classes
Unmanaged Code
What is Garbage Collection?
CLR provides automatic memory management of your heap memory
When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.
Sometimes we utilize resources that are outside the scope of the CLR. We need to clean this up ourselves.
Luckily, there is the IDisposable interface with the dispose() method that can be used to clean up these external resources.
Types that utilize these unmanaged resources inherit from the IDisposable Interface
You can also use using blocks and statements for clean up.
What is a collection?
A collection is a data structure that can hold one or more items.
In C# there are two major types of collections: generic and non generic
All collections provide methods for adding, removing, or finding items in the collection.
What are Arrays?
The basic data structure to store a group of objects
A collection of objects of the same data type stored in a contiguous space in memory
Array types:
Single dimensional
Multidimensional
Jagged: Array of arrays
Note that array sizes are immutable!
Note that arrays belong in the System namespace and is not included in the hierarchy.
All collections that directly or indirectly implement the ICollection interface or the ICollection interface share these features:
The ability to enumerate the collection
The ability to copy the collection contents to an array
Capacity and Count properties
A consistent lower bound
The IEnumerable interface exposes an enumerator, which supports a simple iteration over a non-generic/generic collection
Collection Hierarchy
What are non-generic collections?
Non generic collections are used to store data when the size is unknown. This makes sure that you only take up the memory space you need!
Some common non generic collections: Arraylist Hashtable Stack Queue
Note that non generic collections store data as objects, this means that you can store multiple data types in one collection. To be able to have a homogenous data set, you’ll have to set up some type checking before storing in a non generic collection.
what are generic collections?
Leveraging generics you are able to have a type safe collection
Because you’re not casting things to an object, you’ll be able to save on overhead
Some common generic collections:
List
list of objects that can be accessed by index
An ever expanding array
Dictionary
Represents a collection of key/value pairs that are organized based on the key.
Stack
LIFO
Queue
FIFO
What is the root interface of the collections hierarchy?
ICollection Interface
What is the IEnumerable for?
The IEnumerable interface exposes an enumerator, which supports a simple iteration over a non-generic/generic collection
Arrays vs ArrayList vs List?
Arrays have the same data type (type safe) and a fixed size
ArrayList can have multiple data types and do not have a fixed size
List have the same data type (type safe) and do not have a fixed size
What are non-access modifiers?
AKA keywords, modifiers, etc.
They are used to modify the declaration of types and type members
Applied to: classes and class members
Enables you to create classes and class members that are incomplete and must be implemented in a derived class.
This keyword cannot be used with the static keyword
____ methods are:
Implicitly virtual (implicitly overridable)
Only declared in abstract classes
Have no method body (just a method signature)
Abstract
Applied to: fields
_____ fields and locals aren’t variables and may not be modified.
_____ can be numbers, Boolean values, strings, or a null reference.
Don’t create a ____ to represent information that you expect to change at any time.
Constant
Applied to: fields, structs, and struct members
______ fields:
indicates that assignment to the field can only occur as part of the declaration or in a constructor in the same class.
A ______ field can’t be assigned after the constructor exits
_____ struct:
indicates that the structure type is immutable.
Readonly
Whats the difference between the non-access modifiers: readonly and constant?
Const fields are initialized on declaration. The initialization of readonly fields can be accomplished at a later point.
A const field is a compile-time constant, the readonly field can be used for run-time constants.
Applied to: classes, class members
_____ classes cannot be inherited by other classes
______ methods and properties aren’t overridable in
any classes that inherit those members
Similar to the final keyword in java
Sealed
Applied to: classes and class members
Used to create class members which belongs to the type itself rather than to a specific object
This means that all instances of the type would have the share the same variable value for ___ fields
You wouldn’t need to instantiate an object of the type to access a static method
A ____ class can only have ____ members
Static
Applied to: methods, properties, indexers, events
Used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class.
By default, methods are non-_____. You cannot override a non-_____ method.
You cannot use the ____ modifier with the static, abstract, private, or override modifiers.
Virtual
Applied to: classes and class members
Used to partially declare a class or class member.
This keyword allows definition of a class/class member to be spread across multiple different files.
Partial
Applied to: methods, properties, indexers, events
Required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.
Override
Applied to: methods, properties, indexers, events
____ modifier (different from the ___ keyword!) is used to hide the base implementation of a class member. Instead of extending the virtual or abstract class members, ____ modifier creates a ____ class member with the same name and makes the base implementation inaccessible.
You cannot use this with override, as they are mutually exclusive.
New
What are modifiers? Are they only limited to access modifiers?
The modifiers include private, public, protected, internal, and two combinations: protected-internal and private-protected.
No, there are also non-access modifiers
What are non-access modifiers?
N.O.V.A.S S.P.R.C (novas spark)
New, Override, Virtual, Static, Sealed, Partial, Readonly, Constant, and Abstract
Where can I apply the sealed keyword to?
Applied to: classes, class members
How do I make something overridable? How do I override it?
use keyword override
ex public virtual void dog(){}
public override void dog(){}
What’s the difference between new and override?
The override modifier may be used on virtual methods and must be used on abstract methods. This indicates for the compiler to use the last defined implementation of a method. Even if the method is called on a reference to the base class it will use the implementation overriding it.
The new modifier instructs the compiler to use your child class implementation instead of the parent class implementation. Any code that is not referencing your class but the parent class will use the parent class implementation.