c# | 操作符(一)

本文介绍了C#中的操作符,包括成员访问操作符、方法调用、集合元素访问、后置递增和递减操作符以及typeof关键字的用法。讨论了操作符的优先级和赋值运算的顺序,并通过示例解析了自定义操作符的使用。

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

一、无题

先上一个不是很理解的例子,希望以后能够搞清楚

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

namespace ConsoleApplication7
{
   
    class Program
    {
   
        static void Main(string[] args)
        {
   
            Person person1 = new Person();
            Person person2 = new Person();
            person1.Name = "deer";
            person2.Name = "ou";
            List<Person> nation = person1 + person2;//这里用+号简化了哦
            foreach(var p in nation)
            {
   
                Console.WriteLine(p.Name);
            }
        }

    }

    
    
    class Person
    {
   
        public string Name;

        public static List<Person> operator+ (Person p1,Person p2)
        {
   
            List<Person> people = new List<Person>();
            people.Add(p1);
            people.Add(p2);
            for (int i = 0; i < 11; i++)
            {
   
                Person child = new Person();
                child.Name = p1.Name + "&" + p2.Name + "'s child";
                people.Add(child);
            }

            return people;
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值