Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I came across this situation where List has no type specified after it in <>:
List someVar = new List();
However when I try this in Visual Studio I get an error. What is the reason for VS not letting me declare List this way?
Let me show you where I saw it:
public override IEnumerable SomeMethod()
{
List someVar= new List();
// more code
return someVar;
}
P.S After contacting the owner of the project it turned out Wordpress striped out the tags <> after List and IEnumerable, so it actually should be List<SomeClass> and IEnumerable<SomeClass>
public override IEnumerable<SomeClass> SomeMethod()
{
List<SomeClass> someVar= new List<SomeClass>();
// more code
return someVar;
}
There is not an inbuilt class called List. There is ArrayList, but: click on List and press f12. This will show you where it is declared. There are two options:
a class called List that is nothing whatsoever to do with List<T> has been declared in the local project; for example:
class List { ...} // here we go; a class called List
a using alias (at the top of the file) has been used to spoof List as a name; for example:
using List = System.Collections.Hashtable;
or
using List = System.Collections.Generic.List<int>;
You get an error because the List class does not exist in the .NET Framework. If you want to use a non-generic list that can hold any type of object, use ArrayList.
I don't recognise it (I thought it was ArrayList before List<T> arrived?). Either way it would be an older class invented before generics was implemented. I'd use List<object>.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am developing an ASP/c# webform where I am using JQuery as well. I came into a scenario where I need to call. C# function from JQuery. In order to that, I found that function in c# has to be a static method (web method).
The problem is that I need to access all variables, arrays, etc which I used to populat some data and these are not stated c variables. Also, from the web method I need to re-use some the functions which are not static. I ended up gradually just changing all methods and variables to static.
I would like to know if the approach I am taking is correct, and whether there is any pitfall of using static variables/methods and what in simple words makes a difference between static/none-static.
Static variables can be called directly by using class names such as
public class IhaveStatic
{
public static string Hello = "Hello I am A";
}
When you use static this means this will be in memory for life time of your process.
now consider another class as
public class IhaveNoStatic
{
public string Hello = "Hello I am B"
}
public class C
{
Console.WriteLine(IhaveStatic.Hello); // Correct
IhaveNoStatic obj = new IhaveNoStatic();
Console.WriteLine(obj); // Correct
Console.WriteLine(IhaveNoStatic.Hello); // Compile time error
}
as you can see that you need to create object of that class "IhaveNoStatic" to access non-static variable. So, this will be in memory until there is an instance of that class exist.
So, basically it's on your requirement but it is good to use less static variable in your programs.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'd like to ask if it is possible to add an item to a list of interface?
Example:
I have a list of interface:
List<ITestItem> testList = new List<ITestItem>();
I want to add a new item to that list. But I couldn't find a way to create a new instance from ITestItem.
Besides, there is no class that implements the interface ITestItem.
So, is there anyway to add a new item to ITestItem?
All I want is something like:
List<ITestItem> testList = new List<ITestItem>();
testList.Add(new ITestItem());
Thank you in advanced.
Note: my question is to ask if it is possible. If no, it's OK for me.
You can't create an instance of an interface, but you can create an instance of a class that implements an interface.
As long as the object you're adding, is an instance of a class that implements ITestItem it can be added to your List<ITestItem>
Example:
public class SomeListItem : IListItem
{
}
var item = new SomeListItem();
testList.Add(item);
Alternatively (and possibly more likely) there will be a class that already implements IListItem somewhere within the code that has IlistItem
No you cannot instantiate an interface. You need to create a class that implements that interface.
No, an interface is not instantiable. You need a class that implement your interface, and instantiate an instance of this class that you can then add to your list.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am starting to learn c# and I came across this error
Cannot convert implicitly convert Animal into
System.Collections.ArrayList
This is the code
private Animal adoptedPets;
public Animal AdoptedPets
{
get { return adoptedPets;}
set {adoptedPets = value;}
}
I am trying to set a list of Animals to this property of my object. I tried to cast my list like that (ArrayList) adoptedPets, but it didn't work and gave me the above error.
If you want adoptedPets to be a List<Animal> and not just a single Animal, you should declare it as such. Best would be to declare it a s
private IList<Animal> adoptedPets;
public IList<Animal> AdoptedPets
{
get { return adoptedPets;}
set {adoptedPets = value;}
}
Note the IListinstead of List. You could also use IEnumerable. It is a good pracice so that any kind of list can be assigned.
Now, I am assuming that you want your adoptedPets to store a list of Animals . But you are not precise at all in your question.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Why doesn't the .net generic type List<T> offer any Clone() function?
Why doesn't it implement IClonable?
The problem with cloning objects and, especially the ICloneable interface, is that the public interface doesn't communicate the intention well.
Namely - will such Clone function of the List<T> clone contained elements as well, or just clone the list and copy the references to contained elements? Shallow copy, which copies the references and only creates the new list would be equivalent to this:
List<T> clone = new List<T>(originalList);
However, if you wanted to force all the contained elements to be cloned as well, then it would be equivalent to this:
List<T> clone = originalList.Select(x => (T)x.Clone()).ToList();
This assumes that the type T is implementing ICloneable. However, even with this solution, exact effects of code execution cannot be told in advance. What does it mean for an element x to clone itself? Will it be a shallow copy (offered by the MemberwiseClone method it inherits form System.Object), or will it be a deep copy. And if deep, what will happen if two objects in the list are referencing the same third object? Will that third object be copied twice or only once? And so on... you can see where this is going.
For all the reasons listed above, cloning facilities are not incorporated in the framework. It is left to custom code to decide what it means to clone an object and then implement custom code for that.
To make a cloneable List type you would do:
public class CloneableList<T> : List<T>, ICloneable
{
public object Clone() {
var clone = new CloneableList<T>();
clone.AddRange(this);
return clone;
}
}
Or this if you want it deeply cloned:
public class DeepCloneableList<T> : List<T>, ICloneable where T : ICloneable
{
public object Clone() {
var clone = new DeepCloneableList<T>();
clone.AddRange(this.Select(x => (T)x.Clone()));
return clone;
}
}
Because List<T> does not inherit from IClonable. If you want to do a clone of a list you could do...
var t = new List<IClonable>();
var newList = t.Select(a=>a.Clone()).ToList();
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Just a few questions regarding best practices in c#:
Any reason why I'd prefer to do:
var list = new List<string>();
object[] array = list.ToArray<object>();
comboBox.AddRange(array);
instead of:
var list = new List<string>();
comboBox.AddRange(list.ToArray<object>());
Also any reason why I'd prefer to do:
class myClass
{
private string _hello;
public string Hello
{
get {return _hello;}
set {_hello = value;}
}
}
instead of:
class myClass
{
public string Hello;
}
Your first example just creates an intermediate variable to hold the converted array - if you don't need the array later on then logically they're equivalent.
Your second question is a more significant difference. Properties have many advantages over fields, including potential logic in the get/set accessors, binding to UI controls (most controls can bind to properties but not fields.
In general any public data members should be implemented as properties instead of fields. Non-public data members can be implemented as either.
There are lots of answers on SO that answer your second question.