c sharp

C# Pointers

In C#, pointers are allocated for uncommon and advanced programming; they are normally used when interacting with the COM framework. Pointers are only supported to a limited extent in C#. Pointer types, unlike reference types, are not monitored by the default garbage collection process. Pointers can only point to unmanaged types which include all fundamental enum types, data types, other pointer types, and structs that just include unmanaged types. The article will adequately demonstrate to you about C# pointers.

What are Pointers in C# in Ubuntu 20.04?

Pointers are utilized to dynamically allocate memory. We can say that a pointer is a variable that holds the address of a memory location. There are two parts to every variable specified in a program:

  1. Variable Address.
  2. Variable value stored.

When a statement in C# is declared as unsafe with the unsafe keyword, pointers are used. These statements, which make use of pointer variables, are not controlled by garbage collectors.

Making Declaration of Pointers in C# in Ubuntu 20.04

The subsequent script is the general syntax of how to declare a pointer type:

type  *variable_Name;
int* p;

The dereference operator is denoted by the asterisk symbol (*). The value from the address to which pointer references is acquired using the dereference operator. Declares “p”, a pointer variable that carries an int type’s address. Note that when a function of the code block is designated with the unsafe modifier, C# allows pointer variables to be used. A code block where a pointer variable is declared is known as unsafe code or unmanaged code. In Ubuntu 20.04, we have to compile unsafe code like this: cs /unsafe filename.cs

How to Use Pointers in C# in Ubuntu 20.04

The keyword unsafe can be used in a lot of ways. It can be used to modify a method, property, constructor, and so on. For a better understanding of how pointers are declared in the C# programming language, consider the following examples:

Example # 1: Using Pointers for Declaration in C# in Ubuntu 20.04

The following illustration demonstrates how to declare and utilize a pointer variable. Here, we’ve utilized the unsafe modifier. The sign * is used to declare pointers implicitly, as in the given example:

The code begins with importing the using and namespace files. The namespace file has assigned the name “CsharpPointers”. The namespace file has a block in which we have defined a class “program1”. In the class “program1”, we have unsafe keywords used with the main function. The unsafe keyword allows us to use unsafe codes in C# only. Then, we have declared two variables in the unsafe main function. The first variable is assigned a name “Value” and sets its type to int. The variable “Value” is an integer value. The second variable is represented as “v” of type int with the asterisk (*) symbol so it is a pointer variable of type integer. The pointer variable has the address operator(&) used with the variable “Value”. As a pointer, it will return the value’s address. The writeLine statement will print the value and address of the specified variables.

We got the following output from the above code execution. The value and the address are shown in the image.

Example # 2: Using Pointers to Access Arrays in C# in Ubuntu 20.04

An array is a set of data of the same kind that are only distinguished by their storage order. In the following C# program, arrays are accessed using pointer notations.

The code is demonstrated where we have defined the class in the namespace file “pointerProgram”. The class has assigned a name “Program2” and we have called the main function with an unsafe modifier. In the unsafe main function, we have created an int-type reference array as “MyArray”. We have stored five different values in an array. Then, we have created a variable “ptr” with pointer notation. The pointer variable has stored an array and we have pinned the pointer variable with the keyword “fixed”. The “fixed” keyword limits the array’s variable to a specific memory allocation. The writeLine statement is used to display the array’s memory address and data type value.

As you can see, every element in the array, as well as its address, is displayed in the output of the above code.

Example # 3: Using Pointers in Structures in C# in Ubuntu 20.04

In C#, value types are the only components of structures. Pointers are only used in structures where the primary members are all value types. Pointers are utilized to access struct members in the following program written in C#.

The above code has a structure defined as “Employee” and in the structure, we have set its members “EmpID” of type int and “EmpSalary” of type property double. We have also built the constructor for the structure “Employee” to initialize values for “EmpID” and “EmpSalary” as “x” and “y”. Then, we created a class “Program3” which has the main function. In the main function, we have an unsafe label which would not need to use the unsafe statement to encapsulate the pointer variables or anything else connected to it, because the function declaration will do itself. The values are set for the members “EmpID” and “EmpSalary” in the object “E1”. The pointer is created for the structure “Employee” as “E1_ptr” and initializes it with the address of “E1”.

The WriteLine statement is used to display the given structure “Employee” details. We have used an arrow operator with the pointers which accessed the variables and methods of the structure specified.

The above program execution has the following output shown on the terminal screen.

Example # 4: Using Pointers in Methods in C# in Ubuntu 20.04

A pointer can be returned by the methods as well. The example shown below has passed the pointers as a parameter to a method.

We have a class defined as “Program4” in the namespace file. The class has a main function as “Method” in which we have used the label “unsafe”. We have declared a variable “A1” and “A2” in the unsafe block. These variables are initialized with the integer values. We have created two pointers as “ptr1” and “ptr2” of type int. Now, these pointers are called integer pointer variables. We have given the address of the variables “A1” and “A2” to these pointers. The writeLine method is called which will display the value and the address of the given variables. Then, we have another class “Demo” where we have to declare an object “P” for the class “Program4”. The “Method” is invoked in this class to access the value and address of its member in the “Demo” class.

We get the subsequent output on the console after compiling the above code.

Conclusion

We have come to the end of the article by discovering that pointers displaying the memory address are executed using an unsafe set of statements. The unsafe code is needed because the garbage collector does not maintain memory locations in an unmanaged environment. We have learned the ways to use pointers in methods, structures, and access the array of elements through the different illustrations.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.