求高手解决C#问题

stutianji 2010-08-23 03:32:57
using System;

class swap ;
{
static void swap( ref int m,ref int n)
{
int temp=m;
m = n;
int n=temp;
}
static void Main()
{
int i=1,j=2;
swap(ref i,ref j);
Console.WriteLine("{0},{1})",i,j);

}
}

调试说swap成员不能与它的封闭类型相同,是个啥意思,求哥哥姐姐指导
...全文
9067 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace swap
{
class Swap
{
public static void swap<T>( ref T m, ref T n)
{
……
[/Quote]
跟我做的一样,http://www.cnblogs.com/hirisw/
redstone8415 2012-06-29
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace swap
{
class Swap
{
public static void swap<T>( ref T m, ref T n)
{
T temp;
temp = m;
m = n;
n = temp;
}
static void Main(string[] args)
{
int i=1,j=2;
Console.WriteLine("Befor Swapped: ({0},{1})", i, j);
Swap.swap(ref i,ref j);
Console.WriteLine("After Swapped: ({0},{1})", i, j);
}
}
}
整理了一下!
redstone8415 2012-06-29
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace swap
{
class Swap
{
public static void swap<T>( ref T m, ref T n)
{
T temp;
temp = m;
m = n;
n = temp;
}


static void Main(string[] args)
{

int i=1,j=2;
Swap.swap(ref i,ref j);
Console.WriteLine("({0},{1})",i,j);

}
}
}
  • 打赏
  • 举报
回复
值传参跟地址传参不同的 地址传参是不不了的、值传才可以对换
H_Gragon 2012-06-25
  • 打赏
  • 举报
回复
楼主为什么要用ref呢?直接用int不就行了
using System;

class swap ;
{
static void swap(int m,int n)
{
int temp=m;
m = n;
int n=temp;
}
static void Main()
{
int i=1,j=2;
swap(i,j);
Console.WriteLine("{0},{1})",i,j);

}
}

  • 打赏
  • 举报
回复
代码整个都有问题,class 后面的那个";"是什么意思?
楼主太粗心了
maysaber 2010-09-05
  • 打赏
  • 举报
回复
变量的作用域以及方法的定义上出现了错误,四楼应该是LZ需要的正解!
YellowManDog 2010-09-03
  • 打赏
  • 举报
回复
四楼之前的,我对你们都无语了,他说调试的时候出的问题,你看看他的编译能通过吗?先不说他的方法名和类名相同,你看看他的那个方法,在同一个作用域呢,同时给一个变量声明2次,你们感觉编译能通过吗?然后,如果是构造函数,就不要有返回类型,如果是普通方法,就不要和类名相同,如果是析构函数,不要有返回类型
Peter200694013 2010-08-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 stutianji 的回复:]
using System;

class swap ;
{
static void swap( ref int m,ref int n)
{
int temp=m;
m = n;
int n=temp;
}
static void Main()
{
int i=1,j=2;
swap(ref i,ref j);
Console.WriteLine("{0},{1})",……
[/Quote]
你的本意应该不是用swap构造函数来实现交换吧...

给你修改了下

using System;

class Program
{
static void swap( ref int m,ref int n)
{
int temp=m;
m = n;
n=temp;
}
static void Main()
{
int i=1,j=2;
Program.swap(ref i,ref j);
Console.WriteLine("{0},{1})",i,j);
}
}
Peter200694013 2010-08-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 q107770540 的回复:]
构造函数 不需要返回类型
public swap( ref int m,ref int n)
{
int temp=m;
m = n;
int n=temp;
}
[/Quote]
正解
q107770540 2010-08-24
  • 打赏
  • 举报
回复
构造函数 不需要返回类型
public swap( ref int m,ref int n)
{
int temp=m;
m = n;
int n=temp;
}

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