C#中的数据类型

目录

通用类型系统

C#中,变量是值还是引用仅取决于数据类型

所有的数据类型都是对象。因为它们具有自己ide方法和属性

int int_value = 101;

  1. //调用*int_value*的比较方法与整型*2*进行进行比较
  2. int_value.CompareTo(2);
  3. //在控制台输出
  4. Console.WriteLine(int_value.ToString());

值类型

内置值类型

整型

  • sbyte(System.SByte)

  • short(System.Int16)

  • int(System.Int32)

  • long(System.Int64)

  • byte(System.Byte)

  • ushort(System.UInt16)

  • uint(System.UInt32)

  • ulong(System.UInt64)

  • char(System.Char)

浮点型

  • float (System.Single)

  • double(System.Double)

高精度类型

  • decimal(System.Decimal)

bool(System.Boolean)

用户定义的值类型

结构体类型(派生于System.ValueType)

枚举类型结构体类型(派生于System.Enum)

可空类型结构体类型(派生于System.Nullable泛型结构体)

C#的所有值类型均隐式派生自System.ValueType;

判断是否为值类型

Type.IsValueType

引用类型

数组(派生于System.Array)

用户可以自定义的引用类型

类:class(派生于System.Object)

接口:interface

委托类型:delegate(派生于System.Delegate)

字符串:string(System.String的别名)

Lambda表达式

数组类型

值类型数组

int[] int_array = new int[10];

  • 在堆内存中一次初始化10个int类型的存储空间

  • 自动初始化这10个元素

  • 将10个元素存储到刚刚分配的内存空间内

引用类型数组

object[] obj_array = new object[10];

  • 在堆内存中分配一次空间

  • 不会自动初始化任何元素

    • obj_array[i]都是null
  • 当有代码初始化某个元素时,对应元素的存储空间会分配在堆内存上

    • obj_arr[i]=new object();

内存部署

堆内存上存储了所有的引用类型

object item = new objct();

  • new 关键字在堆内存中分配内存空间,并且返回该内存空间的地址

  • item存储分配后返回的内存地址

语法

结构体

关键字struct定义

  1. public struct LORect
  2. {
  3. private float x;
  4. private float y;
  5. private float width;
  6. private float height;
  7. }

构造函数

  1. public LORect(float x,float y,float width,float height)
  2. {
  3. this.x = x;
  4. this.y = y;
  5. this.width = width;
  6. this.height = height;
  7. }
  • 不允许重载无参构造函数

属性

  1. public float X{
  2. set{
  3. this.x = value;
  4. }
  5. get{
  6. return this.x;
  7. }
  8. }

定义变量

LORect frame = new LORect(0f,0f,100f,100f);

枚举

关键字enum定义

  1. public enum LOControlType
  2. {
  3. LOControlTypeNormal = 0,
  4. LOControlTypeHighlight = 1,
  5. LOControlTypeDisable = 2,
  6. }

定义变量

LOControlType type = LOControlType.LOControlTypeNormal;

父类

关键字class定义

  1. public class LOPerson
  2. {
  3. private string name;
  4. private int age;
  5. }

构造函数

  • public LOPerson(){}

  • public LOPerson(string name){this.name = name;}

  • public LOPerson(string age){this.age = age;}

  • 多个参数的构造函数

    1. public LOPerson(string name,int age)
    2. {
    3. this.name = name;
    4. this.age = age;
    5. }

自定义函数

  1. public void SayHi()
  2. {
  3. Console.WriteLine (this.name + “: Hello”);
  4. }

析构函数

  1. ~LOPerson()
  2. {
  3. this.name = null;
  4. this.age = 0;
  5. }
  • 在析构函数中,将引用类型成员变量置为null,内存处理

  • 在析构函数中,将值类型成员变量置为默认值,程序逻辑安全

子类

关键字class定义

  1. public class LOStudent:LOPerson
  2. {
  3. private float score;
  4. }

