How Can a Constructor be Returned in C#?

  • C#
  • Thread starter Silicon Waffle
  • Start date
In summary, the conversation discusses how the Decimal constructor is used in the function func() and how it is output in the WriteLine method. It is explained that the constructor creates an instance of the Decimal struct and returns it to the caller, func(). The conversation also touches on the concept of boxing and converting the Decimal value to a string in the WriteLine method.
  • #1
Silicon Waffle
160
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
  • #2
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()?
 
  • #3
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;
}
 
  • #4
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);
 
  • #5
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.
 
  • #6
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;
 
  • #7
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

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

What does it mean for a constructor to be returned in C#?

In C#, a constructor can be returned as a value from a method. This means that the method will create and return a new instance of the class that the constructor belongs to.

Why would you want to return a constructor in C#?

Returning a constructor in C# allows for more flexibility and control in object creation. It allows for dynamic instantiation of objects based on different conditions or parameters.

How do you return a constructor in C#?

To return a constructor in C#, you can use the new keyword followed by the name of the class and any necessary parameters. This will create and return a new instance of the class using the specified constructor.

Can you return a constructor from any method in C#?

No, constructors can only be returned from methods that have a return type of the same class that the constructor belongs to. This ensures that the method can properly create and return an instance of the class.

Does returning a constructor in C# affect the original instance of the class?

No, returning a constructor in C# does not affect the original instance of the class. The constructor will create a new instance of the class, leaving the original instance unchanged.

Similar threads

  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
0
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
Back
Top