How Can a Constructor be Returned in C#?

  • Context: C# 
  • Thread starter Thread starter Silicon Waffle
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the concept of returning a constructor in C# and how the returned value can be output using the Console.WriteLine method. Participants explore the behavior of the Decimal struct and its constructor, focusing on the evaluation and conversion processes involved.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants explain that the constructor is not directly returned; instead, the function evaluates the constructor call, creating an instance of the Decimal struct which is then returned.
  • One participant suggests that the function header should specify 'public Decimal func()' instead of 'public decimal func()'.
  • Another participant proposes an alternative way of writing the function to clarify the process of returning the Decimal instance.
  • There is a discussion about what the variable 'dec' represents when returned, with a participant questioning whether it is a raw number.
  • Some participants mention that the value returned is boxed when passed to Console.WriteLine, and that it is converted to a string within that method.
  • One participant expresses uncertainty about when the conversion to a string occurs, suggesting that it may be the value of 'dec' that is being passed as a string.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of how the Decimal instance is created and returned, but there are differing views on the specifics of the conversion process and the nature of the returned value.

Contextual Notes

Some participants reference specific behaviors of value types and boxing in C#, but there are unresolved details regarding the conversion to string and the implications of the Decimal struct's behavior.

Who May Find This Useful

This discussion may be useful for C# developers, particularly those interested in understanding value types, constructors, and method return behaviors in the context of outputting values.

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.
 
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   Reactions: 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
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
6
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K