|
Thats slick - didn't know you could do that. I have loads of events firing off to do 1 or 2 lines of code. Humour me here...is the following correct...
System.Timers.Timer t = new System.Timers.Timer();
t.Interval = 100;
t.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e) { Console.Write("."); };
t.Start();
while (true)
{
System.Threading.Thread.Sleep(1000);
}
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
|
|
|
|
|
Exactly. And, your timer method will be in the scope of the method that created it, which is sometimes very handy indeed.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
One great advantage of anonymous methods is that it can access local variables within the regular method scope. For example:
List<int> numbers = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
List<int> primeNumbers = new List<int>(new int[] { 2, 3, 5, 7, });
List<int> nonPrimeNumbers = numbers.FindAll(delegate(int i) { return !primeNumbers.Contains(i); });
If I had used a classic delagate, primeNumbers wouldn't be accessible.
|
|
|
|
|
ack thats so useful and I cant believe I didn't think of it!
There are loads of places where I have Find delegates for the List<> object and Ive had to store a member variable with a parameter that I had accessible in the calling method!
|
|
|
|
|
Thanks for pointing this out!
Kevin
|
|
|
|
|
Thanks a lot for your answer. Christian Graus,Malcolm Smart and szukuro.
The way I understand is that things that can be done with anonymous method can also be done with delegate. There is no particular case that has to be used the anonymous method.
Michael Sync wrote: scenarios where can't be done without anonymous methods.
there is no such a case. isn't it?
The advantages of using anonymous methods is that we can organize the code and it can save the time of typing.
So, the usage of anonymous methods is not vital. it's just extra thing that help the developer to write the code quickly (more productivity. )
That's all.
Is there anything can be added more??
|
|
|
|
|
Michael Sync wrote: The way I understand is that things that can be done with anonymous method can also be done with delegate. There is no particular case that has to be used the anonymous method.
Depends on how you view it. As I pointed out in my example, a regular delegate couldn't have accessed the local variable, so it could not have been done that way. Then again you can overcome this by simply declaring a class member for example so the regular delegate can access it. So yes, one way or another anything can be achieved with regular delegates as with their anonymous counterparts, however there are scenarios where one should be favored over the other.
Also a side note, in C# 3.0 you can use lamdba expressions, which are much shorter then anonymous methods (but are more or less the same thing, though the former can be built in run-time using expression trees). So you can write code like:
List<int> nonPrimeNumbers = numbers.FindAll(i => primeNumbers.Contains(i)); Pretty neat, don't you think?
|
|
|
|
|
oh. wow.. ya... i totally agreed with you.. Thanks a lot.. szukuro..
|
|
|
|
|
1. Closures
2. Closures
3. Closures
|
|
|
|
|
hi there,
i have a problem in array that is i define an array of string in general declaration
string[] mlines;
and in load event i assing a value to it like this
mlines[0] = "xyz";
but when i run application it gives error
"Object reference not set to an instance of an object."
its giving error because it default value is null...
can somebody tell how to solve it.....;P
Becoming Programmer...
|
|
|
|
|
string[] mlines = new string[3];
mlines[0] = "xyz";
-^-^-^-^-^-
no risk no funk
|
|
|
|
|
I suggest you read a basic C# text, you cannot create any reference type without the 'new' keyword.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hello,
The StringCollection would be the dynamic way!
System.Collections.Specialized.StringCollection yourStringCollection = new System.Collections.Specialized.StringCollection();
yourStringCollection.Add("addedText");
All the best,
Martin
|
|
|
|
|
xmen_xwk wrote: string[] mlines;
where is the "new" keyword?
try :
string [] mlines = new string [3];
Regards,
Tomi
|
|
|
|
|
thanks
but i dont know how much size of array will be there
for intance
if i wanna write
string [] mlines = new string [3];
mlines = string2.split('.')
it might be possible that splitted part more than 4,
then what i should write instead of 3
Becoming Programmer...
|
|
|
|
|
Hello,
In this case you don't have to instanciate the array, because the Split method returns an new instance of string[].
This will work:
string[] mlines;
mlines = string2.split('.');
All the best,
Martin
|
|
|
|
|
but that defining array in GD giving error as i said above
Becoming Programmer...
|
|
|
|
|
Hello,
It would help if youpost the error message.
But this works:
string test = "abc.def.ghi";
string[] testarray = test.Split('.');
All the best,
Martin
|
|
|
|
|
for error read my first post
and you are doing same as i said above
Becoming Programmer...
|
|
|
|
|
Hello,
xmen_xwk wrote: for error read my first post
and you are doing same as i said above
Sorry, but I'm loosing a little bit of patience write now, as your post seems a little rude to me!
Have you even tried what all the others and I suggested?
You where mixung up two different possibilities of how to use the string[].
1)creating an modifying a string array
string[] testarray = new string[3];
testarray[0] = "firststring";
testarray[1] = "secondstring";
testarray[2] = "thirdstring";
2)Getting a stringarray from the string.Split method.
my answere 10h ago[^]
So if you tried and still have problems, you should post all the involved code + errormessage.
We are trying to help you in our free time, so please respect that, and at least try to help us to help you!
All the best,
Martin
|
|
|
|
|
Martin# wrote: as your post seems a little rude to me!
sorry buddy but i wasnt in rude manner, you misundertood me or i wrote wrong sentences, and i never be rude for who are helping me and others...
i said "for error read my first post" because i thought that you forgot error or werent read that, anyway if you felt hurt then sorry
and about my problem :
string[] mlines;
mlines = string2.split('.');
this code works if i define
string[] mlines; in any event(click, load, etc) but its giving error if i define it in General declaration
the error is
"Object reference not set to an instance of an object."
Becoming Programmer...
|
|
|
|
|
Hello,
xmen_xwk wrote: sorry buddy but i wasnt in rude manner, you misundertood me or i wrote wrong sentences, and i never be rude for who are helping me and others...
My impression was, that you were not trying out what have been suggested.
But anyway, I have a wild guess write now.
I don't think that your problem is the string[] mlines, I think it's "string2" which is null.
Because you have not assigned a text at this time.
try:
if(string2!=null)
mlines = string2.split('.');
Hope it helps!
All the best,
Martin
|
|
|
|
|
Hello,
I am stuck up with one problem from last 4 days please help.
Problem: I have to write a code to simulate DEADLOCK among threads and the formula that is documented for detecting the deadlock is:
Formula: “Current Queue Length” = “Threads Count” AND “Process/% Processor Time” < 1
The Definition of “Current Queue Length” counter is
: This displays the total number of threads that are currently waiting to acquire a managed lock in the application.Iit displays the last observed value. This helps you identify inefficient synchronization mechanisms.
The count keeps on rising steadily.
But this Counter(Current Queue Length) always shows 0 value when I look at it's value through Perfmon utility
The code which I have written is as follows:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Threads
{
class Tester
{
static void Main( )
{
// make an instance of this class
Threads.ThreadingExamples.CPUSpinWithDeadlock p = new Threads.ThreadingExamples.CPUSpinWithDeadlock();
// run outside static Main
p.DoTest();
break;
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
namespace Threads.ThreadingExamples
{
class CPUSpinWithDeadlock
{
public bool locked = true;
public static Thread[] ThreadsArray = new Thread[10];
//Creating a var "Count" which should be in Critical Section to avoid concurrent access to it.
public static string Count = "You r in ";
public void DoTest()
{
Console.WriteLine("Press any key to continue");
Console.Read();
//Main Thread ------- Starts.
//Lock the var Count to avoid concurrent access by threads.
for (int counter = 0; counter < 10; counter++)
{
if (counter == 0)
{
ThreadsArray[counter] = new Thread(
new ThreadStart(ILoop));
}
else
{
ThreadsArray[counter] = new Thread(
new ThreadStart(Loop));
}
ThreadsArray[counter].Start();
}
}
public void ILoop()
{
Console.Write("First Thread ");
Lock(CPUSpinWithDeadlock.Count)
{
Count = "Main Thread";
while (true)
{
Console.WriteLine("Hello");
//Thread Sleep to reduce the CPU Utilization by current thread.
Thread.Sleep(1000);
}
}
}
public void Loop()
{
Console.Write("Calling lock ");
// To make remaining 9 threads wait for thread no 1 to release the lock acquired by it
//so that the remaining threads would be queued and the the counter value of "Current Queue Length"
// would go up.
CPUSpinWithDeadlock.ThreadsArray[0].Join();
Lock(CPUSpinWithDeadlock.Count)
{
CPUSpinWithDeadlock.Count = "Child Thread";
Monitor.Exit(CPUSpinWithDeadlock.Count);
}
}
}
}
Thanks & Regards,
Vikas M Pai
Software Engineer
|
|
|
|
|
Hai Everybody,
Have a nice day!
Can we use default parameters in C# if yes please tell me how or else give me an alternate way to achieve default parameter concept
Best Regards,
M. J. Jaya Chitra
|
|
|
|
|
No, you have to do this
void MyMethod(string s, bool b)
{
}
void MyMethod(string s)
{
MyMethod(s, true); // makes true the default
}
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|