如何返回引用类型c#int [英] how to return a reference type c# int
本文介绍了如何返回引用类型c#int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图返回一个C#引用类型,以便我可以在该类之外编辑值:(有点像Java Integer引用类型)
到目前为止,我想到的是:
i am trying to return a c# reference type so i can edit the value outside of the class:(kinda like how the java Integer reference type)
what i came up with so far are as follows:
public class test
{
int i;
public Object get()
{
return i;
}
}
public void program()
{
test t = new test();
t.get() = 1;
}
我在代码中下划线的地方是有错误的地方.
有人知道吗?
the place which i underline in the code is where there is a error.
anyone know how?
推荐答案
尝试以下操作:
public class test
{
int i;
public int I
{
get{ return i; }
set{ i = value; }
}
}
public void program()
{
test t = new test();
t.I = 1;
}
您的代码存在的问题是您无法设置函数.您必须同时使用get
和set
的属性.然后,您可以创建对象的新实例(在本例中为TestClass)并设置其属性的值.
Problem with your code is that you cant set a function. You have to use a property with bothget
andset
. Then you can create a new instance of your object (in this case TestClass) and set the value of it''s properties.
public class TestClass
{
public int MyNumericValue {get; set; }
}
public void program()
{
test t = new TestClass();
t.MyNumericValue = 8;
Console.WriteLine(t.MyNumericValue); // Will return 8
}
这篇关于如何返回引用类型c#int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文