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. tsung: an open-source multi-protocol distributed load testing tool

     ROPERTIES::type: KnowledgeBase_Cloud:END: 开源.多协议.分布式的压力测试工具   Item Summary tsung-recorder start 通过p ...

  2. Google 分布式关系型数据库 F1

    F1是Google开发的分布式关系型数据库,主要服务于Google的广告系统.Google的广告系统以前使用MySQL,广告系统的用户经常需要使用复杂的query和join操作,这就需要设计shard ...

  3. hh monitor

    http://theholyjava.wordpress.com/2012/09/21/enabling-jmx-monitoring-for-hadoop-and-hive/ http://blog ...

  4. WPF之让ListView中的CheckBox居中显示

    第一步:在资源中定义一个居中的样式: <Window.Resources> <Style x:Key="ListViewItemStyle" TargetType ...

  5. MySQL、SQLServer2000(及SQLServer2005)和ORCALE三种数据库实现分页查询的方法

    在这里主要讲解一下MySQL.SQLServer2000(及SQLServer2005)和ORCALE三种数据库实现分页查询的方法. 可能会有人说这些网上都有,但我的主要目的是把这些知识通过我实际的应 ...

  6. mysql主从数据库

    Mysql主从配置,实现读写分离 大型网站为了软解大量的并发访问,除了在网站实现分布式负载均衡,远远不够.到了数据业务层.数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器扛,如此多的数据库 ...

  7. dfs手写栈模板

    在竞赛中如果系统栈很小的话,过深的递归会让栈溢出,这个时候我们就要自己手写栈,将递归转化成手工栈. 方法其实也很简单. 基本思路上,我们就是用栈不断的pop,push.但是何时push,何时pop呢? ...

  8. jsp base标签与meta标签学习小结

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  9. 字段为空sql语句,设置当前模式

    delete from t_corpinfo  where CORPID='' and CORPNAME=''  该命令是删除字段为空的记录 SET CURRENT SCHEMA DB2INST1;

  10. 在windows系统用odbc连接

    当连接的数据出现失败时,出现数据库别名仍然存在,但还是要用这个别名重新建立连接 在windows客户端,用输入db2cmd输入c:\Users\yexuxia>db2 list db direc ...