c#学习笔记

其实我本身对于c#这门语言不是很有兴趣,但是因为学校的实训课程要求使用这门语言所以就只好跟着学习了。经过一周的学习,也学了个大概 以下就做一个学习笔记,方便日后要用的时候回来再看一下。

其实c#和c++有很多类似之处 其基本语法和c++几乎完全一样 下面先说一下和c++的不同之处
首先是 它的输出部分

Console.WriteLine("姓名:{0} 年龄{1}",name,age);

用控制台输出{0} {1}分别代表逗号后面的第几个位置

c#的 foreach循环

  int count = 0;
        foreach (int element in fibarray)
        {
            count += 1;
            System.Console.WriteLine("Element #{0}: {1}", count, element);
        }

对于数组的循环非常好用
c#重载

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

namespace c2
{
    class Complex
    {
        private int real;
        private int imag;
        public Complex(int a = 0,int b = 0)
        {
            real = a;
            imag = b;
        }
       static public Complex operator+(Complex a,Complex b)
        {
           return new Complex(a.real+b.real,a.imag+b.imag);
        }
       static public Complex operator -(Complex a, Complex b)
       {
           return new Complex(a.real - b.real, a.imag - b.imag);
       }
       static public Complex operator -(Complex a)
       {
           return (new Complex(-a.real, -a.imag));
       }
        public void display()
       {
           Console.WriteLine("{0}i+{1}j", real, imag);
       }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Complex x = new Complex(2,-3);
            Complex y = new Complex(-1, 5);
            Complex z;
            z = x + y;
            z.display();
            z = -z;
            z.display();
            z = y - x;
            z.display();
        }
    }
}

其中的基本语法和c++差不多 但是需要用到重载函数要放到static中
并且没有友元函数的重载了。
c++的双目运算符通常都用放到友元函数中间

引用存储的例子

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

namespace c1
{
    class Person
    {
        private string name;
        private int age;
        public Person(string a,int b)
        {
            name = a;
            age = b;
        }
        public void Setname(string a)
        {
            name = a;
        }
        public void Setage(int a)
        {
            age = a;
        }
        public void display()
        {
            Console.WriteLine("姓名:{0} 年龄{1}",name,age);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Person a = new Person("wang", 13);
            Person b;
            a.display();
            b = a;
            b.Setname("tang");
            a.display();
        }
    }
}

引用存储
二者共用一块存储空间
当改变其中一个的值 另一个值也会改变

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一章 计算机与C++编程简介 C++程序6个阶段编程 ->预处理->编译->连接->装入->执行1.程序在编译器...
    rogertan30阅读 4,133评论 0 1
  • ^函数重载的匹配: 当函数名被重载后,函数的匹配过程:首先寻找能精确匹配的函数,如果未能精确匹配,则尝试...
    鲁大帅阅读 1,068评论 0 1
  • TED这一期由诊疗心理学家Meg Jay演讲的《Why 30 is not the new 20》,今天忽然就...
    cwin阅读 6,171评论 0 1
  • 那一条来自故乡的大河, 在我的心中缓缓流淌, 一曲欢歌,在耳边深情吟唱。难忘 春日夹岸成片的油菜花,醉人 的金黄,...
    天涯飘零苏州阅读 273评论 1 1
  • 2017年12月24号 相博超妈妈 礼拜天休息上午相博超做了些练习题,我洗洗衣服,中午去朋友家吃的鱼。我跟相博超...
    一年级四班相博超妈妈阅读 166评论 0 0