Рассмотрим следующий пример: здесь int i передан для справки.Is Out Out может использоваться для передачи ссылочного типа (i.e) объектов?
Мой вопрос: могу ли я передать ссылочный тип ? как объект (т.е.) статической силы сумма (из OutExample эрстед)
class OutExample
{
static void Sum(out int i)
{
i = 5;
}
static void Main(String[] args)
{
int val;
Sum(out val);
Console.WriteLine(val);
Console.Read();
}
}
Теперь ниже код, имеющий некоторую ошибку,
class OutExample
{
int a;
static void Sum(out OutExample oe)
{
oe.a = 5;
}
static void Main(String[] args)
{
int b;
OutExample oe1=new OutExample();
Sum(out oe);
oe.b=null;
Console.WriteLine(oe.b);
Console.Read();
}
}
Наконец получил ответ!
class OutExample
{
int a;
int b;
static void Sum(out OutExample oe)
{
oe = new OutExample();
oe.a = 5;
}
static void Main(String[] args)
{
OutExample oe = null;
Sum(out oe);
oe.b = 10;
Console.WriteLine(oe.a);
Console.WriteLine(oe.b);
Console.Read();
}
}
Вы попробовали? Это сработало? –
Вы пробовали? –
Я пробовал приведенный выше код, его хорошо работает. @MarcGravell – Balaji