|
The statics of a class are executed once, at the first reference to said class. You can't undo that without unloading the class. The whole purpose of statics is to execute once.
And you can't unload a class, the only relevant thing you can unload is an AppDomain. So if you need a fresh class, you could load it in a separate AppDomain, use it, then unload the AppDomain. And again and again.
|
|
|
|
|
thank you very much for your help.
I'm going to try it
|
|
|
|
|
i created a list<string[]> and added data to it
List<string[]> dataArray = new List<string[]>();
dataArray.Add(new string[] { "start", "end" });
dataArray.Add(new string[] { "start2","end2" });
dataArray.Add(new string[] { "start3", "end3" });
now i want to delete the string[] containing {"start2","end2"} how to do it using built in functions like contains,find or by any other methods ?
when i tried following ....
List<String> Key = new List<String> { "start2","end2" };
dataArray.RemoveAll(k => k.Contains(Key));
getting an error :Compiler Error CS0411
The type arguments for method 'method' cannot be inferred from the usage. Try specifying the type arguments explicitly.
can any one help ????
JANARDHAN
|
|
|
|
|
Hi ,
As the elements in the list item are string arrays, you cannot use the contains keyword to compare.
Use the below code,
List<String> Key = new List<String> { "start", "end" };
dataArray.RemoveAll(item => Enumerable.SequenceEqual(item, Key));
Hope this solves the problem.
-Manognya
__________________________________________________
$ God gives what is best.Not what all you wish
|
|
|
|
|
|
Also, item in 'dataArray.RemoveAll(item => Enumerable.SequenceEqual(item, Key));' specified to represent the item in the list and not a variable.
-Manognya
__________________________________________________
$ God gives what is best.Not what all you wish
|
|
|
|
|
+5 Great answer !
best, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
|
|
|
|
|
Thanks bill
-Manognya
__________________________________________________
$ God gives what is best.Not what all you wish
|
|
|
|
|
You can't remove using dataArray.Remove(new string[] { "start2","end2" }); as that new array isn't the same as the original one.
However I see three ways, each taking some additional code:
1.
keep a reference to the item you will want to remove later on:
...
string[] toBeDeletedLater=new string[] { "start2","end2" };
dataArray.Add(toBeDeletedLater);
...
dataArray.Remove(toBeDeletedLater);
2.
remember the index in the list, then use RemoveAt()
int toBeDeletedLater=dataArray.Count;
dataArray.Add(new string[] { "start2","end2" });
...
dataArray.RemoveAt(toBeDeletedLater);
This will fail if other list operations cause the indexes to change...
3.
locate the element you want to delete (worst case: iterate and check the content of each element), then call RemoveAt.
|
|
|
|
|
|
it's because of the compiler generated types, that have no relation with each other, says that for List<UsingGenericInterface> the compiler generates the type ListUsingGenericInterface and for List<IGenericInterface> they generates the type ListIGenericInterface . the types have no relation with each other and, although the ListIGenericInterface can have the same items than the ListUsingGenericInterface , they are not the same type, so they can't be assigned without a conversion, the compiler doesn't know about the interchangeability, so they doesn't generate one
sorry if my answer aren't so comprehensive, its because i'm a brasiliam and my english is poor
|
|
|
|
|
|
take a look at the List<T>
declaration:
public class List<T> : IList<T>, ICollection<T>,
IEnumerable<T>, IList, ICollection, IEnumerable
i guess that the compiler generates a interface from IEnumerable<IGenericInterface> and makes all types generated for types that implement IGenericInterface implement that interface, so they are interchangeable.
think like this:
you have a Class A that implements interface I, and a Class B that also implements that interface, you can't write:
A a = new A();
B b = new B();
a = b;
because, although they implement the same interface, they are not the same type.
i think the same concept can be applied to List<A> and List<I>, they are both types that implement IEnumerable<I>, but aren't the same type.
what i really need to search more it's this case:
public IList<IGenericInterface> GetStuff()
{
return new List<UsingGenericInterface>();
}
at this moment, i can't explain why it didn't work, but i'll discover
EDIT: i've forgotten about the < and >
|
|
|
|
|
One other reason not mentioned is that returning IEnumerable or IEnumerable<T> is a requirement for using iterator blocks (yield keyword) .
|
|
|
|
|
but this was accomplished by returning an IList<T> , given that IList<T> inherits from IEnumerable<T> , or not?
|
|
|
|
|
SledgeHammer01 wrote: returning IEnumerable or IEnumerable<T> is a requirement for using iterator blocks A very interesting reply, thanks +5.
This is an area where I think I am a bit "stuck" in my grokking of "yield" (waterboarding myself frequently in Skeet's "C# in Action" is not seeming to help me ferret out the plot).
If you care to comment further on this, that would be appreciated.
thanks, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
|
|
|
|
|
Just a shortcut. Instead of implementing a full blown enumerator, you can just do something like:
public IEnumerable<int> GetEnumerator()
{
for (int i = 0; i < 100; i++)
yield return i;
}
foreach on the returned IEnumerable<int> will work on that just as expected:
foreach (int i in GetEnumerator())
{
System.Diagnostics.Writeline(i);
}
|
|
|
|
|
Let's take this question ... an interesting one ... into the "Twilight Zone:"
public IEnumerable<IGenericInterface> GetStuffX()
{
return (new List<UsingGenericInterface>()) as IEnumerable<IGenericInterface>;
}
public IEnumerable<UsingGenericInterface> GetStuffY()
{
return (new List<IGenericInterface>()) as IEnumerable<UsingGenericInterface>;
}
Why do these compile ?
best, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
|
|
|
|
|
List<T> implements IEnumerable<T>.
|
|
|
|
|
BillWoodruff wrote: Why do these compile ?
MSDN:
"The as operator is like a cast operation. However, if the conversion is not possible, as returns null instead of raising an exception." link to the "as" operator in msdn[^]
so, what happens is that the compiler doesn't care if your conversion always returns null.
i'm trying to instal the .net sdk in this computer (don't have admin privileges) to disassemble a dll and see what code the compiler generates for List<IGenericInterface> and for List<UsingGenericInterface> but i suppose that the List<IGenericInterface> implements IList<IGenericInterface> and IEnumerable<IGenericInterface> while List<UsingGenericInterface> implements IList<UsingGenericInterface> , IEnumerable<UsingGenericInterface> and, given that UsingGenericInterface implements IGenericInterface , the List<UsingGenericInterface> also implements IEnumerable<IGenericInterface>
if this is the case, then i think it's a design flaw of the compiler, but i wont take precipitated conclusions.
|
|
|
|
|
My apologies, Jasnoch, and Sentenryu; I do know why they compile, and how "as" functions compared to "casting to Type;" in this case I just thought it would be more interesting to "round out" the possibilities listed.
It was not my intent to divert attention from the main focus of your thread.
best, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
|
|
|
|
|
The short answer is because T on IEnumerable<T>[^] is declared covariant, and the one on IList<T> isn't.
I don't really understand why lists and other collection classes don't have covariant type parameters as well; I guess there must be some problems with implementing it.
|
|
|
|
|
Hi, I'm a beginning C# programmer.
I'm playing with a RichTextBox, applying various colors to the lines. I've noticed that there is a property of the RTB called TextLength. In my testing RTB.TextLength is ALWAYS equal to RTB.Text.Length. So why even have such a property? Everyone knows you can get a String.Length, so why the redundancy?
Originally I thought RTB.TextLength might have something to do with formatting characters vs visible characters but this is apparently not the case.
|
|
|
|
|
Oh nevermind, I think I see now. RTB.Text probably pulls out all that text to a separate copy in memory, THEN takes the length. RTB.TextLength probably doesn't have that overhead. Am I right?
|
|
|
|
|
Yes, TextLength is nothing but the length of the Text property and should be used when you want to obtain the length of the text, and not get at the text for any other purpose.
/ravi
|
|
|
|