C# 对象和类

对象:是对客观事物的抽象
类 : 是对对象的抽象
类和对象是面向对象编程的一个重要组成,类是对象的模板,对象是类的实例。

类和结构的区别
   1.关键字异同:类使用class,结构使用
   2.存储地址不同:类存储在托管堆属于引用类型,结构属于值类型,存储在栈中。
   3.  继承异同:类可以继承类或接口,结构不能继承类或结构只能继承接口
   4.  构造函数不同:结构无论如何系统都会为其创建一个默认无参构造函数,所以在结构中定义无参构造函数是错误的。
   5.  字段初始值:结构体中不能有字段的初始值,只能在结构体中定义有参构造函数为字段初始化
   6.  析构函数:只有类类型才有析构函数,结构没有析构函数、
弱引用

强引用:在应用程序代码内实例化一个类或结构时,只要代码引用,都会形成强引用。在对象的作用域类,垃圾回收器都不会回收对象使用的内存。

弱引用:只要GC运行都会回收对象并释放内存。

什么时候选择弱引用?
当对象很大,很少被访问,并且可以在任意时候进行释放对象内存时,可以选择弱引用。
弱引用只要GC运行就会被释放,往往会使程序产生一些BUG,所以使用弱引用前先判定引用是否还存在。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication20
{
    class Program
    {
        static void Main(string[] args)
        {
            WeakReference mathref = new WeakReference(new MathTest());
            MathTest math;
            if (mathref.IsAlive)
            {
                math = mathref.Target as MathTest;
                math.value = 10;
                Console.WriteLine(math.value);
            }
            else
            {
                Console.WriteLine("Reference is not available.");
            }
            GC.Collect();
            if (mathref.IsAlive)
            {
                math = mathref.Target as MathTest;
                math.value = 20;
                Console.WriteLine(math.value);
            }
            else
            {
                Console.WriteLine("Reference is not available.");
            }
        }
    }
    class MathTest
    {
        public int value { get; set; }
    }
}

代码中使用,IsAlive属性判断当前对象引用是否已被垃圾回收,如果返回true说明没有被回收,可以通过Target强转为相应的对象。GC.Collect手动既刻执行垃圾回收。这里使用release编译,采用debug模式没有效果

部分类

概念:从名字上来看,部分类就是把类拆分开来,即把一个类放在多个代码文件中。

关键字:使用partial修饰在class前面就可以标识该类是部分类。

使用场景:开发窗体程序winfrom时,经常使用部分类。系统创建设计类,程序员只需要关注另一部分的逻辑实现就行,并行处理很方便。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication20
{
    class Program
    {
        static void Main(string[] args)
        {
            TheBigClass c = new TheBigClass();
            c.MethodOne();
            c.MethodTwo();
            c.MethodBase1();
            c.MethodBase2();
        }
    }
    partial class TheBigClass:TheBigClassBase2
    {
        public void MethodOne()
        {
            Console.WriteLine("MethodOne.");
        }

        public void MethodBase2()
        {
            Console.WriteLine("MethodBase2.");
        }
    }
    partial class TheBigClass:TheBigClassBase1
    {
        public void MethodTwo()
        {
            Console.WriteLine("MethodTwo.");
        }
        public void MethodBase1()
        {
            Console.WriteLine("MethodBase1.");
        }
    }
    interface TheBigClassBase1
    {
        void MethodBase1();
    }
    interface TheBigClassBase2
    {
        void MethodBase2();
    }
}

代码中虽然有用到三个class和两个interface,其实我们通过IL反编译,生成的IL代码只有两个类和两个接口
这里写图片描述

扩展方法

概念: 可以在不破坏源代码的情况下对类进行扩展。

使用场景:如果一个程序集,并没有源代码或者在某种特殊情况,源码不能做任何改变,但是此时又必须要往类中新增一个方法才能满足业务需求,这时候可以使用扩展方法

语法:扩展方法必须是静态的,方法第一个参数需要以this标识,后面跟上需要扩展的类类型。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication20
{
    class Program
    {
        static void Main(string[] args)
        {
            MathTest math = new MathTest(10);
            math.printf();
            math.AddValue(20);
            math.printf();
        }

    }
    static class Extension
    {
        public static void AddValue(this MathTest math, int value)
        {
            math.value += value;
        }
    }
    class MathTest
    {
        public int value { get; set; }
        public MathTest(int value)
        {
            this.value = value;
        }
        public void printf()
        {
            Console.WriteLine(value);
        }
    }
}
小结
  1. 类和结构的区别?(关键字不同,继承异同,存储地址不同,构造函数不同,析构函数区别,字段初始值区别)
  2. 弱引用的基本概念和使用场景以及和强引用的区别?
  3. 部分类的基本概念和使用?
  4. 扩展方法的基本概念和使用?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值