Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If a method returns a static array, which is to be used by another class...then what problem will arise...?

What I have tried:

Tried to use static array to be returned from a method....
Posted
Updated 10-Jul-20 23:01pm

1 solution

Why would you want to return a static array from a non-static method?
It's probably a bad idea: a static array will have only a single instance app-wide, and a non-static class can have multiple instances - and with modern OSes, multithreading means that the app could be processing on several different instance at the same (or similar) time.

So if your method fills and returns a static array, and a second instance does the same with different data, then both of them will be working with the last set - the previous one will be discarded. This will happen even if your aren't multithreading: if one instance saves a copy of the returned array for later use and the same method is called on a different instance, the first will still lose it's "original" data.

I'd say on balance there would need to be a very, very good reason for doing that - the scope for some awesomely difficult-to-fix bugs happening later when you've forgotten about this are far, far too great.
 
Share this answer
 
Comments
Member 12712527 11-Jul-20 7:12am    
Sir, you said not to return arrays from a property....so I'm thinking this way...because
I need a method in a class will return a static array which is to be consumed by another class..
Suppose...
class a
{
public static string[,,,] rows;
private string[,,,] DoSomething()
{
Do some work with row...
return row;
}
}

class B
{
public void Consumer()
{
private string[,,,] col;
col=a.row;
Do some work.....
}
}
This will be the program....
Sorry if I said anything wrong....not calling the method but returning a static variable.....
Patrice T 11-Jul-20 10:15am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Member 12712527 11-Jul-20 10:24am    
My problem is this as I stated so what shall I do....?
Leave programming.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900