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. Entity Framework技巧系列之五 - Tip 16 – 19

    提示16. 当前如何模拟.NET 4.0的ObjectSet<T> 背景: 当前要成为一名EF的高级用户,你确实需要熟悉EntitySet.例如,你需要理解EntitySet以便使用 At ...

  2. VMware 全虚拟打开

    1.修改.vmx 文件,最后一行加入: hypervisor.cpuid.v0 = “FALSE" mce.enable = “TRUE" vhv.enable = “TRUE&q ...

  3. textFiled输入字数的控制问题之—把带输入的拼音也判断了

    一个textFiled,控制只能输入五个字,现在你已经输入了四个字,在输入第五个字的时候,输入一个拼音之后就不能输入后一个拼音,这里把拼音也当成字来判断了,这种情况下就需要_textFiled.mar ...

  4. Anton and School

    Anton and School time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. eclipse 终极操作技巧

    eclipse作为一个java开发必备软件,从用户体验来说,还是蛮一般的(按照初始设置的话),所以有必要进行一些设置上的改良,加上对一些好用的快捷键的挖掘,能让你用eclipse更加得心应手,事半功倍 ...

  6. 利用Fiddler抓取手机APP数据包

    Fiddler是一个调试代理,下载地址http://www.telerik.com/download/fiddler 下载安装运行后,查出运行机器的IP,手机连接同一网域内的WIFI,手机WIFI连接 ...

  7. 解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128

    今天做网页到了测试和数据库交互的地方,其中HTML和数据库都是设置成utf-8格式编码,插入到数据库中是正确的,但是当读取出来的时候就会出错,原因就是python的str默认是ascii编码,和uni ...

  8. UVALive 2403 77377解题报告(深搜)

    题意:给你一些固定的字符串,在给出数字,根据键盘的对应关系,输出所有的满足条件的字符串,输出顺序无所谓. 思路:因为题目说了,输出比较小,说明测试数据并不强,所以可以暴力回溯求出答案,将所有的给出的字 ...

  9. Python文件打包成EXE文件

    工具:pyinstaller 安装:pip install pyinstaller 使用: 1 将依赖文件集中到一个文件夹:           pyinstaller -D -w xxx.py    ...

  10. L10,not for jazz

    expressions: It is called a clavichord这被称为古钢琴 a friend of my father's我父亲的朋友   words: musical,adj,音乐的 ...