Code special to MVC
-
Further to Dave's answer, see: Object and Collection Initializers (C# Programming Guide)[^] How to: Initialize Objects by Using an Object Initializer (C# Programming Guide)[^] Since you're not passing any constructor parameters, you don't even need the empty parentheses after the type name:
var movie = new Movie {
Name = "Shrek!"
};
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I think adding the "Seriously, do it" was unnecessary, like I was some idiot. You could have only said "please read this" or something.
larsp777 wrote:
I think adding the "Seriously, do it" was unnecessary, like I was some idiot.
Lighten up, Francis. I hate to tell you this, but that's part of my signature. It shows up on everything I post here. Why do I have that link in there? Because over the 13 years I've been around here I've watched the quality of questions plummet. There are so many "questions" asked around here that don't qualify as questions at all and usually don't have any context to frame the question nor enough detail to answer them. Asking questions really is a skill. As you are/were a teacher: Hi! I am a teaches a course in C# for beginners. you should already know this.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
As I said I misunderstood. It was the signature, not aimed at me. Yes, he did answer my question and thanks for the further clarification.
I suggest you revoke your down-vote on Dave's message by clicking the green arrow next to it. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
larsp777 wrote:
I think adding the "Seriously, do it" was unnecessary, like I was some idiot.
Lighten up, Francis. I hate to tell you this, but that's part of my signature. It shows up on everything I post here. Why do I have that link in there? Because over the 13 years I've been around here I've watched the quality of questions plummet. There are so many "questions" asked around here that don't qualify as questions at all and usually don't have any context to frame the question nor enough detail to answer them. Asking questions really is a skill. As you are/were a teacher: Hi! I am a teaches a course in C# for beginners. you should already know this.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
I suggest you revoke your down-vote on Dave's message by clicking the green arrow next to it. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Further to Dave's answer, see: Object and Collection Initializers (C# Programming Guide)[^] How to: Initialize Objects by Using an Object Initializer (C# Programming Guide)[^] Since you're not passing any constructor parameters, you don't even need the empty parentheses after the type name:
var movie = new Movie {
Name = "Shrek!"
};
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
No problem.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
Hm, so when do you use this? Feels like everything I thought my students about private variables and getters and setters is obsolete... Here´s something to put in my signature. The more I learn the more I realize how little I know...
It doesn't replace the property stuff at all. In fact, this syntax uses those properties. The syntax change is just "shortcut sugar" to avoid having to new up the type and then add a bunch of code to set the properties in multiple statements.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
Hm, so when do you use this? Feels like everything I thought my students about private variables and getters and setters is obsolete... Here´s something to put in my signature. The more I learn the more I realize how little I know...
It's just syntactic sugar designed to reduce the amount of code you type. It doesn't render any of your previous knowledge obsolete, it's just that it's easier to read something like this:
Movie movie = new Movie
{
Name = "Batman vs Superman",
ReleaseYear = 2016,
Rating = 4.5
}Than it is to read this:
Movie movie = new Movie();
movie.Name = "Batman vs Superman";
movie.ReleaseYear = 2016;
movie.Rating = 4.5;It also means you don't have to provide multiple overloaded constructors if you're just providing properties that you want to populate. I don't want to blow your mind, but you can extend this concept to also include collection initialisation as well. Suppose you added a list of actors to your class, you can initialise it like this:
Movie movie = new Movie
{
Name = "Batman vs Superman",
ReleaseYear = 2016,
Rating = 4.5,
Actors = new List<string> { "Ben Afleck", "Jessie Eisenberg", "Gal Gadot", "Henry Cavill" }
};This space for rent
-
Saw this in an example when setting properties. Is this some kind of special ASP.NET MVC syntax?
var movie = new Movie() {
Name = "Shrek!"
};Where Name is a property of the class Movie.
No; It's already available in c# language called as object initializer. for eg :- Student objstd=new Student(){ Name ="Surya", roll=112, Adress="Hyd" }; So it's available in net framework version 3.0 on word.