C
C#DannyRoastBeef㊙

Using ref for large data types.

Hi, just looking for a little clarification on the ref keyword when passing data into a function. I've hot an API endpoint which takes in files which passes them though to a function which checks them and sorts them into an appropriate destination directory. I've got a function in another class which checks the data type is supported, among other things. Would I be right to pass the incoming multipart File data to it using a ref value to avoid making a copy of it? Some of the files can run up to 200mb in size. Thanks for any help.
E
exe13d ago
If you pass in your File class, whatever it's called or contains, you pass the reference to it not a copy. So no need to use ref here.
D
DannyRoastBeef㊙13d ago
Hi thanks for the reply. What about if I was to pass a list containing incoming data as I am here in my GET endpoint?
[HttpPost]
[Route("upload")]
public async Task<IActionResult> UploadDataAsync(List<IFormFile>? files)
{
if (files is not null && _env is not null)
{
return await _checkpoint.SaveDataToStorage(files);
}
return new StatusCodeResult(400);
}
[HttpPost]
[Route("upload")]
public async Task<IActionResult> UploadDataAsync(List<IFormFile>? files)
{
if (files is not null && _env is not null)
{
return await _checkpoint.SaveDataToStorage(files);
}
return new StatusCodeResult(400);
}
In the following function I've got
public async Task<IActionResult> SaveDataToStorage(List<IFormFile> files)
{
List<IFormFile> incomingFiles = files;

if (!CheckValidFileTypes(incomingFiles))
{
return new UnsupportedMediaTypeResult();
(There's more going on in here , shortened it for this post)
}
}
public async Task<IActionResult> SaveDataToStorage(List<IFormFile> files)
{
List<IFormFile> incomingFiles = files;

if (!CheckValidFileTypes(incomingFiles))
{
return new UnsupportedMediaTypeResult();
(There's more going on in here , shortened it for this post)
}
}
Would it be correct to put ref in front of
incomingFiles
incomingFiles
when calling the
CheckValidTypes
CheckValidTypes
function or will that do a copy?
E
exe13d ago
There may be some intricacies which I've forgotten around the reference pointer on reference types using the ref keyword but the object is still the same object ASFAIK a list just contains a copy of the reference not the actual object. Meaning if you change something something on an object inside SaveDataToStorage you'll see the same changes when you step outside in files. You can confirm this quite easily I would not. Confirm that by making a change to one of the files in the list inside CheckValidFileTypes and step outside back into the SaveDataToStorage method, and the change is still reflected. You'll know then that no copies are made and it just passes around references Short answer: use-case for ref is actually quite limited
D
DannyRoastBeef㊙13d ago
Right! Thanks for your help with a succinct answer and explanation. I was getting sick of trying to decipher the correct approach from StackOverflow :blobthumbsup:
E
exe13d ago
No worries, good luck 🙂
J
Jimmacle13d ago
unless the types you're passing in are structs they're already passed as a reference instead of a copy
Want results from more Discord servers?
Add your server
More Posts
crud operations with blazor serverI have a backend that I built and now I want to use it with api calls. I created same model with bacminor glitch, player twitchingso i have a glitch where the player starts "twitching" when you walk beside a block or just on a blo✅ BackgroundWorker getting progressI have a winform applications and with this I have put a ProgressBar on it. I was manually changing My tech stack, what to focus now?- .NET Core - Entity Framework Core (Code First) - MVC - Web API - Identity and JWT - Mediator & CQRBuild Issue due to $(Platform) valueWe are facing build issue when we try to build .net project in custom project configuration, like DePFX certificate for signing the application packageI am trying to use a certificate from azure key vault, but it says that it is invalid or another reaInheritance best practice: test Type of subclass or use enum?See attched UML. Just wondering what the best practice is? Is it better to test the Type of BaseEffeC# - Oracle DataBase Converting the Date (Oracle) to a c# DateTime PropertyHow can I replace TO_TIMESTAMP in my select query with the format my c# code uses? I have a class wiAdding Multitenancy through Finbuckel in the single tenant Dotnet applicationHey I was just implementing the Multitenancy to my existing single tenant dotnet application where oI need help with Deployment LogicSo let's say I have 10 servers (already setup with a load balancer and all that stuff) which all runFilesystem packer slows down after 30k filesAfter the filesystem packer has hashed all 255k files then the DB operations starts to slow down thewhy is my player floating, twitching and can almost not go up a rampso im learning c# for unity and i made a (very) litle scene to test my first person controls i made.Model, Dto, Entity - Id?Hi, I'm messing with a simple CRUD application. My PatientEntity looks like this: public claRequest header field content-type is not allowedI have a third party API which uses SOAP + xml. I want to send a post request. Using Postman I can mRectangle intersection not workinUsing the code provided, I've tried the ways I knew to make the tripods remove once they're hit by aFeeding Random into itself - does it "corrupt the randomness"?If I am to reinitialize an instance of System.Random after every .NextDouble() using the return valuUpdate and add to Resource.resx file via code.We are adding localization to our product and want to automate the process of updating the resx fileWinform project saves the Image with a black backgroundIt works perfectly except it creates a black background which isn't shown in the picturebox preview,✅ Theoretical question. MediatR and DTO and onion structureHello, THe following is just theoretical and I do not need any code In the programming world, transfStack overflow exception when closing one formCan't seem to find the issue.. I have a startscreen, and when any key is pressed on startscreen it t