Home > Article > Backend Development > How to use managed code and unmanaged code in C#
How to use managed code and unmanaged code in C# requires specific code examples
In C# programming, we often need to use managed code and unmanaged code to achieve some specific functions. Managed code refers to code that runs in the CLR (Common Language Runtime) and is managed by the CLR for memory management and resource allocation. Unmanaged code refers to code that runs directly on the operating system and is not controlled by the CLR. The following will introduce how to use managed code and unmanaged code respectively, with examples.
1. Use of managed code
For example, we want to use a managed class named "MathHelper" in C# to provide a static method to add two numbers. Function.
using System; public class MathHelper { public static int Add(int a, int b) { return a + b; } } class Program { static void Main(string[] args) { int result = MathHelper.Add(1, 2); Console.WriteLine("The result of adding is: " + result); } }
In the above example, we defined a managed class named "MathHelper", which contains a static method "Add" to implement the function of adding two integers. In the Main method, we call the Add method of the MathHelper class to print out the results.
2. Use of unmanaged code
Suppose we have an unmanaged dynamic link library (DLL) that contains a function called "NativeHelper" that is used to Calculate the average of two numbers. We can use this unmanaged function using platform calls (P/Invoke) in C#.
using System; using System.Runtime.InteropServices; public class Program { [DllImport("NativeLibrary.dll")] public static extern double CalculateAverage(int a, int b); public static void Main(string[] args) { int num1 = 10; int num2 = 20; double average = CalculateAverage(num1, num2); Console.WriteLine("The average is: " + average); } }
In the above example, we use the [DllImport] attribute to declare functions in unmanaged code. We can use this unmanaged function in C# by specifying the name of the DLL and the name of the function. In the Main method, we call the CalculateAverage function to calculate the average of two integers and print the result.
It should be noted that when using unmanaged code, we need to ensure that the release of memory and resources is placed in the appropriate place to prevent memory leaks and resource waste.
Summary:
This article details how to use managed code and unmanaged code in C#, and gives specific code examples. Managed code is managed by the CLR for memory management and resource allocation, and has the advantages of cross-platform and automatic memory management; unmanaged code is code directly on the operating system and requires manual management of memory and resources. In actual programming, we can use managed code and unmanaged code as needed to implement specific functions.
The above is the detailed content of How to use managed code and unmanaged code in C#. For more information, please follow other related articles on the PHP Chinese website!