How Can a Constructor be Returned in C#?

  • Context: C# 
  • Thread starter Thread starter Silicon Waffle
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
6 replies · 2K views
Silicon Waffle
Messages
160
Reaction score
202
I find that people can do this in C#
Code:
public decimal func()
{
    return new Decimal(...)
}
and use it like this
Code:
Console.Writeline("{0}",func());

My question is how can the constructor returned in func() be output like that ? Thank you.
 
Physics news on Phys.org
Silicon Waffle said:
I find that people can do this in C#
Code:
public decimal func()
{
    return new Decimal(...)
}
and use it like this
Code:
Console.Writeline("{0}",func());

My question is how can the constructor returned in func() be output like that ? Thank you.
The constructor is not being returned. In the call to Writeline, the expression func() is evaluated, which causes the Decimal constructor to be called. The Decimal constructor creates an instance of a Decimal struct and returns it to the caller, func().

In your func() definition, shouldn't the header be public Decimal func()?
 
Silicon Waffle said:
I find that people can do this in C#
Code:
public decimal func()
{
    return new Decimal(...)
}
and use it like this
Code:
Console.Writeline("{0}",func());

My question is how can the constructor returned in func() be output like that ? Thank you.
Does it make more sense to you if it's written this way? It's the same thing as what you wrote.
Code:
public decimal func()
{
    decimal dec = new Decimal(...);
    return dec;
}
 
Borg said:
Does it make more sense to you if it's written this way? It's the same thing as what you wrote.
Code:
public decimal func()
{
    decimal dec = new Decimal(...);
    return dec;
}
Yes, but I'd still like to know what is "dec" that is being returned. I don't think it's a raw number being fed into WriteLine.
Well, the constructor is like this, for example,
Code:
//...
bits[4]={2422422,28225,0,0};
return new Decimal (18,2,false,bits);
 
Mark44 said:
The constructor is not being returned. In the call to Writeline, the expression func() is evaluated, which causes the Decimal constructor to be called. The Decimal constructor creates an instance of a Decimal struct and returns it to the caller, func().

In your func() definition, shouldn't the header be public Decimal func()?
Thank you Mark44, it should be Decimal in that line.
 
Actually I think it is a string, not a number that is being passed into WriteLine. But I don't know when it has been converted to a string ?
This seems more understandable
Code:
dec=new Decimal(...);
return dec.Value;
 
Silicon Waffle said:
I find that people can do this in C#
Code:
public decimal func()
{
    return new Decimal(...)
}
and use it like this
Code:
Console.Writeline("{0}",func());

My question is how can the constructor returned in func() be output like that ? Thank you.

Your func() returns an instance of the value type Decimal [1,2] which is boxed [3] in the call to Console.Writeline [4] (since its a value type [5]) and then finally converted to a string within the Console.Writeline method by one of the Decimal.ToString methods. By the way, note that decimal is shorthand for System.Decimal.

[1] https://msdn.microsoft.com/en-us//library/364x0z75.aspx
[2] https://msdn.microsoft.com/en-us/library/system.decimal.aspx
[3] https://msdn.microsoft.com/en-us/library/yz2be5wk.aspx
[4] https://msdn.microsoft.com/en-us/library/586y06yf.aspx
[5] https://msdn.microsoft.com/en-us/library/s1ax56ch.aspx
 
  • Like
Likes   Reactions: Silicon Waffle