.NET Core
C#
, F#
, Visual Basic .NET
(from .NET Core 5.0
in the future).Relies on Package manager
to receive updates.
CoreCLR
- comparable to CLR
(Common Language Runtime). CoreCLR
is a complete runtime and virtual machine for managed execution of .NET
programs and includes just-in-time compiler RyuJIT
.CoreFX
- comparable to FCL
(Framework Class Library) foundation for Standard Libraries
. CoreFX
shares a subset of .NET Framework
API-s and comes with it’s own API-s that are not part of the .NET Framework
.Supports:
ASP.NET Core
web apps.NET 3.0
- WPF, Windows Forms.NET Framework
.NET
for building any type of desktop app that runs on Windows.FCL
(Framework Class Library).CLR
- application virtual machine.Relies on Windows Update
to receive updates.
Two main components:
CLR
(Common Language Runtime) - virtual machine that manages the execution of .NET
programs and includes just-in-time compiler RyuJIT
.FCL
(Framework Class Library) - provides user interface, data access, database connectivity, web application development, network communications. First implementation of CLI
(Common Language Infrastructure)..NET Core
command-line interface (CoreCLI) is a tool for developing, building, running and publishing .NET Core
applications.Command structure is:
dotnet
build
, publish
, new
, run
, test
, add
, remove
, etc.options
NuGet
packages and are run by the command prompt..NET Core SDK
.CLR
(Common Language Runtime) in .NET
.CIL
).MarshalByRefObject
.CLR
with the necessary information to build a .NET
application.Reflection
.Process
class provides access to local or remote processes.Process
class can start, stop, control and monitor local system processes.CLR
.System.Diagnostics
namespace.CLR
can be executed by ThreadPool
class.Thread
class creates and controls a thread, sets its priority and gets it started.System.Threading.Thread
namespace.receive
methods and properties from an existing class.NET framework
is managed and directly executed by CLR
(no matter the language).CLR
provides memory management, type safety, etc..NET framework
and do not run under the control of CLR
.VB
is an example of unmanaged code.C
/ C++
are unmanaged - they are loaded as binary to the memory by the operating system.C# 8.0
an interface can have a default implementation for members for classes / structs that don’t provide an override method.C# 8.0
an interface may include also explicit access modifiers (public is default) and static fields. Instance fields are not permitted in interfaces..NET
assembly (in any supported language).C#
compiler to the CIL
(intermediate language) which saves it in *.exe
or *.dll
files.CIL
code is generated, the CLR
converts that native code into machine code (using the JIT
compiler). JIT
compiler runs CIL
code from *.exe
or *.dll
file into native code which instantly executes by the processor.*.exe
.null
.null
reference (unless Nullable
is used).sealed
type.protected
or protected internal
.new
.using
directive.global::System
will always refer to the . NET System namespace.::
- to access a member of an aliased namespace.using
statement in C#?IDisposable
objects which provides a mechanism for releasing unmanaged resources.C# 8.0
ensures the correct use of IAsyncDisposable
objects which provides a mechanism for releasing unmanaged resources asynchronously.C#
exception handling is represented by classes that mainly derived from System.Exception
.try
, catch
, finally
blocks the core program is separated from the error-handling statement.Structure:
try
- a block that checks the statement and activates it. It is followed by one or more catch
blocks that handle it.catch
- exception handler that catches the problem at the exact place in the program. This is the place where we decide what to do with the exception.finally
- executes a set of statements no matter of an exception is caught or not.throw
- the program creates a new exception then the problem occurs and throws it. We can trow an object if it is derived from the System.Exception
class.
Exception
class.System.IO
namespace provides with methods that allow reading / writing to files and data streams, and some basic file and directory support.Directory
- has static methods for creating and moving directories and subdirectories. The class can not be inherited.File
- has static methods for creating, copying, deletion, moving and opening of a single file.IOException
- the exception that is thrown then an I/O error occurs.Path
- performs operations on String
instances that contain file or directory path information.TextReader
- reader that reads a sequential series of characters.TextWriter
- writer that can write a sequential series of characters. The class is abstract.StreamReader
- implements TextReader
that reads characters from a byte stream in a particular encoding.StreamWriter
- implements TextWriter
for writing characters to a byte stream in a particular encoding.FileStream
- reading from, doing some operation (writing to) and closing the file..NET Framework
.Struct
.object
type.System.Object
instance and stores it in the heap. int number = 7;
object obj = i;
obj = 123;
number = (int)obj;
Break
is used to jump out of the loop (exit).Continue
breaks one iteration of the loop and continues to execute with the next iteration.C#
destructor automatically implements the Finalize
method. The garbage collector is non-deterministic, you do not know precisely when the garbage collector performs finalization.Object
.null
.null
.Array
that implements IEnumerable
and IEnumerable<T>
For
, ForEach
, While
loops.System.Array
class includes method for creating, manipulating, searching and sorting arrays like Sort
, Clone
, Copy
, Equals
, Empty
, Find
, FindIndex
, Exists
, GetValue
, IndexOf
, etc. int[] singleArray = new int[5];
int[,] multiArray = new int[2, 3];
int[,] multiArray2 = new int { {1, 2, 3 }, {7, 8, 9}};
int[] [] jaggedArray = new int [6][];
jaggedArray[0] = new int[4]{1, 2, 3, 4, 5};
int[] [] jaggedArray = new int [3] [];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
string
is a object of type String
that is a sequence of characters - text.StringBuilder
.Length
property - number of Char
objects it contains, not the Unicode characters.String
class provides methods for creating, manipulating and comparing strings like String.Empty
, String.Substring
, etc.Enumerable
class because it implements IEnumerable<T>
.String.CompareTo
, String.Equals
(value), String.Equality
(value).Object.ReferenceEquals
- if the two strings are the same objects..Net Framework
.System.Text.RegularExpression
namespace.Regex.IsMatch
, Regex.Match
, Regex.Replace
, etc. string pattern = @"^[A-Z][a-zA-Z]*$";
string firstName = "Victor";
if(Regex.IsMatch(firstName, pattern){
Console.WriteLine("It's a match!");
}
Parse
and TryParse
) and DateTime objects. We can also parse strings that represent Char
, Boolean
and Enum
into data types.Casting is a type conversion that can be automatically supported by . NET Framework or custom type conversion.
Automatic type conversions:
Object
instance.Custom type conversions:
implicit
operator - widen conversions between typesexplicit
operator - narrow conversions between typesIConvertible
interface, Convert
class and TypeConverter
classSystem.Delegate
class.+
operator. public static void WriteToScreen(string str) {
Console.WriteLine("The String is: {0}", str);
}
public delegate void printString(string s); // declaration
printString firstString = new printString(WriteToScreen); // create delegate object
// declare a delegate type for the event
public delegate string BoilerLogHandler(string str);
class EventProgram {
// declare the event
event MyDel MyEvent;
public EventProgram() {
this.MyEvent += new MyDel(this.WelcomeUser);
}
public string WelcomeUser(string username) {
return "Welcome " + username;
}
static void Main(string[] args) {
EventProgram obj1 = new EventProgram();
string result = obj1.MyEvent("To Events");
Console.WriteLine(result);
}
}
GetType
method returns the type of any variable.Reflection
enables you to access them.System.Reflection
namespace.System.Collections.Generic
namespace are the most common generic-based collection methods. public class GenericList<T>
{
public void Add(T input) { }
}
async
and await
?TAP
(Task Asynchronous programming model) provides more abstraction to asynchronous programming.Task<TResult>
or Task
and are defined by the async
modifier.Async
).Await
is an expression that marks a point where the method can’t continue until the awaited asynchronous operation is finished.await
some process, the program continues to execute if that process is not in use. When the process is finished, the control resumes andDeadlock
?System.Threading.Monitor
), avoid unnecessary locks and avoid nested locks..NET
Interlocked
class to carry all the operations in one single operation..NET
has Wait Based Primitives - Monitor
, Mutex
, ReaderWriterLock
classes for that technique.JSON
or XML
stringcatch
blocks with try
statement but only the one that first matches the exception will be executed.catch
block is executed, the controls skip the other catch blocks and is transferred to finally
block.catch
blocks is important: from more specific exception to more general.public
- access is not restricted.private
- access is limited to the containing type.protected
- access is limited to the containing class or types derived from the containing type.internal
- access is limited to the current assembly.protected internal
- access is limited to the current assembly or types derived from the containing type.private protected
- access is limited to the containing class or type derived from the containing class within the current assembly.private
, allowed accessibility of members: allpublic
, allowed accessibility of members: nonepublic
, allowed accessibility of members: noneprivate
, allowed accessibility of members: public
, internal
, private
static
keyword.this
.void
.static
constructor without parameters or access modifier that will be invoked automatically when we create the first instance of the class. In other words, it will be invoked only once no matter how many class instances we create.Ref
is a keyword in C#
.Out
is a keyword in C#
.this
command within a static method?Object
.This
points to an instance of the current class. In a static method there is no inheritance. Hence, we can not use this
in a static method.C#
: bool
, byte
, char
, decimal
, double,
enum,
float
, int
, long
, sbyte
, short
, struct
, uint
, ulong,
ushort
.C#
are: string
, array
, class
, delegate
.null
.protected
in the base class.private
in the base class. The reason is they can not be accessed by the derived class. The solution is the virtual methods to have protected
access modifier.:
symbol.C#
supports single inheritance only..NET
type system inherit from Object
.sealed
.Not inherited from a class are: - static constructors - instance constructors - finalizers - called by the runtime’s garbage collector to destroy an instance
Object
class in the System
namespace.Object
is implicit.Object
is inherited by all objects - Equals
, ToString
, Finalize
, GetHashCode
.virtual
to declare it.virtual
.sealed override
in the derived class and prevent the method from further overriding in the following derived class.null
value. double? pi = null;
Nullable<T>.HasValue
.Nullable<T>.Value
gets the value of the underlying type if the HasValue
is true
.Is
operatortrue
if the object is the same and false
if not.As
operatornull
if not.This
keyword is used to define the indexer. class SampleCollection<T>
{
// Declare an array to store the data elements.
private T[] arr = new T[100];
// Define the indexer to allow client code to use [] notation.
public T this[int i]
{
get { return arr[i]; }
set { arr[i] = value; }
}
}
class Program
{
static void Main()
{
var stringCollection = new SampleCollection<string>();
stringCollection[0] = "Hello, World";
Console.WriteLine(stringCollection[0]);
}
}
throw
and throw ex
?throw ex
..Net
.Description
, DefaultValue
, Serializable
, DllImport
(exposed by an unmanaged dynamic-link library (DLL) as a static entry point), Conditional
(method call should be ignored unless a specified conditional compilation symbol is defined), AssemblyVersion
, Obsolete
(raise a warning about code that should no longer be used), etc.public sealed class Singleton
{
private static Singleton instance=null;
private Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance==null)
{
instance = new Singleton();
}
return instance;
}
}
}
public sealed class Singleton
{
private static Singleton instance = null;
private static readonly object padlock = new object();
Singleton()
{
}
public static Singleton Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
}
Finalize
vs Dispose
usage?protected
methods.Disposable
method, the object is made unusable.