c#基础练习--05 静态类和非静态类,字符串不可变性

从语法结构上看,静态类和非静态类的差别在于:
①静态类只能拥有静态成员。
②非静态类可以有静态成员,也可以有非静态成员。

从含义上看,静态类和非静态类的差别在于
①静态类强调这个类是一种“工具类”,减少了代码的复杂度。
②因为静态类不能创建实列,因此它在整个项目中,可以模仿为“全局类,公用类”,做到资源共享。
③非静态类(仅有非静态成员)是不占内存的,只有实例才占内存,同时会被GC回收;但静态类始终占用内存(放在静态存储区),只有当程序结束后才会释放资源,能提高运行效率。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 字符串不可变性
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = null;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < 100000; i++)
            {
                str += i;
            }
            sw.Stop();
            Console.WriteLine(str);
            Console.WriteLine("总耗时:{0}",sw.Elapsed);
            Console.Read();
        }
    }
}

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 字符串不可变性
{
    class Program
    {
        static void Main(string[] args)
        {
            //string str = null;
            StringBuilder sb = new StringBuilder();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < 100000; i++)
            {
                //  str += i;
                sb.Append(i);//sb没有=+操作,而是append方法
            }
            sw.Stop();
            Console.WriteLine(sb);
            Console.WriteLine("总耗时:{0}",sw.Elapsed);
            Console.Read();
        }
    }
}

在这里插入图片描述
差距在100倍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值