[英]return default ToString() for a class instance c#
I have a class named Person 我有一堂人
public class Person
{
string name;
int age;
SampleObject(string name, int age)
{
this.name = name;
this.age = age;
}
public override string ToString()
{
string s = age.ToString();
return "Person: " + name + " " + s;
}
}
I have overridden the ToString() to return the name of the person. 我重写了ToString()以返回该人的名字。
I am using the class in another class: 我正在另一堂课中使用该课:
public class MyClass
{
public int Id {get;set;}
public Person person {get;set;}
}
Now, I want to access the class as 现在,我想以以下方式访问该类
MyClass my = new MyClass();
I want that when I execute my.person
, it should return the ToString() value of the person class without explicitly calling the my.person.ToString()
我希望在执行
my.person
,它应返回person类的ToString()值,而无需显式调用my.person.ToString()
Is it possible and if possible, how can I make this happen. 是否有可能,如果可能的话,我该如何做到这一点。
Thanks 谢谢
You can create another readonly property 您可以创建另一个只读属性
public string PersonName { get {return this.person.ToString();} }
Or add checking for possible null 或添加检查是否可能为空
public string PersonName
{
get
{
return (this.person == null) ? String.Empty : this.person.ToString();
}
}
Based on your comment about setting Name
of person by same property 基于您对通过相同属性设置
Name
的评论
I think approach with separated/specific property will be more maintainable 我认为具有分离/特定属性的方法将更易于维护
public string PersonName
{
get
{
return (this.person == null) ? String.Empty : this.person.ToString();
}
set
{
if(this.person == null)
{
this.person = new Person(value, 0);
}
else
{
this.person.Name = value;
}
}
}
I don't understand your question very well. 我不太了解您的问题。 but one of solutions to your question is to use implicit cast .
但解决您的问题的方法之一是使用隐式强制转换 。
public class Person
{
string name;
int age;
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public override string ToString()
{
string s = age.ToString();
return "Person: " + name + " " + s;
}
// here
public static implicit operator string(Person d)
{
return d.ToString();
}
}
public class MyClass
{
public int Id { get; set; }
public Person Person { get; set; }
}
static void Main(string[] args)
{
var myclass = new MyClass();
myclass.Person = new Person("test", 12);
// use like this
string name = myclass.Person;
Console.WriteLine(name);
Console.WriteLine("Press any key to continue.");
Console.ReadLine();
}
You can achieve this with operator overload. 您可以通过运算符重载来实现。
public static implicit operator string(Person p)
{
return p.ToString();
}
If you wish you can implement operator overload in MyClass to so it calls person as well. 如果您希望可以在MyClass中实现操作符重载,以便它也可以调用person。
public static implicit operator string(MyClass my)
{
return my.person;
}
With this you can do something like 有了这个你可以做类似的事情
string personString = my;
// or my.person if you only implement it for Person
Also you don't need to have a string variable for the age. 另外,您不需要年龄的字符串变量。 Doing the following will work just fine.
进行以下操作就可以了。
return "Person: " + name + " " + age;
I would suggest to use string.Format though. 我建议虽然使用string.Format。
return string.Format("Person: {0} {1}", name, age);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.