前田稔(Maeda Minoru)の超初心者のプログラム入門
![]()
![]()
/********************************/
/*★ struct Sample 前田 稔 ★*/
/********************************/
using System;
class console
{
public static void Main()
{
Point pnt = new Point();
Point pnt2;
pnt.x = 123;
pnt.y = 45;
Console.WriteLine("pnt.xypos= {0}",pnt.xypos());
pnt2= pnt; // 値が渡される
pnt2.x = 7; // pnt には影響しない
Console.WriteLine("pnt.xypos= {0}",pnt.xypos());
System.Console.ReadLine();
}
}
struct Point
{
public int x;
public int y;
public string xypos()
{
return "X:" + x.ToString() + ", Y:" + y.ToString();
}
}
|
pnt.xypos= X:123, Y:45
pnt.xypos= X:7, Y:45
|
pnt.xypos= X:123, Y:45
pnt.xypos= X:123, Y:45
|
![]()
/***************************************/
/*★ struct(class) の代入 前田 稔 ★*/
/***************************************/
using System;
class console
{
public static void Main()
{
Obj s1 = new Obj();
Obj s2;
s1.X = 1;
s1.Y = 2;
s2 = s1;
Console.WriteLine("s2.X={0} s2.Y={1} s1.X={2}", s2.X, s2.Y, s1.X);
s1.X = 123;
Console.WriteLine("s2.X={0} s2.Y={1} s1.X={2}", s2.X, s2.Y, s1.X);
System.Console.ReadLine();
}
}
struct Obj
{
public int X;
public int Y;
}
|
Obj s1 = new Obj();
s1.X = 1;
s1.Y = 2;
|
Obj s2;
s2 = s1;
s1.X = 123;
|
s2.X=1 s2.Y=2 s1.X=1
s2.X=1 s2.Y=2 s1.X=123
|
//struct Obj
class Obj
|
s2.X=1 s2.Y=2 s1.X=1
s2.X=123 s2.Y=2 s1.X=123
|
![]()