C# How Can a Constructor be Returned in C#?

  • Thread starter Thread starter Silicon Waffle
  • Start date Start date
Click For Summary
In C#, when a method like `func()` returns a Decimal instance, it is not the constructor itself that is returned, but rather an instance of the Decimal struct created by that constructor. The call to `Console.WriteLine("{0}", func());` evaluates `func()`, invoking the Decimal constructor, which creates and returns a Decimal value. This value is then boxed because it is a value type, allowing it to be passed to `Console.WriteLine`, which ultimately converts it to a string using one of the Decimal's ToString methods. The discussion also clarifies that the method header should specify `public Decimal func()` to accurately reflect the return type.
Silicon Waffle
Messages
160
Reaction score
203
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.
 
Technology 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 Silicon Waffle

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
Replies
1
Views
2K
Replies
1
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
6
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K