C#结构体特性实例分析

所属分类: 软件编程 / C#教程 阅读数: 44
收藏 0 赞 0 分享

本文实例讲述了C#结构体特性。分享给大家供大家参考。具体如下:

结构体的定义:

结构体也可以象类一样可以单独定义.

class a{};
struct a{};

结构体也可以在名字前面加入控制访问符.

public struct student{};
internal struct student{};

如果结构体student没有publice或者internal的声明 类program就无法使用student结构定义 obj对象

如果结构体student的元素没有public的声明,对象obj就无法调用元素x
因为默认的结构体名和元素名是*******类型

程序:

using System;
public struct student
{
  public int x;
};
class program
{
 public static void Main()
 {
  student obj=new student();
  obj.x=100;  
 }
};

在结构体中也可以定义静态成员与类中一样,使用时必须用类名,或结构名来调用不属于实例,声明时直接定义.

程序:

using System;
public struct student
{
  public static int a = 10;
};
class exe
{
 public static void Main()
 {
  Console.WriteLine( student.a = 100);
 }
};

或:

using System;
class base
{
 public struct student
 {
  public static int a = 10;
 };
}
class exe
{
 public static void Main()
 {
  Console.WriteLine( base.student.a = 100);
 }
};

在结构体中可以定义构造函数以初始化成员,但不可以重写默认无参构造函数和默认无参析构函数

程序:

public struct student
{
  public int x;
  public int y;
  public static int z;
  public student(int a,int b,int c)
  {
   x=a;
   y=b;
   student.z=c;
  }
};

在结构体中可以定义成员函数。

程序:

public struct student
{
  public void list()
  {
   Console.WriteLine("这是构造的函数");
  }
};

结构体的对象使用new运算符创建(obj)也可以直接创建单个元素赋值(obj2)这是与类不同的因为类只能使用new创建对象

程序:

public struct student
{
  public int x;
  public int y;
  public static int z;
  public student(int a,int b,int c)
  {
   x=a;
   y=b;
   student.z=c;
  }
};
class program
{
 public static void Main()
 {
 student obj=new student(100,200,300);
 student obj2;
 obj2.x=100;
 obj2.y=200;
 student.z=300;
 }
}

在使用类对象和函数使用时,使用的是引用传递,所以字段改变
在使用结构对象和函数使用时,是用的是值传递,所以字段没有改变

程序:

using System;
class class_wsy
{
 public int x;
}
struct struct_wsy
{
 public int x;
}
class program
{
 public static void class_t(class_wsy obj)
 {
  obj.x = 90;
 }
 public static void struct_t(struct_wsy obj)
 {
  obj.x = 90;
 }
 public static void Main()
 {
  class_wsy obj_1 = new class_wsy();
  struct_wsy obj_2 = new struct_wsy();
  obj_1.x = 100;
  obj_2.x = 100;
  class_t(obj_1);
  struct_t(obj_2);
  Console.WriteLine("class_wsy obj_1.x={0}",obj_1.x);
  Console.WriteLine("struct_wsy obj_2.x={0}",obj_2.x);
  Console.Read();
 }
}

结果为:

class_wsy obj_1.x=90
struct_wsy obj_2.x=100

希望本文所述对大家的C#程序设计有所帮助。

更多精彩内容其他人还在看

C#使用oledb读取excel表格内容到datatable的方法

这篇文章主要介绍了C#使用oledb读取excel表格内容到datatable的方法,涉及C#操作oledb及datatable的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用oledb操作excel文件的方法

这篇文章主要介绍了C#使用oledb操作excel文件的方法,涉及C#中oledb操作excel的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用IHttpModule接口修改http输出的方法

这篇文章主要介绍了C#使用IHttpModule接口修改http输出的方法,涉及C#操作IHttpModule接口的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#给图片加水印的简单实现方法

这篇文章主要介绍了C#给图片加水印的简单实现方法,涉及C#操作图片的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#生成随机数的方法小结

这篇文章主要介绍了C#生成随机数的方法,实例总结了C#生成随机数的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用jQuery实现无刷新评论提交的方法

这篇文章主要介绍了C#使用jQuery实现无刷新评论提交的方法,涉及C#结合jQuery进行Ajax操作的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#读取中文文件出现乱码的解决方法

这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像对比度调整的方法

这篇文章主要介绍了C#图像对比度调整的方法,涉及C#实现图像对比度操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像灰度级拉伸的方法

这篇文章主要介绍了C#图像灰度级拉伸的方法,涉及C#灰度操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像线性变换的方法

这篇文章主要介绍了C#图像线性变换的方法,涉及C#操作图像线性变换的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多