Reputation: 93
I have the doubt regarding few things....
As i am completely new(less than 15 days age in MVC) to this ASP.net MVC concepts and due to lake of knowledge on some fundamental concepts in MVC, really distrubing my work... so,
Can any one clearly and simply explain me the things like,
1.When should we use "IEnumerable", "List"....
2.What is main use behind using the statement like
@model IEnumerable
3.What is main use of
public Virtual Classname Identifier{get; set;}
4.What is main use of
public IEnumerable Identifier{get;set;}
5.What is main use of public List Identifier{get;set;}
I would be very thankfull to you...
Thanks in advance...
Upvotes: 2
Views: 5170
Reputation: 1038710
All your questions except 2.) have strictly nothing to do with ASP.NET MVC. Those are basic .NET/C# questions about data structures and OOP that you should learn before getting into ASP.NET MVC.
1) When should we use "IEnumerable", "List"....
IEnumerable<T>
is the highest interface in the collections hierarchy. It doesn't allow direct access to each element of the collection. It is commonly used when the consumer only needs to iterate over the elements of this collection. List<T>
could be used when you need to model a variable length collection allowing you to add and remove elements to it and directly access an element using an index.
2) What is main use behind using the statement like
@model IEnumerable<Namespace.Models.ModelClasses>
This statement is used to indicate the type of the model in a given Razor view. In this case it indicates that the view is strongly typed to the IEnumerable<Namespace.Models.ModelClasses>
interface meaning that the corresponding controller action that is rendering this view needs to pass an instance of this type to the view.
3) What is main use of
public Virtual Classname Identifier{get; set;}
None. This is not valid C# and it won't compile. On the other hand if you meant the following:
public virtual Classname Identifier { get; set; }
then this means that you have declared the Identifier property as virtual meaning that it could be overriden in derived classes. Contrary to Java, in .NET members are sealed by default unless you mark them as virtual
.
4) What is main use of
public IEnumerable<ClasseName> Identifier{get;set;}
It declares the Identifier
property of type IEnumerable<ClasseName>
which is an interface.
5) What is main use of
public List<ClasseName> Identifier{get;set;}
It declares the Identifier
property of the specific List<ClasseName>
type.
Upvotes: 2