结构体

C# 结构体特性详解
本文探讨了C#中结构体的特性,包括作为值类型的性质、如何直接使用结构体而不需实例化对象,以及结构体成员初始化的重要性。通过具体示例说明了结构体在C#中的使用方式。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 结构体
{
    class Program
    {
        static void Main(string[] args)
        {
            //设置控制台字体颜色  前景色
            Console.ForegroundColor = ConsoleColor.Black;
            Console.BackgroundColor = ConsoleColor.Yellow;



            //不new 就能用是有条件的,  必须是公有的成员变量,不能是私有再封装的
            Student stu;
            stu.age = 20;
            stu.Say();
            Console.ReadKey();

        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 结构体
{
    //1.结构体是值类型
    //2.结构体可以不new直接使用,但是是有条件的
    //3.不能对结构体中的成员变量赋初值
    //4.声明结构的对象后,必须对结构的成员赋初值
    public struct Student
    {
        public int age;
        public void Say()
        {
            Console.WriteLine("Say");
            Console.ReadLine();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值