本文整理汇总了C#中System.Object.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# Object.ToString方法的具体用法?C# Object.ToString怎么用?C# Object.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了Object.ToString方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using Examples;
namespace Examples
{
public class Object1
{
}
}
public class Example
{
public static void Main()
{
object obj1 = new Object1();
Console.WriteLine(obj1.ToString());
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:19,代码来源:Object.ToString 输出:
Examples.Object1
示例2: Object2
//引入命名空间
using System;
public class Object2
{
private object value;
public Object2(object value)
{
this.value = value;
}
public override string ToString()
{
return base.ToString() + ": " + value.ToString();
}
}
public class Example
{
public static void Main()
{
Object2 obj2 = new Object2('a');
Console.WriteLine(obj2.ToString());
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:26,代码来源:Object.ToString 输出:
Object2: a
示例3: Automobile
//引入命名空间
using System;
public class Automobile
{
private int _doors;
private string _cylinders;
private int _year;
private string _model;
public Automobile(string model, int year , int doors,
string cylinders)
{
_model = model;
_year = year;
_doors = doors;
_cylinders = cylinders;
}
public int Doors
{ get { return _doors; } }
public string Model
{ get { return _model; } }
public int Year
{ get { return _year; } }
public string Cylinders
{ get { return _cylinders; } }
public override string ToString()
{
return ToString("G");
}
public string ToString(string fmt)
{
if (string.IsNullOrEmpty(fmt))
fmt = "G";
switch (fmt.ToUpperInvariant())
{
case "G":
return string.Format("{0} {1}", _year, _model);
case "D":
return string.Format("{0} {1}, {2} dr.",
_year, _model, _doors);
case "C":
return string.Format("{0} {1}, {2}",
_year, _model, _cylinders);
case "A":
return string.Format("{0} {1}, {2} dr. {3}",
_year, _model, _doors, _cylinders);
default:
string msg = string.Format("'{0}' is an invalid format string",
fmt);
throw new ArgumentException(msg);
}
}
}
public class Example
{
public static void Main()
{
var auto = new Automobile("Lynx", 2016, 4, "V8");
Console.WriteLine(auto.ToString());
Console.WriteLine(auto.ToString("A"));
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:71,代码来源:Object.ToString 输出:
2016 Lynx
2016 Lynx, 4 dr. V8
示例4: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] cultureNames = { "en-US", "en-GB", "fr-FR",
"hr-HR", "ja-JP" };
Decimal value = 1603.49m;
foreach (var cultureName in cultureNames) {
CultureInfo culture = new CultureInfo(cultureName);
Console.WriteLine("{0}: {1}", culture.Name,
value.ToString("C2", culture));
}
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:18,代码来源:Object.ToString 输出:
en-US: $1,603.49
en-GB: £1,603.49
fr-FR: 1 603,49 €
hr-HR: 1.603,49 kn
ja-JP: ¥1,603.49
示例5:
int[] values = { 1, 2, 4, 8, 16, 32, 64, 128 };
Console.WriteLine(values.ToString());
List<int> list = new List<int>(values);
Console.WriteLine(list.ToString());
开发者ID:.NET开发者,项目名称:System,代码行数:5,代码来源:Object.ToString 输出:
System.Int32[]
System.Collections.Generic.List`1[System.Int32]
示例6: CList
//引入命名空间
using System;
using System.Collections.Generic;
public class CList<T> : List<T>
{
public CList(IEnumerable<T> collection) : base(collection)
{ }
public CList() : base()
{}
public override string ToString()
{
string retVal = string.Empty;
foreach (T item in this) {
if (string.IsNullOrEmpty(retVal))
retVal += item.ToString();
else
retVal += string.Format(", {0}", item);
}
return retVal;
}
}
public class Example
{
public static void Main()
{
var list2 = new CList<int>();
list2.Add(1000);
list2.Add(2000);
Console.WriteLine(list2.ToString());
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:35,代码来源:Object.ToString 输出:
1000, 2000
示例7: foreach
//引入命名空间
using System;
using System.Collections.Generic;
public static class StringExtensions
{
public static string ToString2<T>(this List<T> l)
{
string retVal = string.Empty;
foreach (T item in l)
retVal += string.Format("{0}{1}", string.IsNullOrEmpty(retVal) ?
"" : ", ",
item);
return string.IsNullOrEmpty(retVal) ? "{}" : "{ " + retVal + " }";
}
public static string ToString<T>(this List<T> l, string fmt)
{
string retVal = string.Empty;
foreach (T item in l) {
IFormattable ifmt = item as IFormattable;
if (ifmt != null)
retVal += string.Format("{0}{1}",
string.IsNullOrEmpty(retVal) ?
"" : ", ", ifmt.ToString(fmt, null));
else
retVal += ToString2(l);
}
return string.IsNullOrEmpty(retVal) ? "{}" : "{ " + retVal + " }";
}
}
public class Example
{
public static void Main()
{
List<int> list = new List<int>();
list.Add(1000);
list.Add(2000);
Console.WriteLine(list.ToString2());
Console.WriteLine(list.ToString("N0"));
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:43,代码来源:Object.ToString 输出:
{ 1000, 2000 }
{ 1,000, 2,000 }
注:本文中的System.Object.ToString方法示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论