现在的位置: 首页 > 综合 > 正文

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

2014年09月05日 ⁄ 综合 ⁄ 共 597字 ⁄ 字号 评论关闭

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

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 () );
            
        }
    }
}

 

抱歉!评论已关闭.