欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

C#結構體特性實例分析

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

本文實例講述了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;  
 }
};

在結構體中也可以定義靜態(tài)成員與類中一樣,使用時必須用類名,或結構名來調用不屬于實例,聲明時直接定義.

程序:

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);
 }
};

在結構體中可以定義構造函數(shù)以初始化成員,但不可以重寫默認無參構造函數(shù)和默認無參析構函數(shù)

程序:

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;
  }
};

在結構體中可以定義成員函數(shù)。

程序:

public struct student
{
  public void list()
  {
   Console.WriteLine("這是構造的函數(shù)");
  }
};

結構體的對象使用new運算符創(chuàng)建(obj)也可以直接創(chuàng)建單個元素賦值(obj2)這是與類不同的因為類只能使用new創(chuàng)建對象

程序:

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;
 }
}

在使用類對象和函數(shù)使用時,使用的是引用傳遞,所以字段改變
在使用結構對象和函數(shù)使用時,是用的是值傳遞,所以字段沒有改變

程序:

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#基于UDP實現(xiàn)的P2P語音聊天工具

欄    目:C#教程

下一篇:C#中實現(xiàn)一次執(zhí)行多條帶GO的sql語句實例

本文標題:C#結構體特性實例分析

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6939.html

網頁制作CMS教程網絡編程軟件編程腳本語言數(shù)據庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網 版權所有