Introduction To Pointers in C# Language
C# language is vastly used to build mobile applications, web applications for windows-based games, and desktop applications. C# is the fully object-oriented language. With the constant update of frameworks of Microsoft’s .net core, it is always ready to support most of the latest techniques to implement. Here, we will see the details of various pointers used in C#.
Pointers are variable that holds the memory address of value types and arrays in C#. There are two types of codes in C#, safe and unsafe. When an entire code is running under the supervision of the Common Language Runtime, we call it a safe code. Unsafe code extracts resources from external sources like streaming music, or video file, etc.
If we consider garbage collection mechanisms, their pointer types are not tracked, therefore it is a safe code. In unsafe code, if we want to use the unused resources, then we need to implement a technique or we can call GC.Collect(). Pointers are only used in unsafe coding. In a default scenario in C# applications, it does not allow to use of pointers.
Table of contents
Examples to Implement Pointers In C#
Given below are the examples of Pointers in C#:
Example #1
Code:
using System;
namespace Pointers
{
class cbresearch
{
static void Main(string[] args)
{
int* cbPointer;
Console.ReadKey();
}
}
}
Output:
Explanation:
- While declaring this pointer and running this code, it would show an error stated as ‘pointers and fixed-size buffers only be used in an unsafe context’.
- To implement this, we need to go to the built option, where we can find a checkbox called ‘Allow unsafe code’. We need to enable that so that the code can be executed. Before that, we need to declare ‘unsafe’ in the coding part as well.
Example #2
In this example, while running the code, it will return a pointer, as it is justified.
Code:
using System;
class CBresearch
{
public unsafe void Method()
{
int a = 15;
int b = 25;
int* sum = swap(a, b);
Console.WriteLine(sum);
}
public unsafe int*swap (int*a, int*b)
{
int sum;
sum = (a b);
}
}
class CBemployee
{
public static void Main()
{
CBresearch mc = new CBresearch();
mc.Method();
}
}
Output:
Example #3
In this example, we have used the ToString() method to retrieve the data, which is stored at the located referenced by the variable of the pointer. If we execute the code it would produce the output.
Code:
using System;
namespace UnsafeCodeExecution
{
class CBResearch
{
public static void Main()
{
unsafe
{
int var = 99;
int * x = & var;
Console.WriteLine ("Value of Data is: {0}", var);
Console.WriteLine ("Value of Data is: {0}", *x);
}
}
}
}
Output:
Working & Uses of Pointers In C#
Below we see where the pointers are used and how they work:
- So pointers are associated with unsafe contexts. Instead of passing the entire structure to the functions, one can pass the address of the structure, thus makes it efficient. To execute an unsafe code of statements, pointers are used to locate the memory addresses. From the function, it returns more than one value. As it is not tracked by the garbage collector, its best suits the environment which is not managed. More efficiently and effectively it passes strings and arrays to the function.
- Mainly pointers are used in the linked list, queue, stack, etc. We should not forget the difference between unsafe keywords in function, context, and alternatives. While doing runtime checks, saves lots of time for correctness and type safety. You can always balance and utilize this feature to your advantage while developing an application in C#. For more abstract reference data types, the pointer is the perfect implementation.
Recommended Articles
This is a guide to How to Use Pointers in C#? Here we discuss a brief overview on pointers in C# language and its working along with different examples and code implementation. You can also go through our other suggested articles to learn more –
- C# Versions | Top 10 Advantages
- Introduction to Variables in C#
- C# Data Types | Top 3 Examples
- Namespaces in C#
Are you preparing for the entrance exam ?
Join our Programming Languages test series to get more practice in your preparation
View More