15、其他类

一、Object类

  Object是所有类的基类。

  Object类中的方法 

  

二、类包含

  包含类型是在另一个对象中作数据成员的对象类型。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class A
    {
        private string val1;   //声明了一个私有变量
        public A(string str)  //定制了一个构造函数
        {
            val1 = str;
        }
        public string Val1  //定义一个属性
        {
            get { return val1; }
            set { val1 = value; }
        }
    }
public class B
    {
        private A bval1;
        public A Bval1
        {
            get { return bval1; }
            set { bval1 = value; }
        }
        public A Geta()
        {
            return new A("测试字符串");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            B b = new B();
            b.Bval1 = new A("参数");
            Console.WriteLine(b.Bval1.Val1);

            A a = b.Geta();
            Console.WriteLine(a.Val1);
            Console.ReadLine();
        }
    }
}

三、类嵌套

  一个类完整的包含了另一个类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
            public class comp
    {
        public string name
            {
            get;
            set;           
            }
        public class cod
        {
            public string use;
            public cod(string name)
            {
                use = name;
            }
            public string Use
            {
                get
                { return use; }
                set
                { use = value; }
            }
            public string codf(string name)
            {
                return "我现在用的是" + name;
            }
            public static string aa(int i)
            {
                return "一共有" + i + "语言";
            }
        }
        
    }
    class Program
    {
        static void Main(string[] args)
        {
            comp.cod cc = new comp.cod("电脑名称");
            Console.WriteLine(cc.use);
            Console.WriteLine(cc.codf("C#"));
            Console.WriteLine(comp.cod.aa(3));
            Console.ReadLine();      
        }
    }
    
}

四、匿名类型

  

var Worker = new { name = "YWJ", age = 30, sex = "" };
            string str=Worker.name;
            int i = Worker.age;
            string str1 = Worker.sex;

五、分部类

  

六、扩展方法

原文地址:https://www.cnblogs.com/zytr/p/14737211.html