When you use the new
syntax, you are essentially calling a special method called a constructor. A constructor always has the same name as the class and never has a return type (not even void). Otherwise, you write it like an ordinary method (with certain enhanced abilities). So for example
class Student
{
public Student(string name)
{
Console.WriteLine(name);
}
}
If you then instantiate a Student, the constructor runs:
var s = new Student("Joe"); //Outputs "Joe"
If you would like to accept two names, you could of course add a constructor with two arguments:
class Student
{
public Student(string name1, string name2)
{
//Implementation
}
}
Or if you want a flexible number of arguments, you can use the params keyword:
class Student
{
public Student(params string[] name)
{
//Implementation
}
}
Although it looks like an array to the constructor, the caller can supply a list of arguments as if they were separate parameters. The compiler will convert the parameter list into an array.
var s = new Student("John","Dick","Harry");
On the other hand, this whole approach doesn't make sense to me. A student has only one name. Maybe you need a list of students, like this:
class Student
{
public string Name { get; set; }
public Student(string name)
{
this.Name = name;
}
}
var s = new System.Collections.Generic.List<Student>();
s.Add(new Student("John"));
s.Add(new Student("Dick"));
s.Add(new Student("Harry"));
Here is a link to a working example on DotNetFiddle