美文网首页
c#学习笔记

c#学习笔记

作者: 幽泉流霜 | 来源:发表于2019-01-16 10:25 被阅读0次

    其实我本身对于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#学习笔记

          本文链接:https://www.haomeiwen.com/subject/wqvpdqtx.html