pointer in C#
-
Can anyone tell me if there is something like pointer in C#. If I want to interchange two rows in a mtrix which is the most efficient way? In C, probabaly we can use, if we wan to elliminate the extensive use of the stoarage space. Regards, Amit
-
Can anyone tell me if there is something like pointer in C#. If I want to interchange two rows in a mtrix which is the most efficient way? In C, probabaly we can use, if we wan to elliminate the extensive use of the stoarage space. Regards, Amit
You can use regular old pointers in C#. You just have to do it in an unsafe context:
unsafe static void WriteLocations()
{
int *p;
int i;
i = 10;
p = &i;string addr;
addr = int.Format((int) p, "X");
//To Format the Pointer to a StringConsole.WriteLine(addr);
Console.WriteLine(*p);*p = 333;
Console.WriteLine(i);
//Change the Value using Pointeri = *(&i) + 10;
Console.WriteLine(i);}
Also, instead of marking a class or function as static, you can mark portions of code as unsafe:
void DoSomething() // safe function
{
unsafe // unsafe block of code in safe function
{
// use pointers here
}
}Also note that the C# compiler needs the /unsafe switch turned on in order to use unsafe code.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango
-
You can use regular old pointers in C#. You just have to do it in an unsafe context:
unsafe static void WriteLocations()
{
int *p;
int i;
i = 10;
p = &i;string addr;
addr = int.Format((int) p, "X");
//To Format the Pointer to a StringConsole.WriteLine(addr);
Console.WriteLine(*p);*p = 333;
Console.WriteLine(i);
//Change the Value using Pointeri = *(&i) + 10;
Console.WriteLine(i);}
Also, instead of marking a class or function as static, you can mark portions of code as unsafe:
void DoSomething() // safe function
{
unsafe // unsafe block of code in safe function
{
// use pointers here
}
}Also note that the C# compiler needs the /unsafe switch turned on in order to use unsafe code.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango
Thanks Judah! How can one function be unsafe? what is a unsafe code. regards, Amit
-
Thanks Judah! How can one function be unsafe? what is a unsafe code. regards, Amit
Unsafe code is
- code that cannot be verified as type-safe by the .NET Common Language Runtime (CLR).
- is not Common Language Specification (CLS)-compliant, meaning that other .NET languages like VB.NET, J#, and so on are not required to implement nor are they guaranteed to consume such code, if it were declared as public and made available to other .NET dlls & executables.
- requires a full trust security scenario in order to run, for instance, it must be installed/run from the target machine; if you try to use pointers in code run in a partial-trust scenario (e.g. a C# applet over the internet), the .NET CLR will throw a SecurityException because pointers require full access to the machine's memory, thus are potentially dangerous to the end user.
For a function, class, or code block to be unsafe, just use the unsafe keyword:
unsafe class MyClass
{
}unsafe void MyFunction()
{
}void MyFunctionWithUnsafeBlock
{unsafe
{
}}
Despite all these cautions, you're free to use pointers as much as you like if your application is a standalone app or dll, indeed, the .NET framework uses pointers internally quite often for performance gains and interop scenarios.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuality in Christianity Judah Himango
-
Can anyone tell me if there is something like pointer in C#. If I want to interchange two rows in a mtrix which is the most efficient way? In C, probabaly we can use, if we wan to elliminate the extensive use of the stoarage space. Regards, Amit
-
There is seldom any real need for pointers. Can't you use a reference instead? --- b { font-weight: normal; }
I really wanted to use something which can allow me to interchange position of two rows in a 2D array. say i want to make 3rd row 7th row and place 7th row as the third row. I don't want to use a temporary 1Darray for that. If each row has say 1000 elements, then i need 1000 temporary varibale to do the calculation. I don't want to do that.
-
I really wanted to use something which can allow me to interchange position of two rows in a 2D array. say i want to make 3rd row 7th row and place 7th row as the third row. I don't want to use a temporary 1Darray for that. If each row has say 1000 elements, then i need 1000 temporary varibale to do the calculation. I don't want to do that.
-
What does the array contain? If it contains objects, it's really an array of references. Then you can easily exchange different elements in the array. --- b { font-weight: normal; }
Ironically, it contains double precision numerical value. Is there a way? How can I change the reference if it consists of objects. Regards, Amit
-
I really wanted to use something which can allow me to interchange position of two rows in a 2D array. say i want to make 3rd row 7th row and place 7th row as the third row. I don't want to use a temporary 1Darray for that. If each row has say 1000 elements, then i need 1000 temporary varibale to do the calculation. I don't want to do that.
-
Ironically, it contains double precision numerical value. Is there a way? How can I change the reference if it consists of objects. Regards, Amit