AddressOf alternative in C #

Can anyboby help me with an alternative solution in C # regarding the AddressOf operator in VB6 ? AddressOf returns a long value. How can I get the result in C #?

+5
source share
4 answers

Turning around Harper Shelby's answer, yes, it can be done, but, as a rule, this is the smell of code for this in .NET.

To get the address of a variable in C #, you can use C-style pointer syntax (*) / address (+ amp;) / dereference (->). To do this, you will need to compile the application using the / unsafe compiler, since you exit the secure network of managed code as soon as you start accessing the memory addresses directly.

An example from MSDN tells most of the story:

int number;
int* p = &number;
Console.WriteLine("Value pointed to by p: {0}", p->ToString());

This assigns the address of the variable to numberpointer-to-int p.

There are some catches:

  • The variable whose address you select must be initialized. Not a problem for value types that are by default, but a problem for reference types.
  • In .NET, variables can move in memory without knowing it. If you need to deal with the address of a variable, you really want to use fixedto bind the variable in RAM.
  • & , . ( , int* p = &GetSomeInt();)
  • , , CLR, .

, - , , .NET. .NET , , . () , ; , , , , , .

, , , , .

+11

# /. <delegate> += <function>;

, . , . , - , , .

+6

Apparently this can be done (although I'm not sure where you need it). Here is the MSDN page .

+2
source
EventHandler handler1 = this.button1_Click;
EventHandler handler2 = new EventHandler( this.button1_Click );
...
...
...
void button1_Click( object sender, EventArgs e ){
    // ....
}

Both notations are equivalent.

0
source

All Articles