构造函数

  • 基于父类无参的构造函数
  1. public LOStudent(float score):base()
  2. {
  3. this.score = score;
  4. }
  • public LOStudent(int age):base(age){}

  • 在构造函数的继承中,都会先调用父类的构造函数

自定义函数

  1. public void SayHello()
  2. {
  3. base.SayHi();
  4. Console.WriteLine (“this.SayHi”);
  5. }
  • 在自定义函数中,调用父类的某个函数要用到base关键字

析构函数

  1. ~LOStudent()
  2. {
  3. this.score = 0f;
  4. }
  • 子类和父类的析构函数的执行顺序

    • 1.自动调用子类的析构函数

    • 2.自动调用父类的析构函数

    • 不需要特别语法指名

特性

关键字Attribute定义

  1. [AttributeUsage(AttributeTargets.Property)]
  2. public class LOTypeAttribute:Attribute
  3. {
  4. public string Type{set;get;}
  5. }

特性的用法

  1. public class LOPeople
  2. {
  3. [LOType(Type=“NoHealthy”)]
  4. public string hobby{set;get;}
  5. }

获取特性的值

  1. PropertyInfo item = property_list[0];
  2. LOTypeAttribute attribute = (LOTypeAttribute)Attribute.GetCustomAttribute(item,typeof(LOTypeAttribute));
  3. Console.WriteLine (attribute.Type);

反射

命名空间

using System.Reflection;

获取Type

  1. LOPeople people = new LOPeople();
  2. people.hobby = Smoke”;
  3. Type p_type = people.GetType();

属性(PropertyInfo)

  • 获取属性列表

    • PropertyInfo[] property_list = p_type.GetProperties();
  • 获取指定属性

    • PropertyInfo property = p_type.GetProperty(“hobby”);
  • 方法

    • 获取people对象的属性值 .GetValue()

      • property.GetValue(people,null);
    • 设置people对象的属性值 .SetValue()

      • property.SetValue(people,”Drink”,null);

方法(MethodInfo)

  • 获取方法列表

    • MethodInfo[] method_list = p_type.GetMethods();
  • 获取指定方法

    • MethodInfo method = p_type.GetMethod(“SayHi”);
  • 方法调用

    • Invoke()
  • 方法的参数列表

    • method.GetParameters();
  • 方法的返回值

    • method.ReturnType;

成员(MemberInfo)

  • 获取成员列表

    • MemberInfo member_list = p_type.GetMembers();
  • 获取指定成员

    • MemberInfo[] member= p_type.GetMember(“name”);

构造函数(ConstructorInfo)

  • 获取构造函数列表

    • ConstructorInfo[] constructor_list = p_type.GetConstructors();
  • 获取指定构造函数

    • ConstructorInfo constructor = p_type.GetConstructor(new Type[]{typeof(int)});

委托

关键字delegate

public delegate void LODataSource(List<int> data_list);

定义委托类型变量

LODataSource dataSource;

委托类型变量赋值

Lambda表达式赋值

  1. dataSource = (List<int> data_list)=>{
  2. foreach (int item in data_list)
  3. {
  4. Console.WriteLine (item.ToString());
  5. }
  6. } ;

函数地址赋值

  1. void ProcessData(List<int> data_list)
  2. {
  3. foreach (int item in data_list)
  4. {
  5. Console.WriteLine (item.ToString());
  6. }
  7. }
  • dataSource = new LODataSource(ProcessData);

  • dataSource = ProcessData;

接口

关键字interface

  1. public interface LOInterface{
  2. void SayNice();
  3. }

接口中只能声明函数,不能实现函数

接口的用法

  1. public class LOTeacher:LOInterface{
  2. private string name;
  3. public void SayNice()
  4. {
  5. Console.WriteLine (this.name + “: Nice”);
  6. }
  7. }
  1. public class LOManager:LOInterface{
  2. private string name;
  3. public void SayNice()
  4. {
  5. Console.WriteLine (this.name + “: Nice”);
  6. }
  7. }

接口的好处

