以后如何交换两个变量? [英] How to swap two variable later?

查看:82
本文介绍了以后如何交换两个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有非常简单的代码片段来解释我的问题。


Class" Swap"在主要中被包含在内。有两个初始的

变量。

后来,交换 class会交换这两个变量。


如何实现这个交换类?

因为C#没有指针,我不能请。

请给我任何想法....


谢谢,

Jongmin

//

使用System;

使用System.Collections;


命名空间测试

{

公共类MyClass

{

public static void Main()

{

string mainA,mainB;

mainA =" A";

mainB =" B";


交换swapCommand = new Swap(mainA,mainB);

Console.WriteLine(mainA); // Print" A"

Console.WriteLine(mainA); // Print" B"


swapCommand.Do(); //在这里交换...

Console.WriteLine(mainA); //想要打印B不是A

Console.WriteLine(mainA); // wnat打印A不是B

}

}


公共班级互换

{

string _A;

string _B;


public Swap(string a,string b)

{

_A = a;

_B = b;

}


public void Do()

{

string temp;

temp = _A;


_A = _B;

_B = temp;

}

}

}

Hi Everybody,

I have very simple code snippet to explain my problem.

Class "Swap" is construncted in "Main" with two initial
variables.
Later, "Swap" class is going to swap those two variables.

How to implement this Swap Class?
Because C# doesn''t have pointer, I can''t do.
Please give me any idea....

Thanks,
Jongmin
//
using System;
using System.Collections;

namespace Test
{
public class MyClass
{
public static void Main()
{
string mainA, mainB;
mainA = "A";
mainB = "B";

Swap swapCommand = new Swap(mainA, mainB);
Console.WriteLine(mainA);// Print "A"
Console.WriteLine(mainA);// Print "B"

swapCommand.Do(); // Swap take plase here...
Console.WriteLine(mainA);//want to Print "B" not A
Console.WriteLine(mainA);//wnat to print "A" not B
}
}

public class Swap
{
string _A;
string _B;

public Swap(string a, string b)
{
_A = a;
_B = b;
}

public void Do()
{
string temp;
temp = _A;

_A = _B;
_B = temp;
}
}
}

推荐答案

添加另一个相同类型的变量


快乐交换!
add another variable of the same type

happy swapping!


- ----原帖-----
添加相同类型的另一个变量

快乐交换!

-----Original Message-----
add another variable of the same type

happy swapping!
.




我的问题与简单有些不同。


我想迟到交换这些变量。



My issue is some different from simple.

I want to swap those variables late.


Jongmin写道:
Jongmin wrote:
-----原始消息-----
添加另一个相同类型的变量

快乐交换!

-----Original Message-----
add another variable of the same type

happy swapping!
.



我的问题与简单有些不同。

我想迟到交换这些变量。


My issue is some different from simple.

I want to swap those variables late.



你的意思是什么?


vois Swap( ref SomeClass a,ref SomeClass b){

SomeClass t = a;

a = b;

b = t;

}


再见

Rob


to you mean something like that?

vois Swap(ref SomeClass a, ref SomeClass b) {
SomeClass t = a;
a = b;
b = t;
}

bye
Rob


这篇关于以后如何交换两个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