Home > Net >  Excuse me how like C language, C # realize the pointer to the structure?
Excuse me how like C language, C # realize the pointer to the structure?

Time:02-04

You much you are good:
Before I write a c + +, recently started to rewrite the c #, also conforms to the trend of The Times ~

In c + +, and I'm used to the operation of the pointer
My purpose now, there are a lot of class instantiation obj, hypothesis is called obj1 ~ good obj5
Every obj, inside has a struct (because of the same class instantiation, of course, the same struct)

I hope outside a pointer, you can switch the corresponding to the five struct, operate

Of course I know actually to complete this purpose, there are other practice, I now, just looking for different grammar?

I wrote 2 c + + and c # code, I thought is the same meaning, but carry out different
But can you tell me how to modify the part c #?

Thank you ~ ~ & lt; (_ _) & gt;

Create points 1
//
POINT p1={0, 0};

//to create a "indicators" to '1'
POINT * PTR=& amp; P1.

//'1', modify its value
P1. X=1;
P1. Y=1;

//is to extract the values' indicators' to 'point 2
'POINT p2=* PTR.

//output '1', '2' the value of the
ShowMessage (String (p1) x) + ", "+ String (p1) y));
ShowMessage (String (p2) x) + ", "+ String (p2) y));

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Output:
P1=1, 1
The p2=1, 1
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


Create points 1
//
Point p=new Point (0, 0);

//to create a "indicators" to '1'
IntPtr PTR=Marshal. AllocHGlobal (Marshal. SizeOf (p1));
Marshal. StructureToPtr (p1, PTR, false);


//'1', modify its value
P1. X=1;
P1. Y=1;


//is to extract the values' indicators' to 'point 2
'Point p2=(Point) Marshal. PtrToStructure (PTR, typeof (Point));


//output '1', '2' the value of the
Console. WriteLine (p1) X.T oString () + ", "+ p1. Y.T oString ());
Console. WriteLine (p2) X.T oString () + ", "+ p2. Y.T oString ());

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Output:
P1=1, 1
The p2=0, 0
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//

CodePudding user response:

C # the unsafe can directly use pointer

Object-oriented, Pointers are not used, struct as value types

CodePudding user response:

The c # code is the Point in structure or class? The former is a value type, the latter is a reference type, the result will not be the same.

CodePudding user response:

 
using System;

The namespace DifferentBetweenStructAndClass
{
//https://bbs.csdn.net/topics/399055308? Page=1 # post - 414144690
Class Program
{
The static void Main (string [] args)
{

//the first, if you use a class.
PointClass p1=new PointClass () {X=0, Y=0};
PointClass p2=p1;
P1. X=1;
P1. Y=1;
Console. WriteLine ($" p1. X: {p1. X}, p1. Y: {p1. Y} ");
Console. WriteLine ($" p2. X: {p2. X}, p2. Y: {p2. Y} ");

//second, if you use a structure.
PointStruct p3=new PointStruct () {X=0, Y=0};
PointStruct p4=p3;
P3. X=1;
P3. Y=1;
Console. WriteLine ($" p3. X: {p3. X}, p3. Y: {p3. Y} ");
Console. WriteLine ($" p4. X: {p4. X}, p4. Y: {p4. Y} ");
Console.ReadKey();
}
}

Public class PointClass
{
Public double X {get; set; }
Public double Y {get; set; }
}

Public struct PointStruct
{
Public double X {get; set; }
Public double Y {get; set; }
}
}

The above code, the output is:
P1. X: 1, p1. Y: 1
P2. X: 1, p2. Y: 1
P3. X: 1, p3. Y: 1
P4. X: 0, p4. Y: 0
Interview often asked one of the problems: the differences between class and struct.

CodePudding user response:

Code uploaded: https://github.com/zhanggaolei001/DifferentBetweenStructAndClass

CodePudding user response:

refer to the second floor ziqi0716 response:
Point is the structure of the c # code or class? The former is a value type, the latter is a reference type, the result will not be the same.

Structures are value types, not class
Thank you for your reply
In fact, I now is in the study, is there any way like c + + c # directly operation of similar address
Kind words can be (see official specification, Marshal processing are also object, not a value)
So could you please tell me, except for the unsafe methods fleas have no other way?

CodePudding user response:

reference 5 floor strong12345 reply:
Quote: refer to the second floor ziqi0716 response:
Point is the structure of the c # code or class? The former is a value type, the latter is a reference type, the result will not be the same.

Structures are value types, not class
Thank you for your reply
In fact, I now is in the study, is there any way like c + + c # directly operation of similar address
Kind words can be (see official specification, Marshal processing are also object, not a value)
So could you please tell me, except for the unsafe methods fleas have no other way?

Simply don't use unsafe this thing, if must use, also have to learn other knowledge through again to use).
You can think so, in c #, all class instance variable is actually a pointer. Such as student class instances, such as a student class variable named student_A student_B=student_A that if another student variables, so in fact lead to the two variables is the same instance, the instance of any property changes, through the two variables get will be the same.

CodePudding user response:

Actually you should try to forget as far as possible a pointer in c # this stuff, using object-oriented ideas to see everything in the world.

CodePudding user response:

You have to learn how to abstract, and now the idea is to completely dry the c + + code translation, you should use a pseudo code or UML diagrams to describe your logic, this is the same, no matter use what language plus ca change.

CodePudding user response:

 Span S1=stackalloc char [10]. 
S1 [0]='a';
S1 [1]='b';
ReadOnlySpan S2="iamstring". AsSpan ();
ReadOnlySpan S3=s2. Slice (3, 2);


Or Memory

CodePudding user response:

You should say your application scenarios, and then to consider from the perspective of object-oriented c # code
  •  Tags:  
  • C#
  • Related