Unit 1: Introduction to .NET and C# - Subjective Questions
CSE253 — .Net Programming • Practice Questions with Detailed Answers
20 questions
Define .NET and explain its major features.
.NET is a free, open-source, cross-platform development platform created by Microsoft for building desktop, web, mobile, cloud, IoT, and service-oriented applications.
Major features of .NET include:
- Cross-platform support: Applications can run on Windows, Linux, and macOS.
- Multiple language support: Languages such as C#, F#, and Visual Basic can be used.
- Common runtime: The Common Language Runtime manages execution, memory, exceptions, and security.
- Large class library: The .NET Class Library provides reusable APIs for files, collections, networking, databases, XML, and web development.
- Automatic memory management: Garbage collection automatically releases unused memory.
- Type safety: The runtime and compiler help prevent invalid type operations.
- Interoperability: Applications can communicate with native libraries, databases, web services, and other platforms.
- Modern development tools: Visual Studio, Visual Studio Code, and the .NET CLI support development, debugging, testing, and deployment.
Explain the evolution from the .NET Framework to .NET Core and the modern .NET platform.
The evolution of .NET occurred in the following stages:
- .NET Framework: The original .NET implementation, primarily designed for Windows. It supports technologies such as Windows Forms, WPF, ASP.NET, and Windows-specific APIs.
- .NET Core: A redesigned, modular, open-source, and cross-platform implementation. It was created to support Windows, Linux, and macOS and provided improved performance and side-by-side versioning.
- Modern .NET: Starting with .NET 5, Microsoft unified the .NET Core and modern .NET technologies under the name .NET. Later versions, such as .NET 6, .NET 7, .NET 8, and later releases, continue this unified platform.
The modern .NET platform provides cross-platform execution, high performance, a unified SDK, a common runtime, modern application frameworks, and support for cloud-native development. The .NET Framework remains useful for maintaining older Windows applications, while new applications generally use modern .NET.
Distinguish between the .NET SDK and the .NET Runtime.
The .NET SDK and .NET Runtime serve different purposes:
| Feature | .NET SDK | .NET Runtime |
|---|---|---|
| Purpose | Used to create, build, test, and publish applications | Used to run already-built applications |
| Includes | Compilers, CLI tools, templates, MSBuild, and runtimes | CLR and libraries required for execution |
| Required by | Developers and build servers | End users and production machines running applications |
| Common commands | dotnet new, dotnet build, dotnet test, dotnet publish |
Commonly used indirectly by dotnet application.dll |
The SDK contains the runtime, so installing the SDK is normally sufficient for development. A machine that only needs to execute an application may require only the appropriate runtime. Different application types, such as ASP.NET Core applications, may require a specific runtime package.
Describe the Common Language Runtime (CLR) and explain its responsibilities in a .NET application.
The Common Language Runtime (CLR) is the execution environment for .NET applications. It converts and manages the execution of Intermediate Language code produced by .NET languages.
Important responsibilities of the CLR include:
- Code execution: It uses Just-In-Time compilation to convert Intermediate Language into machine code.
- Memory management: It allocates memory for objects and uses garbage collection to reclaim unused memory.
- Exception handling: It provides a consistent mechanism for detecting and handling runtime errors.
- Type safety: It verifies that code uses types correctly and securely.
- Thread management: It supports creation, scheduling, synchronization, and management of threads.
- Security: It applies runtime checks and access rules.
- Interoperability: It allows managed code to interact with unmanaged code when required.
- Debugging and profiling: It provides services that help development tools inspect program execution.
What is the .NET Class Library? Explain its importance with suitable examples.
The .NET Class Library is a collection of reusable classes, interfaces, structures, enumerations, and APIs supplied by .NET. It provides standard functionality so that developers do not need to implement common operations from scratch.
Examples include:
System.Stringfor text processing.System.Collections.Generic.List<T>for strongly typed collections.System.IO.FileandSystem.IO.Directoryfor file and directory operations.System.Net.Http.HttpClientfor HTTP communication.System.Threading.Tasks.Taskfor asynchronous programming.System.Text.Jsonfor JSON serialization and deserialization.System.DataAPIs for database-related operations.
The class library improves productivity, reliability, portability, and maintainability. It follows common design conventions and integrates closely with the CLR and language features of C#.
Describe the steps for setting up a .NET development environment using Visual Studio.
The usual steps for setting up Visual Studio for .NET development are:
- Download and install a supported edition of Visual Studio.
- During installation, select the required workload, such as ASP.NET and web development, .NET desktop development, or .NET Multi-platform App UI development.
- Ensure that the required .NET SDK and runtime components are selected.
- Launch Visual Studio and sign in if required.
- Select Create a new project.
- Choose a project template, such as Console App, Class Library, ASP.NET Core Web App, or Web API.
- Select the target framework and provide the project name and location.
- Create the project and inspect the generated files.
- Use the Build, Run, and Debug commands to compile and execute the application.
- Configure breakpoints, inspect variables, and view errors through the Error List and Output windows.
Explain how to set up a .NET development environment using Visual Studio Code.
Visual Studio Code is a lightweight editor that can be configured for .NET development as follows:
- Install the appropriate .NET SDK from the official .NET distribution source.
- Install Visual Studio Code.
- Install the C# Dev Kit extension and any supporting C# extension recommended by the editor.
- Open the integrated terminal and verify the installation with
dotnet --info. - Create or open a project directory.
- Create a project using a command such as
dotnet new console. - Open the directory in Visual Studio Code with
code .. - Restore dependencies and build the project using
dotnet restoreanddotnet build. - Run the application using
dotnet run. - Configure breakpoints and use the Run and Debug panel to debug the program.
Visual Studio Code provides editing, IntelliSense, project navigation, integrated terminal support, source control integration, and debugging through extensions.
What is the .NET CLI? Explain commonly used .NET CLI commands.
The .NET Command-Line Interface (CLI) is a cross-platform toolset for creating, building, running, testing, publishing, and managing .NET applications from a terminal.
Common commands include:
dotnet --info: Displays installed SDKs, runtimes, and environment information.dotnet --version: Displays the active SDK version.dotnet new: Creates a project or solution from a template.dotnet restore: Downloads and resolves project dependencies.dotnet build: Compiles the project.dotnet run: Builds and runs the project.dotnet test: Builds and executes tests.dotnet clean: Removes generated build outputs.dotnet publish: Produces deployment-ready files.dotnet add package: Adds a NuGet package to a project.dotnet sln: Creates or modifies a solution file.
The CLI is useful for automation, continuous integration, remote development, and consistent workflows across operating systems.
Describe the typical project structure of a .NET application.
A typical .NET application contains the following files and directories:
- Solution file (
.sln): Groups one or more related projects. - Project file (
.csproj): Defines the target framework, project properties, package references, and build settings. - Source files (
.cs): Contain classes, methods, program logic, and application configuration code. Program.cs: Usually contains the application entry point and startup configuration.bindirectory: Contains compiled binaries and runtime output. It is generated during builds.objdirectory: Contains intermediate build files and generated metadata.Propertiesdirectory: May contain launch settings and other project-specific settings.- Configuration files: Examples include
appsettings.jsonfor application settings and environment-specific configuration. - Test projects: Separate projects commonly contain unit and integration tests.
The project file is central because the .NET SDK uses it to determine how the application should be restored, compiled, tested, and published.
Explain NuGet and the role of the NuGet Package Manager in .NET development.
NuGet is the package management system for .NET. A NuGet package is a distributable unit, usually containing compiled assemblies, metadata, documentation, and sometimes source or build assets.
The NuGet Package Manager helps developers to:
- Search for and install third-party or Microsoft libraries.
- Specify package dependencies in a project file.
- Resolve transitive dependencies automatically.
- Restore packages when a project is cloned or built on another machine.
- Update or remove packages.
- Lock or control package versions for reproducible builds.
Packages can be managed through Visual Studio, the dotnet CLI, or the Package Manager Console. For example, dotnet add package Newtonsoft.Json adds a JSON library to a project. Package references are recorded in the project file, while restored package contents are usually stored in a local NuGet cache.
Explain the complete process of building and executing a .NET application.
The process of building and executing a .NET application generally follows these stages:
- Source development: The programmer writes C# source code.
- Restore: The SDK reads the project file and obtains required NuGet dependencies.
- Compilation: The C# compiler checks syntax and types and converts source code into Intermediate Language.
- Assembly generation: The compiled code and metadata are placed into an assembly such as a
.dllor executable file. - Execution: The CLR loads the assembly and prepares methods for execution using Just-In-Time compilation.
- Runtime services: The CLR manages memory, exceptions, threads, and type safety while the program runs.
- Output: The application produces console output, web responses, files, or other results.
Common commands are dotnet restore, dotnet build, dotnet run, and dotnet publish. The publish command creates files intended for deployment and can produce framework-dependent or self-contained output.
Introduce C# and explain its important characteristics as a programming language.
C# is a modern, general-purpose programming language developed by Microsoft for the .NET platform. It is widely used for web applications, desktop software, cloud services, games, mobile applications, and enterprise systems.
Important characteristics of C# include:
- Object-oriented: It supports classes, objects, inheritance, encapsulation, abstraction, and polymorphism.
- Strongly typed: Variables and expressions have defined types that are checked by the compiler.
- Type-safe: The language prevents many invalid operations and unsafe conversions.
- Component-oriented: Programs can be organized into reusable components and assemblies.
- Managed: C# applications normally run under CLR services such as garbage collection and exception handling.
- Modern syntax: It supports generics, properties, delegates, events, pattern matching, nullable reference types, and asynchronous programming.
- Cross-platform: Modern C# applications can run on all platforms supported by .NET.
- Extensible: It can use .NET libraries, NuGet packages, native code, and web services.
Explain the basic structure of a C# program with a suitable example.
A basic C# program consists of statements, types, methods, and an entry point. Modern C# supports top-level statements, but a traditional structure can make the main components clearer.
Example:
using System;
namespace SampleApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, .NET");
}
}
}The components are:
using System;: Imports the namespace containingConsole.- Namespace: Organizes related types and prevents naming conflicts.
- Class: Defines a type named
Program. Mainmethod: Acts as the entry point for a traditional console application.static: Allows the method to be called without creating aProgramobject.string[] args: Receives command-line arguments.- Statement:
Console.WriteLinedisplays output. - Braces and semicolons: Define blocks and terminate statements.
Compare a traditional C# console application structure with a top-level statements structure.
A traditional C# console application explicitly declares a namespace, class, and Main method. A top-level statements application places executable statements directly in the source file.
Traditional form:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello");
}
}Top-level form:
using System;
Console.WriteLine("Hello");The main differences are:
- Amount of code: Top-level statements require less boilerplate.
- Entry point: The compiler generates the entry point for top-level code.
- Learning and small programs: Top-level statements are convenient for simple applications.
- Large applications: Explicit classes and methods often provide clearer organization.
- Restrictions: Top-level statements must appear before type declarations in a file.
Both forms are compiled into .NET assemblies and executed by the CLR.
Explain the relationship among source code, Intermediate Language, assemblies, and machine code in .NET.
The execution pipeline in .NET involves several forms of code:
- Source code: The developer writes C# code in
.csfiles. - Compilation: The C# compiler analyzes the source and converts it into Common Intermediate Language, also called Intermediate Language or IL.
- Assembly: IL, metadata, and related resources are packaged into an assembly, commonly a
.dllor.exefile. - Loading: The CLR loads the assembly and verifies its metadata and types.
- JIT compilation: The Just-In-Time compiler converts IL methods into native machine code for the current processor.
- Execution: The processor executes the native code while the CLR supplies runtime services.
This design allows the same assembly to run on different operating systems and processor architectures when a compatible .NET runtime is available. It also supports language interoperability because different .NET languages can compile to compatible IL and metadata.
Describe the steps for creating, running, and publishing a console application using the .NET CLI.
A console application can be created and managed with the following commands:
- Create a directory and move into it.
- Run
dotnet new console -n DemoAppto create a console project namedDemoApp. - Move into the project directory with
cd DemoApp. - Inspect or edit
Program.cs. - Restore dependencies with
dotnet restoreif necessary. - Compile the application using
dotnet build. - Execute it using
dotnet run. - Run automated tests, if available, with
dotnet test. - Create deployment output with
dotnet publish -c Release.
The generated project contains a .csproj file and source code. The build output is normally placed under bin, while intermediate files are stored under obj. Publishing creates files that can be copied to a target machine. The deployment style can be framework-dependent, requiring a runtime, or self-contained, including the runtime for a selected operating system and architecture.
Explain the purpose of a solution file and a project file in .NET applications.
A project file and a solution file have different roles.
The project file, usually ending in .csproj, defines an individual .NET project. It can specify:
- The target framework.
- Output type and build settings.
- Package references.
- Project-to-project references.
- Compilation and publishing properties.
- Application and assembly metadata.
The solution file, usually ending in .sln, groups related projects. It can contain an application project, class libraries, test projects, and supporting tools. It helps an IDE load and manage the complete system.
A solution is useful for organizing a multi-project application, while the project file is sufficient for building a single project. The CLI can manage them with commands such as dotnet new sln, dotnet sln add, and dotnet build.
Compare Visual Studio and Visual Studio Code for .NET development.
Visual Studio and Visual Studio Code can both be used for .NET development, but they target different workflows.
| Aspect | Visual Studio | Visual Studio Code |
|---|---|---|
| Type | Full integrated development environment | Lightweight, extensible code editor |
| Platform support | Primarily Windows, with platform-specific editions and support | Windows, Linux, and macOS |
| Features | Integrated designers, advanced debugging, project management, testing, and profiling | Editing, extensions, terminal, debugging, and source control integration |
| Installation | Uses selected workloads | Uses the editor, .NET SDK, and extensions |
| Resource usage | Generally larger | Generally lighter |
| Best suited for | Large enterprise and Windows-focused development | Cross-platform, lightweight, and CLI-oriented development |
Both tools use the .NET SDK and can work with the same project files. The choice depends on operating system, project requirements, team workflow, and the desired level of integrated tooling.
Explain the role of NuGet package versions, dependency restoration, and transitive dependencies.
NuGet package management uses package versions and dependency information to produce a consistent application build.
- Package version: Identifies a specific release of a library. Explicit version selection helps control compatibility and behavior.
- Dependency restoration: The .NET SDK reads package references from the project file and downloads the required packages from configured sources.
- Transitive dependency: A package required by another package. Developers may not reference it directly, but it is restored automatically when needed.
- Version conflict resolution: NuGet evaluates dependency requirements and selects compatible versions according to its resolution rules.
- Locking and reproducibility: A lock file or controlled package versions can help ensure that different machines restore the same dependency graph.
- Security and maintenance: Packages should be obtained from trusted sources and updated regularly to receive bug fixes and security patches.
These mechanisms reduce manual library management and make builds repeatable across development and deployment environments.
What is Git, and how does GitHub support source code management for .NET projects?
Git is a distributed version control system used to track changes in source code. GitHub is a hosting and collaboration platform that stores Git repositories and provides additional project-management features.
Git supports .NET development by allowing developers to:
- Record changes in commits.
- Create branches for features and fixes.
- Merge changes into shared branches.
- Compare revisions and identify who changed a file.
- Revert unwanted changes.
- Work offline because each clone contains repository history.
GitHub adds:
- Remote repository hosting.
- Pull requests for code review.
- Issue tracking and project planning.
- Access control and collaboration.
- Continuous integration workflows through GitHub Actions.
Typical commands include git init, git clone, git status, git add, git commit, git branch, git pull, and git push. Build output directories such as bin and obj should normally be excluded through a .gitignore file.
Define .NET and explain its major features.
.NET is a free, open-source, cross-platform development platform created by Microsoft for building desktop, web, mobile, cloud, IoT, and service-oriented applications.
Major features of .NET include:
- Cross-platform support: Applications can run on Windows, Linux, and macOS.
- Multiple language support: Languages such as C#, F#, and Visual Basic can be used.
- Common runtime: The Common Language Runtime manages execution, memory, exceptions, and security.
- Large class library: The .NET Class Library provides reusable APIs for files, collections, networking, databases, XML, and web development.
- Automatic memory management: Garbage collection automatically releases unused memory.
- Type safety: The runtime and compiler help prevent invalid type operations.
- Interoperability: Applications can communicate with native libraries, databases, web services, and other platforms.
- Modern development tools: Visual Studio, Visual Studio Code, and the .NET CLI support development, debugging, testing, and deployment.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →