结构体来实现格式化输出字符串

来源:互联网 发布:淘宝top排行榜 编辑:程序博客网 时间:2024/06/05 09:40

通过结构体来实现,格式化输出字符串,简单理解结构体的方便之处,分享:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication8{    class Program    {        struct order        {            public string itemName;            public int unitCount;            public double unitCost;            public double TotalCost()            {                return unitCount * unitCost;            }            public string Info()            {                return "Order information :" + unitCount.ToString() + " " +                    itemName + " items at $  " + unitCost.ToString() + " each , total cost $ " +                    TotalCost().ToString();            }        }        static void Main(string[] args)        {            order myOrder;            myOrder.itemName = "TestStruct";            myOrder.unitCount = 3;            myOrder.unitCost = 2.1;            Console.WriteLine("通过结构体来格式输出:{0}" , myOrder .Info () );                    }    }}


 

原创粉丝点击