I went through an interview and interviewer asked what will print if I have loop through the object array, where this array contains 3 values in first index array[0] it has string then arr[1] integer and third arr[2] is class type value.
Let me give you an example with c# console application program.
As you have noticed the output for third array is like "NamespaceName.ClassName".
Here you won't be able to get the id or name from that object. If you want to get the name from that object in this case please see below example.
public class Employee {
public int Id { get; set; }
public string Name { get; set; }
// By default you can override 3 method in any custom class named ( 1. Equals() 2. GetHasCode() and 3. ToString() )
// I have to use override the ToString() method to get the name from this class.
public override string ToString() {
return this.Name;
}
}
Now well be able to fetch the name from the custom class object (Employee class).
Let me give you an example with c# console application program.
As you have noticed the output for third array is like "NamespaceName.ClassName".
Here you won't be able to get the id or name from that object. If you want to get the name from that object in this case please see below example.
public static class PracticalExample1 {
public static void Method1() {
var obj = new object[3];
obj[0] = 1;
obj[1] = "Ramesh";
obj[2] = new Employee {
Name = "Vijay"
};
foreach (var o in obj) {
Console.WriteLine(o);
}
} }
public static void Method1() {
var obj = new object[3];
obj[0] = 1;
obj[1] = "Ramesh";
obj[2] = new Employee {
Name = "Vijay"
};
foreach (var o in obj) {
Console.WriteLine(o);
}
} }
public int Id { get; set; }
public string Name { get; set; }
// By default you can override 3 method in any custom class named ( 1. Equals() 2. GetHasCode() and 3. ToString() )
// I have to use override the ToString() method to get the name from this class.
public override string ToString() {
return this.Name;
}
}
Now well be able to fetch the name from the custom class object (Employee class).