are you trying to decide whether you want to learn c plus or c sharp well let me walk you
through some of the differences in the next few minutes hi my name is chuck mccullough and let's
dive in one of the biggest differences between c plus plus and c sharp is in how code is organized
among source files in the case of c plus code is split between what we call header files
and implementation files the reason for that is that each source file compiles in isolation
without any awareness of any other code the header files contain declarations not necessarily
lines of code or algorithms but the declaration so that the compiler can successfully match up
your function calls with the function being called in the case of c sharp we have one stop shopping
declarations and implementation are in a single location in a single file the c-sharp compiler
can see the content of other modules that have already been compiled because the c-sharp
compiler includes a lot of metadata in those compiled files now some of the basics of c plus
plus versus c sharp they are both strongly typed c plus plus however is very static in the sense that
the compiler strips away most of the original source code when it produces the machine code
if you were to decompile or reverse engineer the compiled code you probably wouldn't end
up with something that looked too much like the original source code especially with an
optimizing compiler that means that there's a limited ability to dynamically load and execute
code in c plus that's what we mean by static also c plus is a value based language c sharp on the
other hand also strongly typed but it's dynamic when the c sharp compiler converts
source code it leaves a lot of metadata the method names the class names the parameter names
in that compiled code that means that we can through something called reflection
dynamically discover and generate even and execute code that was dynamically created
and loaded into memory also c sharp is a reference based language now the execution environment in c
plus the source code is converted into machine code as we mentioned that machine code executes
directly on your hardware and the compiler oftentimes optimizes for a specific cpu
the programmer really needs to have a pretty good understanding of what computer programs look like
when they're running on the actual machine c-sharp on the other hand is converted into
a machine code but not for a physical piece of hardware for an intermediate machine something
we call a virtual machine this particular virtual machine is known as the common language runtime
or clr it is helpful still for the programmer to understand computer architecture and c-sharp is
supervising or managing the execution so you don't have to be quite as worried about a mistake that
would cause a catastrophic result in c plus as an object-oriented language we can declare
new data types using the keyword struct or the keyword class we tend to use the keyword struct
for defining data types whereas we use the keyword class for the object oriented class concept c
plus also allows us to create enumerated types there are many ways in c sharp to define new types
struct and class just like c plus but these are different in c-sharp a struct is used to define
a value-based data type similar to types in c plus while class is used to create the typical c-sharp
reference type c sharp also supports interfaces which are not directly supported in c
plus we just use the keyword class there c sharp also supports enums and starting in c
sharp version 9 there's a new type specifier called record that's very convenient for
creating data transfer objects for defining simple aggregates that are strongly typed still
with a minimum syntax to use as parameters and return types from functions a very key difference
is that c plus programmers will allocate memory with new but the programmer must explicitly
free that memory with delete now in c plus plus in modern c plus the usage of smart pointers and
resource acquisition is initialization or raii alleviate a lot of the explicit memory management
as well removes a very troublesome aspect from most of our source code and that is called raw
pointers raw pointers give programmers a lot of trouble and so in modern c plus you'll still
have pointers and you still have to do memory management but not nearly as much as you had to in
the old days now the c sharp the programmer still dynamically allocates memory with the keyword new
but garbage collector will free that memory for them the good thing is we don't have to
worry so much about memory leaks the downside is that the programmer has no control over
garbage collection so if it isn't quite as immediate as you had hoped you have no way to
change that behavior both c plus and c sharp support generic programming
in c plus plus it's known as a template and templates generate a new class for every variant
of that template a first class type the result of that is something we call invariance now
there's also in programming something known as covariant and contravariant beyond the scope of
this discussion and c plus supports those concepts through the use of conversion operators in c-sharp
generics are known as generics essentially the same syntax as templates in c plus plus and they
are invariant by default as c sharp also generates a new type for each generic variant that is
specified in code much the same as c plus c sharp however has keywords that can be used to control
whether it is invariant covariant or contravariant both languages also support functional programming
in c plus basically inherited from the c programming language a variable can hold a
pointer to a function that can be used to call the function indirectly and starting in c plus version
11 we can specify lambda expressions to generate anonymous functions or functor classes on the fly
as inline code c-sharp formalized the concept of a function pointer a little bit over c plus plus
with something known as a delegate and in c-sharp we also have lambda expressions that
can be used to generate anonymous delegates as inline code both c plus and c sharp are
object oriented programming languages or at least support object orientation and therefore support
inheritance c plus plus supports multiple inheritance meaning that a class can have
multiple distinct base classes now most project leads and architects would bristle at the idea
of multiple inheritance so even in c plus we tend to try to limit the inheritance to a single class
and use the multiple inheritance to implement multiple interfaces because there's not a specific
interface syntax syntactically it looks like multiple inheritance but we'll assume that all of
the classes that we quote unquote inherit from are actually interfaces and not classes polymorphism
is implemented essentially at compile time through the use the clever use of function pointers
so that at runtime we're only one extra assembly language or a couple of extra assembly language
statements away from actually making the same call that you would make statically in a
non-polymorphic environment now charp supports only a single inheritance and the root of all
of the classes in c-sharp is an object type it does have a specific syntax for interfaces and a
c-sharp class can implement multiple interfaces polymorphism in c-sharp is accomplished through
dynamic binding by the runtime mechanism so it's a little different mechanism but again performance
is still pretty good now the libraries for c plus plus and c sharp are quite distinctly different c
plus supports the basic stuff that all programs would basically need dates and times collections
utilities and as of version 11 threads and tasks as well as i o the c plus library is pretty rich
but doesn't include by default things like user interface libraries and other specialized
libraries they are available but just not part of the standard library in c sharp you get everything
that's in the c plus plus standard library plus several different gui apis for developing desktop
applications as well as a framework asp.net for web development and a whole connectivity a whole
api for database connectivity including an object relational mapper known as entity framework much
broader more encompassing library in.net for c sharp now certainly c plus has a longer learning
curve and a successful c plus developer will need to have some understanding of how software
executes on the hardware in exchange for this though you will have blazing fast performance
c plus is best for computationally intensive applications like simulations and games
as well as embedded systems like in automotive or telecommunications c-sharp on the other hand
was designed to be easier to learn than c plus so clearly it has a shorter learning curve
however it is a much larger language with a vastly larger library c-sharp is better
suited for applications with user interfaces like web applications and desktop applications
hey i'm chuck mccullough and i hope you enjoyed this presentation check out our
other videos in this channel as well as our other courses listed in the description below