C#类和结构区别的总结(一)

本文通过一个C#示例程序详细介绍了结构体和类的初始化过程,包括默认值、成员变量的赋值及构造函数的使用。文中还对比了结构体与类在初始化方面的差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
int i = new int();
            
string a = " ";
            Console.WriteLine(
"{0}{1}",a,i);

            
int[] b = new int[10];
            Console.WriteLine(b[
3]);

            structOne soInstanceOne 
= new structOne ();
            Console.WriteLine(
"soInstanceOne:{0}:{1}", soInstanceOne.filed1, soInstanceOne.filed2);

            structOne[] soArrayOne 
= new structOne[10];
            Console.WriteLine(
"soArrayOne[]:{0}:{1}",soArrayOne[2].filed1,soArrayOne[2].filed2 );
            soArrayOne[
0].people = new string[4];
            soArrayOne[
0].people[0= "";
            soArrayOne[
0].people[1= "18";
            soArrayOne[
0].people[2= "Jack";
            
foreach (string peopleJack in soArrayOne[0].people)
            {
                Console.WriteLine(peopleJack);
            }

            soInstanceOne.people 
= new string[3];
            soInstanceOne.people[
0= "";
            soInstanceOne.people[
1= "17";
            soInstanceOne.people[
2= "Alice";
            
foreach (string peopleAlice in soInstanceOne.people)
            {
                Console.WriteLine(peopleAlice);
            }


            Console.WriteLine(structOne.filed4);
            soInstanceOne.StructOneMethodOne();


            classOne Test 
= new classOne();
            Console.WriteLine (
"Test:");
            Console.WriteLine (
"{0}:{1}:{2}",Test.cFiled1,Test.cFiled2 ,Test.cFiled3 ); //打印0::false


            Console .ReadLine ();
        }
        
struct structOne
        {
            
public int filed1; //执行这个语句structOne soInstanceOne = new structOne()之后, filed1的值初始化为0
            public string filed2; //执行这个语句structOne soInstanceOne = new structOne()之后, filed2的值初始化为null
            public string[] people;
            
//public sting[] people = { "男", "17", "Jack" };//结构中不能有实例字段初始值设定项,意思就是不能为实例字段赋值
            
//private int filed3 = 3;  //不能有实例字段初始值设定项,意思就是不能为实例字段赋值
            public  static int filed4 = 4//结构中可以为静态字段赋值
            private const string conA = "conA"//结构中可以为常数字段赋值,因为常数属于Static
            private static string strName = "John"//结构中可以为静态字段赋值
            public static string[] anotherPeople = { """17""Tom" }; //结构中可以为静态数组赋值
            public bool bResult;
            
public void StructOneMethodOne()
            {
                Console.WriteLine(conA);
                Console.WriteLine(strName);
                Console.WriteLine(bResult);
            }
        }
        
class classOne
        {
            
public int cFiled1;
            
public string cFiled2;
            
public bool cFiled3;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值