Can someone help me with explaining the following code in c#?

Home » Programming & Design » Can someone help me with explaining the following code in c#?
Programming & Design No Comments

Student student = new Student()

I want to know what every word means, i know its for creating a student object, but how to explain every word?

I would really appreciate it, thanks.

Other answer:

Khan:
This simple code is identical to Java, you miss an ; at the end though.

Student is the class, student is the object, I would've use somewhat whimsical names to both amuse myself and make it more clear, there are schools of programming conventions why you stuck with a Sun/Java one I have not a clue.

What came before = sign means "I have declared a declaration to memorially declare a Student object being referred to by the name of student". At that point, nothing more than that declaration is done and it is barely few bytes in primary RAM.

What's after = is the bigger part, I think Java and C# is identical in this regard too. "new" is the operative word, meaning "I'm acting upon my former declaration to memorially declare a ??? from a chunk of heap, major keyword there, hint-hint-Wikipedia…" Student() is the companion to the operative word that fills the ??? to be "a Student object initialized with default/nothing/empty/identity/blah constructor()" The result is achieving a chunk of Student in a particular chunk of RAM and = glues that back to the student name. So at the end you have "its for creating a student object" there.

husoski:
In C#, "new" is an operator used to create an object. It's takes a type on the right to say what type of object is being created. Additional information may be used to further describe the object–either (a) arguments to a constructor, in parentheses; or (b) the size of an array in [] brackets.

So, "new Student()" creates a new object of type Student, and the empty () parentheses mean that no extra information is provided.

Assuming Student is a class type, the return value from that expression is a reference to the newly-created Student object. The front part of that statement says what is done with that reference; namely, storing it in a variable named "student".

Side note: It's usually a bad idea to have identifiers (like "student" and "Student") that differ only in the case of one or more letters. The compiler won't get confused, but the programmer or another reader might.

david:
Here it is, word by word.

Student

This indicates that we are declaring a variable of type Student.

student

This is the name of the variable.

=

This indicates that we are setting the variable to the following value.

new Student()

This creates a new Student object. If there is a constructor, it is also called here.

So, in all, this declares a variable called student, of type Student, and sets it equal to a new Student object, calling the constructor if it exists.

AJ:
Student is a class. What you have there is creating a variabled called student of a Type called Student and then iniatilized the class.
Q The First Timelord:
seriously. you gotta play with this stuff to figure it out for yourself.

LEAVE A COMMENT