C#无参无返回值与无参有返回值方法的理解与使用

本文详细介绍了C#编程的基础知识,包括命名空间、类的定义与使用、数组及对象实例化等核心概念。通过具体示例展示了如何在C#中创建和操作类,以及如何使用数组来存储和管理数据。

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

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
//引用程序员代码
namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            //数组
            /*
             整数类型、浮点型、字符型、字符串、枚举、日期、对象、、、
             */
            //string name1 = "元芳";
            //string name2 = "狄仁杰";
            //Console.WriteLine(name1 + name2);
            /*
             过多的数据,会使使用不方便,维护麻烦
             数组:用于存放一组数据类型相同的值
             数组4个基本要素
             1、数组标识符    数组名称
             2、数组元素      存放的值
             3、数组下标      下标从0开始
             4、数组类型      数组元素类型
            */
            //创建一个学生
            //实例化对象  Student学生

            Student stu1 = new Student();
            stu1.Name = "元芳";//调用并赋值
            stu1.Age = 19;
            stu1.Sex = '男';
            Student stu2 = new Student();
            stu2.Name = "狄仁杰";
            stu2.Age = 20;
            stu2.Sex = '男';

            Console.WriteLine($"姓名{stu2.Name},{stu2.Age},{stu2.Sex}");
            Console.Write(stu2.Name+":");stu1.XueXi();

            bool or = stu1.y();//调用方法
            if (or)
            {
                Console.WriteLine("调用成功");
            }
            else
            {
                Console.WriteLine("调用失败");
            }

        }
    }
}

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

namespace ConsoleApp2
{
    ///<summary>
    ///学生
    /// </summary>
    public class Student
    {
        //静态特征:描述对象特征
        public string Name;
        /// <summary>
        /// 姓名
        /// </summary>
        public int Age;
        /// <summary>
        /// 年龄
        /// </summary>
        public char Sex;
        /// <summary>
        /// 性别
        /// </summary>
        public string Phone;
        ///<summary>
        ///电话
        /// </summary>
        
        ///<summary>
        ///学习(功能)动态特征
        /// </summary>
        
        public void XueXi()//无参无返回值
        {
            //逻辑代码
            Console.WriteLine(this.Name + "你怎么看!");
        }
        public bool y()//无参有返回值
        {
            //逻辑代码
            Console.WriteLine(this.Name + ":大人,此事必有蹊跷!");
            bool or = true;
            return or;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值