编程论坛
注册
登录
编程论坛
→
C# 论坛
求一段能编译通过的int类型的拓展方法
the_second
发布于 2016-04-21 22:45, 2558 次点击
例如调用的时候
int i = 1234;
Console.WriteLine(i.ReverseDigits());
ReverseDigits()为int的拓展函数
求一段代码借鉴借鉴,学习一下,谢谢
2 回复
#2
qq1023569223
2016-04-21 23:26
http://blog.
http://
https://msdn.
#3
qq1023569223
2016-04-22 09:02
程序代码:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
ConsoleTest
{
static
class
intExtendMethod
{
public
static
void
showmsg(
this
int
i)
{
Console.WriteLine(
"
int show extend message!
"
);
}
public
static
int
ReverseDigits(
this
int
i)
{
/*
***********
*/
/*
degisn here
*/
/*
***********
*/
return
--i;
//
return your result
}
public
static
int
addOne(
this
int
i,
int
n)
{
return
i + n;
}
}
class
Program
{
static
void
Main(
string
[] args)
{
Program test =
new
Program();
int
i =
0
;
test.testExtend(i,
1
);
i =
12345
;
test.testExtend(i,
10
);
i = -
12345
;
test.testExtend(i, -
10
);
Console.ReadKey();
}
private
void
testExtend(
int
m,
int
n)
{
m.showmsg();
Console.WriteLine(
"
{0}
"
, m.ReverseDigits());
Console.WriteLine(
"
{0}
"
, m.addOne(n));
}
}
}
1