|
What do you think of this bit of code?
Subscriber[] subscribers = subscription.GetSubscribers();
Array.ForEach(subscribers, s => s.SendResponse(publishableItem));
i.e. the use of Array.ForEach with lambda rather than a traditional foreach with a code block?
Regards,
Rob Philpott.
|
|
|
|
|
Looks fine to me. What's the issue?
|
|
|
|
|
No issue, it works ok, but I'm a bit old fashioned and come from a point of view where simplicity always wins the day. Just little things like why are we using static Array.ForEach when the language provides 'built in' support for such things. Also, if I remember back, doesn't every delegate result in a whole class being generated behind the scenes at MSIL level. Isn't there a performance hit associated with invoking a delegate?
Although there's nothing wrong with it as such, why do it this way? It just has a ring of macho programming about it.
Regards,
Rob Philpott.
|
|
|
|
|
Does the job nicely.
I'd probably stick with the foreach code block, only because VS's Edit and Continue doesn't work in functions with lambda expressions, so it would slow down my tweaking/debugging a bit.
But Array.ForEach does look a lot cleaner.
|
|
|
|
|
Why not combine the two lines, by the way?
Array.ForEach(subscription.GetSubscribers(), s => s.SendResponse(publishableItem));
|
|
|
|
|
Well, I'd argue that its more readable and readily understandable being spread over several lines (it makes the execution flow clearer), and the local variable you have removed could be something useful to hover over when debugging.
There's nothing wrong with the code, I'm not saying that, but I think it can be expressed more clearly and would be easier to debug done the traditional way and I'm trying to gauge whether people agree with me or not.
Regards,
Rob Philpott.
|
|
|
|
|
I'm sure that everyone has their own opinion on "to variable, or not to variable". Personally, I never create one-time-only variables: I only establish them if they need to persist, or if I need to use them more than once. (And honestly, "readability" is never an argument that holds water for me, I've seen it used to defend bad practices in the past.)
It will work fine as you have it, of course.
|
|
|
|
|
Lambdas are a code smell.
|
|
|
|
|
HI,
I need of a nice gauge control to show a measurament of an angle variation ... can help me to found control / soucecode?
Thanks a lot!
Alex
|
|
|
|
|
Have you tried Google? "C# gauge control" gives some good results.
|
|
|
|
|
I don't known what does Attributes means and how to use it? Please show me the basic about it, thanks a lot!
|
|
|
|
|
If only somebody would invent a mechanism to find information on this cluttered mess we call the internet. By golly, they'd make a fortune. I take it you're hoping that we have stumbled across some nugget of information that provides the answer.
By randomly typing words into the browser address bar, I managed to find this page[^] in seconds. I must be some form of savant.
|
|
|
|
|
|
I've tried to be nice for a long time, but the level of dross we're trawling through is getting to me. I guess I'm back to my old ways.
|
|
|
|
|
I know exactly what you mean, I've even been giving it a bit of a break recently.
The quality of the questions has been low recently, and some claque-y voting for articles etc. The CP has been consistently been the best .net forum for the 10ish years I've been developing, but I'm not confident that will continue to be the case in the next few years unless things improve substantially & soon.
|
|
|
|
|
I don't understand the word "sarcasm". Is there some place I can go to learn more?
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
|
Mark Nischalke wrote: I don't understand the word "sarcasm". Is there some place I can go to learn
more?
JSOP's house?
|
|
|
|
|
I'm sure I could get several different calibre of answers there.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Certainly no idiot, that's for sure
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
|
|
|
|
|
Attribute
Developers can add metadata to their code through attributes. There are two types of attributes, custom and pseudo custom attributes, and to the developer these have the same syntax. Attributes in code are messages to the compiler to generate metadata. In CIL, metadata such as inheritance modifiers, scope modifiers, and almost anything that isn't either opcodes or streams, are also referred to as attributes.<br />
A custom attribute is a regular class that inherits from the Attribute class. <br />
Custom attributes are used by the .NET Framework extensively. Windows Communication Framework uses attributes to define service contracts, ASP.NET uses these to expose methods as web services, LINQ to SQL uses them to define the mapping of classes to the underlying relational schema, Visual Studio uses them to group together properties of an object, the class developer indicates the category for the object's class by applying the [Category] custom attribute. Custom attributes are interpreted by application code and not the CLR.When the compiler sees a custom attribute it will generate custom metadata that is not recognised by the CLR. The developer has to provide code to read the metadata and act on it.
|
|
|
|
|
Is there a time-out for a loop.
for example, i have a loop that goes on and on for long time and by some means i could set time to 2 seconds so that it breaks out of loop after specified time.
|
|
|
|
|
I'm not sure if it is the best solution, but you could start a timer when the loop first enters and when the timer expires set a static boolean (or thread safe boolean) to true.
the for loop would like this more or less:
staticbool = false;
for(int i = 0; i < verybiginteger && staticbool == false; i++){
}
private void TimerTimeoutReached(){
staticbool = true;
}
note that it will finish the current loop before exiting.
hope this helps.
V.
|
|
|
|
|
This doesn't feel quite right but you could do it like this
DateTime d = DateTime.Now.AddMinutes(2);
for (int i = 0; i <= veryBigInt; i++ )
{
if (DateTime.Now > d)
break;
}
But as i said this really feels like a hack, and I would say there is a faulty decision in the design somewhere.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
|
|
|
|
|
First of all, there is no implicit loop operation that breaks out at a specified time interval. You'll have to code it yourself.
Secondly, as your loop depends on reaching a value or a timeout being reached, a for loop is not the appropriate choice - it is designed to work with a fixed number of iterations. In this case, I would choose to use a while loop instead. Then you could do something like the following:
Stopwatch watch = new Stopwatch();
int count = 0;
do
{
if (!watch.IsRunning)
{
watch.Start();
}
PerformSomeLongRunningOperation();
} while (++count < 10 && watch.Elapsed <= TimeSpan.FromSeconds(2) );
watch.Stop();
|
|
|
|