hey guys i was creating indexer and i had some issue i didnt got any compile or runtime error but there was no out put
here is that coding tell me where i made wrong coding.
using System;
namespace ConsoleApplication13
{
class employee
{
public string name;
public string branch;
public int ID;
}
class employees
{
employee[] empl=new employee[3];
public employee this[int x]
{
get
{
switch(x)
{
case 0: empl[0] = new employee();
return empl[0];
case 1: empl[1] = new employee();
return empl[1];
case 2: empl[2] = new employee();
return empl[2];
}
return null;
}
}
}
class Program
{
static void Main(string[] args)
{
employees e=new employees();
e[0].name="prashant";
string a=e[0].name;
Console.WriteLine("{0}",a);
Console.ReadLine();
}
}
}
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Aug 3, 2014, 10:19 AM
Consequently, the name property for e[0] won't have been set because it's a different object you're getting to the one whose name property was set originally.
If you add the highlighted code, it will work OK: