Introduction to C# and differences between C++ and C#

C# is a type-safe object-oriented language that enables users to build robust applications in the .Net ecosystem.

When you start learning C#, you can see that it is very easy to learn and made simple. C# supports the concepts of object-oriented programming like encapsulation, inheritance, and polymorphism.

C# is object and component oriented programming language.

Features worth noting

  • Garbage collection: As you can understand by the word, it means that it automatically reclaims memory occupied by unreachable unused objects.
  • Exception Handling: Provides an extensible approach to error detection and recovery
  • Lambda Expressions: It is a functional programming technique which I will discuss in the later topics.
  • Query Syntax: We can create a common pattern for working with data from any source
  • Async operations: Provides syntax for building distributed systems.
  • Pattern matching: Provides syntax to separate data from algorithms in modern distributed systems.
  • Unified type system: All the primitive types like int and double inherit from a single root ‘object’ type.
How to create a C# project in Visual studio?

I will explain step by step how one can create a visual studio C# project. If you have not installed the environment yet view this article which is a pre-requisite to try out programs on your machine.

  1. Open visual studio 2019 (or any other version you may have)

Go to File->New->project.

File new project

2. Create a new C# project

Select C# and you can view a list of different types of projects you can create. I will choose Console app.

Create a new project

Give the project name and browse for the location you want to store the project in. Hit ‘Create’.

3. Hello World program

The visual studio will have the following code after you create it. You can run and see “Hello World” on the console.

using System;

namespace TelestreakTutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
Key differences between C++ and C#

This is a popular interview question.

C# C++
Managed CodeUnmanaged code
compiles to Intermediate language that conforms to CLI compiles to machine code
Does not support multiple inheritancesSupports multiple inheritances
members of struct and class are private by default. Members of struct are public and class are private.
Difference between C++ and C#

Conclusion

I introduced a bit about the language and the differences between C++ and C#. Let us delve into the beauty of C#.

You can try the hello world program in the browser, Click here.

References

  1. Microsoft Library on C# and .Net

Prev: Installation

Next: Variables and built-in types