MSDN Documentation

C# and .NET Core

Introduction to C# and .NET Core

.NET Core is a free, cross-platform, open-source framework for building many different types of applications, including web, mobile, desktop, and cloud applications. C# is a modern, object-oriented programming language that runs on .NET. Together, they offer a powerful and flexible platform for developers.

Getting Started with .NET Core

To begin your journey with .NET Core, you'll need to install the .NET SDK. This includes the .NET runtime and the C# compiler.

  1. Download and Install: Visit the official .NET download page (dotnet.microsoft.com/download) and download the latest LTS or Current version for your operating system.
  2. Verify Installation: Open your terminal or command prompt and run dotnet --version to confirm the installation.
  3. Create Your First Application: Use the .NET CLI to create a new project:
    dotnet new console -o MyFirstApp
    cd MyFirstApp
    dotnet run
Note: .NET Core has evolved into just ".NET". Starting with .NET 5, Microsoft unified the .NET ecosystem under a single name. This documentation still refers to .NET Core for historical context and clarity when discussing earlier versions, but modern development uses ".NET".

Core Concepts

C# Language Features

C# is a versatile language with a rich set of features:

  • Object-Oriented Programming: Classes, objects, inheritance, polymorphism.
  • Type Safety: Strong typing helps catch errors at compile time.
  • Asynchronous Programming: async and await keywords for efficient I/O operations.
  • LINQ (Language Integrated Query): A powerful way to query data sources directly within the language.
  • Generics: Create reusable components that work with any type.
  • Pattern Matching: Advanced pattern matching for cleaner code.

The .NET Runtime

The .NET runtime (CoreCLR) is responsible for executing your C# code. Key aspects include:

  • Just-In-Time (JIT) Compilation: Code is compiled to native machine code at runtime.
  • Garbage Collection: Automatic memory management.
  • Cross-Platform Compatibility: Run your applications on Windows, macOS, and Linux.

Base Class Library (BCL) and Framework Libraries

The .NET ecosystem provides a vast set of libraries for common tasks:

  • System: Fundamental types and core functionalities.
  • System.Collections: Data structures like lists, dictionaries, and arrays.
  • System.IO: File system operations, stream handling.
  • System.Net: Networking capabilities.
  • ASP.NET Core: For building web applications and APIs.
  • Entity Framework Core: An Object-Relational Mapper (ORM) for database access.

Building Applications with .NET Core

You can build a wide variety of applications:

  • Console Applications: Simple command-line tools.
  • Web Applications: Using ASP.NET Core MVC or Razor Pages.
  • Web APIs: For building RESTful services.
  • Desktop Applications: Using WPF or WinForms (Windows) or MAUI (cross-platform).
  • Cloud-Native Applications: Optimized for microservices and containers.
Tip: Explore the various project templates available with dotnet new --list to quickly scaffold different application types.

Best Practices for C# and .NET Core Development

  • Follow naming conventions (PascalCase for types and public members, camelCase for local variables and parameters).
  • Write clean, readable, and maintainable code.
  • Leverage asynchronous programming for I/O-bound operations to improve responsiveness.
  • Use dependency injection for better testability and modularity.
  • Implement robust error handling and logging.
  • Stay updated with the latest .NET and C# features.
Important: For the most up-to-date information and detailed API references, please refer to the official Microsoft Learn documentation.