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