C#中的数据类型

目录

通用类型系统

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

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

int int_value = 101;

  	//调用*int_value*的比较方法与整型*2*进行进行比较
int_value.CompareTo(2); //在控制台输出
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定义

public struct LORect
{
private float x;
private float y;
private float width;
private float height;
}

构造函数

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

属性

    public float X{
set{
this.x = value;
}
get{
return this.x;
}
}

定义变量

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

枚举

关键字enum定义

public enum LOControlType
{
LOControlTypeNormal = 0,
LOControlTypeHighlight = 1,
LOControlTypeDisable = 2,
}

定义变量

LOControlType type = LOControlType.LOControlTypeNormal;

父类

关键字class定义

public class LOPerson
{
private string name;
private int age;
}

构造函数

  • public LOPerson(){}

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

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

  • 多个参数的构造函数

    public LOPerson(string name,int age)
    {
    this.name = name;
    this.age = age;
    }

自定义函数

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

析构函数

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

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

子类

关键字class定义

  public class LOStudent:LOPerson
{
private float score;
}

构造函数

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

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

自定义函数

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

析构函数

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

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

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

    • 不需要特别语法指名

特性

关键字Attribute定义

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

特性的用法

  public class LOPeople
{
[LOType(Type=“NoHealthy”)]
public string hobby{set;get;}
}

获取特性的值

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

反射

命名空间

using System.Reflection;

获取Type

  LOPeople people = new LOPeople();
people.hobby = “Smoke”; 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表达式赋值

  dataSource = (List<int> data_list)=>{
foreach (int item in data_list)
{
Console.WriteLine (item.ToString());
}
} ;

函数地址赋值

  void ProcessData(List<int> data_list)
{
foreach (int item in data_list)
{
Console.WriteLine (item.ToString());
}
}
  • dataSource = new LODataSource(ProcessData);

  • dataSource = ProcessData;

接口

关键字interface

  public interface LOInterface{
void SayNice();
}

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

接口的用法

  public class LOTeacher:LOInterface{
private string name; public void SayNice()
{
Console.WriteLine (this.name + “: Nice”);
}
}
  public class LOManager:LOInterface{
private string name; public void SayNice()
{
Console.WriteLine (this.name + “: Nice”);
}
}

接口的好处

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

[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. kafka和flume的对比

    摘要: (1)kafka和flume都是日志系统.kafka是分布式消息中间件,自带存储,提供push和pull存取数据功能.flume分为agent(数据采集器),collector(数据简单处理和 ...

  2. hdu 1394 Minimum Inversion Number(这道题改日我要用线段树再做一次哟~)

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

  3. nginx 安装三方包重新编译

    sudo -sapt-get source nginxapt-get build-dep nginxwget 'https://github.com/agentzh/chunkin-nginx-mod ...

  4. asp.net控件ControlToValidate同OnClientClick冲突解决办法

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Error ...

  5. to_date()与to_char()

    1.以时间(Date类型)为查询条件时,可以用to_date函数实现: select t.* from D101 t where t.d101_40 = to_date('2013/9/12', 'y ...

  6. nginx libpcre.so.1: cannot open shared object file

    linux 64位安装nginx后启动出错报以下错误 1 2 3 [root@localhost nginx-1.3.0]# /usr/local/nginx/sbin/nginx error whi ...

  7. linux下MMC/SD/SDIO驱动系列之二 ---- host注册过程(一)

    参考了 http://blog.csdn.net/xieweihua2012/article/details/12844733 在他的基础上更详细的解析源 ...................... ...

  8. virt

    www.itwhy.org/linux/debian7-%E5%AE%89%E8%A3%85-kvm-%E8%99%9A%E6%8B%9F%E6%9C%BA.html www.storageonlin ...

  9. 【servlet】客户端是否可以访问到WEB-INF下的jsp文件

    一般情况下(不考虑出现安全问题被入侵,那样啥都能访问到),WEB-INF下的jsp文件单凭浏览器端请求时访问不到的. 想访问的话需要通过服务端servlet的转发. 下面通过转发和重定向的尝试来观察访 ...

  10. Centos 6.4下使用VSFTPD无法正常连接与无法上传文件的问题解决

    发表于 2014 年 4 月 13 日 作者 SCKA 最近利用Linux搭建服务器 搭建FTP的时候决定使用VSFTP搭建,结果却出现了无法正常连接与无法上传文件等诸多问题 经过许久的努力,终于让V ...