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 pointerObject-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/DifferentBetweenStructAndClassCodePudding user response: