Click here to Skip to main content
15,867,928 members
Home / Discussions / C#
   

C#

 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
User987432-Jan-18 23:06
professionalUser987432-Jan-18 23:06 
AnswerRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
OriginalGriff2-Jan-18 22:45
mveOriginalGriff2-Jan-18 22:45 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
User987432-Jan-18 23:06
professionalUser987432-Jan-18 23:06 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
Marc Clifton6-Jan-18 4:19
mvaMarc Clifton6-Jan-18 4:19 
AnswerRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
BillWoodruff4-Jan-18 23:07
professionalBillWoodruff4-Jan-18 23:07 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
User987435-Jan-18 13:18
professionalUser987435-Jan-18 13:18 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
BillWoodruff5-Jan-18 21:13
professionalBillWoodruff5-Jan-18 21:13 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
Marc Clifton6-Jan-18 4:14
mvaMarc Clifton6-Jan-18 4:14 
BillWoodruff wrote:
I think it imperative to think carefully about when, how, and where, any Class instance may be modified.


How very functional of you. Wink | ;)

BillWoodruff wrote:
I am inclined, now, to do without 'ref and 'out,


C# 7's tuple return all but eliminates the need for out. Of all the new additions to the language, it is currently my favorite.

BillWoodruff wrote:
to think about the issue of "immutability" ... F# enforces that strictly.


Actually not quite, the mutable keyword explicitly declares that the field can be changed, which is particularly useful dealing with .NET classes in F#.

BillWoodruff wrote:
With a very complex Class,


The interesting thing I found about F# is that it one usually doesn't write complex classes. Furthermore, a function call rarely needs to clone an object or mutate a field, instead, the function returns a completely new type with the result. This is probably the most dramatic change in thinking when using FP languages -- a function operates on a type and returns a different type with the result of the operation.

A simple example is a type that contains a vehicle make, model, and year. In C#, we might implement the class with a Bitmap for a stock image of that vehicle and method called GetStockImage() that mutates the container class by assigning the bitmap.

In F#, you might simply return a different type containing the image and let the caller combine the make/model/year with the image into a new type. Or, the call to GetStockImage could do that, returning that new type.

So in C#, you would have implemented one "type" as a class. In F#, you would have implemented two, possible three types. The beauty of the F# approach is that the function ShowStockImage is guaranteed (well, in theory) of being passed only a type instance that actually has an image that's been initialized. No "if (image == null) throw Exception" necessary!

Ideally, in F#, you would also have real types for make, model, and year, not just strings and ints!

I hope that made sense!
Latest Article - Code Review - What You Can Learn From a Single Line of Code

Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

Artificial intelligence is the only remedy for natural stupidity. - CDP1802

GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
BillWoodruff6-Jan-18 7:40
professionalBillWoodruff6-Jan-18 7:40 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
Marc Clifton8-Jan-18 3:03
mvaMarc Clifton8-Jan-18 3:03 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
BillWoodruff8-Jan-18 5:18
professionalBillWoodruff8-Jan-18 5:18 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
User9874315-Jan-18 4:10
professionalUser9874315-Jan-18 4:10 
AnswerRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
Marc Clifton6-Jan-18 3:56
mvaMarc Clifton6-Jan-18 3:56 
QuestionC# Progammer Forum for Beginners? Pin
User987432-Jan-18 21:27
professionalUser987432-Jan-18 21:27 
AnswerRe: C# Progammer Forum for Beginners? Pin
Ralf Meier2-Jan-18 21:55
professionalRalf Meier2-Jan-18 21:55 
AnswerRe: C# Progammer Forum for Beginners? Pin
OriginalGriff2-Jan-18 22:09
mveOriginalGriff2-Jan-18 22:09 
GeneralRe: C# Progammer Forum for Beginners? Pin
Pete O'Hanlon2-Jan-18 22:15
subeditorPete O'Hanlon2-Jan-18 22:15 
GeneralRe: C# Progammer Forum for Beginners? Pin
OriginalGriff2-Jan-18 22:36
mveOriginalGriff2-Jan-18 22:36 
GeneralRe: C# Progammer Forum for Beginners? Pin
Pete O'Hanlon2-Jan-18 22:43
subeditorPete O'Hanlon2-Jan-18 22:43 
GeneralRe: C# Progammer Forum for Beginners? Pin
OriginalGriff2-Jan-18 22:49
mveOriginalGriff2-Jan-18 22:49 
GeneralRe: C# Progammer Forum for Beginners? Pin
Dave Kreskowiak3-Jan-18 2:23
mveDave Kreskowiak3-Jan-18 2:23 
GeneralRe: C# Progammer Forum for Beginners? Pin
OriginalGriff3-Jan-18 2:33
mveOriginalGriff3-Jan-18 2:33 
GeneralRe: C# Progammer Forum for Beginners? Pin
Dave Kreskowiak3-Jan-18 4:18
mveDave Kreskowiak3-Jan-18 4:18 
GeneralRe: C# Progammer Forum for Beginners? Pin
OriginalGriff3-Jan-18 4:46
mveOriginalGriff3-Jan-18 4:46 
GeneralRe: C# Progammer Forum for Beginners? Pin
Dave Kreskowiak3-Jan-18 4:56
mveDave Kreskowiak3-Jan-18 4:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.