为多个不同的类提供相同名称的方法接口,提供不同的实现过程

[Unity]C#.数据类型总结的更多相关文章

  1. C#开发Unity游戏教程之游戏对象的属性变量

    C#开发Unity游戏教程之游戏对象的属性变量 Unity游戏对象的属性——变量 通过对上一章的学习,读者应该了解到了,游戏对象上的属性与脚本中的变量,建立联系的方式就是将脚本赋予游戏对象.上一章只是 ...

  2. 高速上手Unity中最好的补间动画插件DFTween

     出处:http://blog.csdn.net/u010019717 author:孙广东      时间:2015.3.17   23:00 DFTween 是一个在 Unity 游戏引擎中高 ...

  3. Unity数据类型转XML/Json-封装函数直接调用(Chinar)

    Unity将数据直接转XML/Json文件 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar ...

  4. 【Unity Shader】---数据类型和关键字

    一.基本数据类型:Cg支持7种基本的数据类型 1.float,32位浮点数据,一个符号位.浮点数据类型被所有的图形接口支持: 2.half,16位浮点数据: 3.int,32位整形数据 4,fixed ...

  5. C#、Unity 数据类型的默认值

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class Main : M ...

  6. Unity比较常用的数据类型

    几种常见数据结构的使用情景 Array需要处理的元素数量确定并且需要使用下标时可以考虑,不过建议使用List<T> ArrayList不推荐使用,建议用List<T> List ...

  7. Unity C#最佳实践(上)

    本文为<effective c#>的读书笔记,此书类似于大名鼎鼎的<effective c++>,是入门后提高水平的进阶读物,此书提出了50个改进c#代码的原则,但是由于主要针 ...

  8. Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译

    本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...

  9. 玩转Unity资源,对象和序列化(上)

    这是一系列文章中的第二章,覆盖了Unity5的Assets,Resources和资源管理 本文将从Unity编辑器和运行时两个角度出发,主要探讨以下两方面内容:Unity序列化系统内部细节以及Unit ...

随机推荐

  1. LeetCode OJ 111. Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  2. 开发MOSS自定义字段类型

    前段时间,由于刚好项目定制的需要,笔者就开发了几个自定义字段类型.在这抽空做个详细笔记,方便初学者学习.这方面的资料也很多,如果自身觉得不大明白可以参考下SDK和网上的相关文章.本章的目的主要是给新手 ...

  3. find unique values in an array

    Problem: given an array that contains duplicates (except one value), find the one value that does no ...

  4. JavaScript 伪造 Referer 来路方法

    Javascript 是一种由Netscape的LiveScript发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如Perl,遗留的速度问题, ...

  5. ecshop后台管理显示扩展分类

    ecshop 后台商品列表默认只显示分类下的商品,而不显示扩展分类中的商品,以下是我个人给出的解决方法: 打开admin/includes/lib_goods.php 第839行左右的位置 可以看到如 ...

  6. pro文件常用内容

    qmake生成的pro文件中常用变量 SUBDIRS 指定子目录 TARGET 指定生成的应用程序名(默认为项目名) DEPENDPATH 指定程序编译时依赖的相关路径 INCLUDEPATH 指定头 ...

  7. Hibernate关于openSession和getCurrentSession的理解

    来源(转载):http://blog.csdn.net/woshisap/article/details/7024482 1:getCurrentSession会把Session和当前的线程关联起来, ...

  8. THE ROAD TO PROGRAM

    <The C Programming Language> <The Practice of Programming><The Art of Computer Progra ...

  9. oracle数据库的一次异常起停处理。

    在重启数据库的时候,忘记把一个应用关停了,想起来的时候,就ctrl+c,把数据库shutdown immediate 给强制停下了,把该应用再停止,然后shutdown immdiate,这时候数据报 ...

  10. java 数据结构 队列的实现

    java 数据结构队列的代码实现,可以简单的进行入队列和出队列的操作 /** * java数据结构之队列的实现 * 2016/4/27 **/ package cn.Link; import java ...