C
C#Moods

with or without ref

so i learned that in c#(or most oop languages) when you create a method that takes an object as a parameter, and you call said method and pass an object of the same type as an argument, it passes the reference to the caller. so if you were to alter any properties of the object parameter in the method, it affects the object that was used as a parameter something like
void ChangeName(Person p) {
p.Name = "Alice";
}

Person person = new Person();
person.Name = "Bob";
ChangeName(person);
void ChangeName(Person p) {
p.Name = "Alice";
}

Person person = new Person();
person.Name = "Bob";
ChangeName(person);
i assume that after the last statement person's Name property is now "Alice". i was reading up the docs and it mentioned you would use ref before an object parameter if you wanted to alter the original object argument and I'm confused cus isnt this how objects already worked like..?
X
x0rldβ€’400d ago
you pass the reference of that object ref would be for a valuetype I think πŸ€”
M
Moodsβ€’400d ago
yeahhh i know how it works for value types but the docs covered reference types too and i got all confused ok reading it again i believe i understand it now. with ref it's possible to make the original object point to a new memory
A
Aaronβ€’400d ago
ref allows you to reassign, when talking about reference types
M
Moodsβ€’400d ago
yep
A
Aaronβ€’400d ago
dunno why I said out
M
MODiXβ€’400d ago
Windows10CE#8553
REPL Result: Success
void Assign(ref string s) => s = "abc";

string s = null;
Assign(ref s);
s
void Assign(ref string s) => s = "abc";

string s = null;
Assign(ref s);
s
Result: string
abc
abc
Compile: 458.034ms | Execution: 36.602ms | React with ❌ to remove this embed.
Want results from more Discord servers?
Add your server
More Posts
βœ… .net trying to find non-existent cs files`Error IDE1100 Error reading content of source file 'D:\Dev\C++\Test\TestCS\obj\Debug\net6.0\.❔ WPF - Binding to last child of collectionI have the following setup: ```csharp public partial class StatusMessageService : ObservableObject βœ… What does WPF want from me?I'm actually so tired of WPF and MVVM and all this contrived fuckin bs you guys MainWindow control:❔ I apparently have an infinite loop, but I can't fix it to workI've tried a lot of stuff, even a "NewtonJson" fix I saw, but it wouldn't solve the problem because βœ… doing nothingdfg❔ I would appreciate some help.So i have this assignment and in lieu of my grandma who passed this past week i've not had the oppor❔ Discord Bot Service ProblemDoes anyone know how to register that service properly?❔ abstract class vs interface```cs public interface IFoo { protected static string GetFormattedTime() => DateTimeβœ… Refit / RestLess API SDK StructureHey! How do I structure an SDK for my API? Let's say I have 2 resources: Users and Groups. I coul❔ Best way to display a continually updating list of messages```cs @inject MerpieClient Client @inject MerpieHubConnection HubConnection @inject ILogger<MessageL❔ How can I fix this I keep getting null value in my string variable❔ How do I go from interface to implementation while navigating ASP.NET core code?I saw this youtube video and he seamlessly navigates the source? How can I do this? Thanks.Console Application published as PublishSingleFile the window auto close for few devloper.I have published a .exe file with runtime framework included, and distributed to other developer forβœ… Set Background color for one specific Row in GridI have a WPF project and a grid with a bunch of rows and columns. How can I change the background co❔ Facial recognition and centering using emgucv and arduino servos (visual studio)Hi guys, I am code illiterate. I found this code and need help because I do not know how to fix any ❔ Speech Recognizition makes me wanna cryI have already tried, using speech recogniztion on C++,Rust and now C#, since when this was that har❔ SystΓ¨me connexion wpfHello, Is someone making a connection system in wpf c# xaml to be simultaneously connected to my sitβœ… How to use SVGs in XAML file (WPF)I have a WPF project and I want to use an svg file in MainWindow.xaml. How do I properly use svg in