Skip to content

1.1 Fundamentals

1. Visual Studio Project Types

When working with Visual Studio 2022, you’ll notice that you can create different types of Console Applications. The two main options are:

1. Console App (.NET Framework) 2. Console App

1.1 Console App (.NET Framework)

  • Target Framework: This type of project targets the .NET Framework, which is a Windows-specific framework that has been around since the early 2000s. It’s primarily used for building Windows desktop applications, Windows services, and web applications using ASP.NET Web Forms or ASP.NET MVC.

  • Compatibility: The .NET Framework is only compatible with Windows. It is not cross-platform, meaning it won’t run natively on macOS, Linux, or other non-Windows operating systems.

  • Libraries: You have access to the full suite of .NET Framework libraries, which include Windows-specific APIs like WPF, Windows Forms, and many legacy libraries that may not be available or fully supported in .NET Core or .NET 5/6/7/8+.

  • Project File Format: The project file (.csproj) in .NET Framework projects is often more complex and uses an older format compared to .NET Core/.NET projects.

  • Deployment: Applications built with .NET Framework typically require the target .NET Framework version to be installed on the machine where the application is running.

1.2 Console App (aka .NET 5, .NET 6, .NET 7, .NET 8, etc.)

  • Target Framework: This type of project targets the .NET runtime (previously known as .NET Core), which is a modern, cross-platform, open-source framework. Starting from .NET 5, the name was unified to just ”.NET” (e.g., .NET 5, .NET 6, .NET 7, .NET 8).

  • Cross-Platform: Applications built using this template can run on Windows, macOS, and Linux. This is one of the key benefits of the .NET runtime compared to the .NET Framework.

  • Performance: .NET applications tend to have better performance and smaller deployment footprints compared to .NET Framework applications due to the optimizations in the runtime and the ability to deploy as self-contained applications.

  • Modern APIs: The .NET runtime is actively developed, and new features and APIs are added with each release. It also includes modern libraries and supports the latest C# language features more rapidly than the .NET Framework.

  • Project File Format: The project file (.csproj) in .NET projects is simpler and uses a more streamlined, SDK-style format. This format is easier to read and manage, especially in source control systems.

  • Deployment: You can create self-contained deployments that include the .NET runtime, so the target machine doesn’t need to have the runtime installed. This is in contrast to the .NET Framework, where the correct version of the framework must be installed on the target machine.

1.3 Key Differences

1. Platform Support:

  • .NET Framework Console App: Windows-only.
  • .NET Console App: Cross-platform (Windows, macOS, Linux).

2. Development and Deployment:

  • .NET Framework: Requires the specific version of .NET Framework to be installed on the target machine.
  • .NET: Can be deployed as a self-contained application, bundling the runtime with the app.

3. Project Structure:

  • .NET Framework: Uses an older, more complex project file format.
  • .NET: Uses a simpler, SDK-style project file.

4. API Availability:

  • .NET Framework: Includes legacy APIs and Windows-specific libraries.
  • .NET: Focuses on modern APIs and cross-platform development.

5. Performance:

  • .NET Framework: May have higher memory and performance overhead compared to .NET.
  • .NET: Generally more optimized for performance and has better support for modern hardware.

1.4 Which One Should You Choose?

  • Choose Console App (.NET Framework) if**:

    • You need to use libraries or APIs that are only available in the .NET Framework.
    • You’re maintaining or extending a legacy application that was built on the .NET Framework.
  • Choose Console App (.NET) if**:

    • You want to build cross-platform applications.
    • You need to use the latest features of C# and .NET.
    • You are starting a new project and want to take advantage of the modern, high-performance features of the .NET runtime.
    • You prefer a simpler and more modern project structure and tooling.

2. Creating a DLL (Dynamic Link Library)

Play

Create a DLL

A DLL (Dynamic Link Library) is a file that contains code and data that can be used by multiple programs simultaneously. It helps in modularizing applications and reduces memory and disk space usage since the code can be shared. DLLs are commonly used in the Windows operating system for providing reusable functions, procedures, and data structures.

Certainly! Let’s go through an example of creating a simple DLL in C# using Visual Studio.

2.1 Example

We’ll create a DLL that contains a class MathLibrary with a method Add that adds two integers.

1. Create a New Project

  • Open Visual Studio.
  • Go to File > New > Project....
  • In the “Create a new project” dialog, search for "Class Library (.Net Framework)" and select “Class Library” under the C# language.
  • Name the project MathLibrary and choose a location. Click Create.

2. Write the Code

  • Visual Studio will create a default class file named Class1.cs. Rename this file to MathLibrary.cs.

  • Modify the contents of the MathLibrary.cs file:

    using System;
    namespace MathLibrary
    {
    public class MathOperations
    {
    public int Add(int a, int b)
    {
    return a + b;
    }
    public int Subtract(int a, int b)
    {
    return a - b;
    }
    }
    }
  • This example defines a class MathOperations within the MathLibrary namespace. The class has two methods: Add and Subtract.

3. Build the DLL

  • Save your changes.
  • Go to Build > Build Solution (or press Ctrl+Shift+B). Visual Studio will compile the code and generate a DLL file.
  • The DLL will be located in the bin\Debug\net6.0 (or similar) directory within your project folder, depending on the .NET version you’re targeting.

4. Using the DLL in Another Project

To use this DLL in another project:

  • Create a new Console Application (or any other type of application):

    • Go to File > New > Project....
    • Select “Console App” under C# and name it MathClient.
    • Click Create.
  • Add a Reference to the DLL:

    • In the Solution Explorer, right-click on the MathClient project and select Add > Reference....
    • In the Reference Manager, select Browse on the left side, then click the Browse... button.
    • Navigate to the MathLibrary.dll file you created, typically found in the bin\Debug\net6.0 folder of the MathLibrary project. Select the DLL and click Add.
  • Use the DLL in the Console Application:

    • In the Program.cs file of the MathClient project, add the following code:
    using System;
    using MathLibrary;
    namespace MathClient
    {
    class Program
    {
    static void Main(string[] args)
    {
    MathOperations math = new MathOperations();
    int sum = math.Add(5, 3);
    int difference = math.Subtract(5, 3);
    Console.WriteLine($"Sum: {sum}");
    Console.WriteLine($"Difference: {difference}");
    }
    }
    }
  • This code creates an instance of the MathOperations class from the MathLibrary DLL and calls its methods.

5. Run the Application

  • Set the MathClient project as the startup project by right-clicking on it in Solution Explorer and selecting Set as StartUp Project.

  • Run the application (press F5). You should see the output:

    Sum: 8
    Difference: 2