C#第一天

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

using N1;

namespace Test
{
    class Program
    {
        static void Main()
        {
            A oa = new A();
            oa.Mysl();
        }
    }
}

namespace N1
{
    class A
    {
        public void Mysl()
        {
            Console.WriteLine("okk");
            Console.ReadLine();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{
    class C
    {
        public int _Value = 0;
    }
    static void Main()
    {
        int v1 = 0;
        int v2 = v1;
        v2 = 789;
        C r1 = new C();
        C r2 = r1;
        r2._Value = 112;
        //r1._Value = 110;
        Console.WriteLine("Values:{0},{1}",v1,v2);
        Console.WriteLine("Refs:{0},{1}",r1._Value,r2._Value);
    }
}

应用类型的特征:(主要的对象类型数据)

  1.必须在托管堆堆中为引用类型变量分配内存。

  2.必须使用new关键字来创建引用类型的变量。

  3.在托管堆中的每个对象都有与之相关联的附加成员,这些成员必须被初始化。

  4.引用类型变量由垃圾回收机制来管理。

  5.多个引用类型变量可以引用同一对象,这种情形下,对一个变量的操作会影响另一变量所引用的同一对象。

  6.引用类型被赋值前的值都是NULL。

值类型与引用类型的区别:

  1。值类型直接存储其值,而引用类型存储对其值得引用。

  2.值类型是在栈中操作,而引用类型是在堆(托管堆)中分配存储单元。(值类型相当于现金,要用直接用。引用类型相当于存折,想用的要去取)

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

class Program
{
    static void Main()
    {
        int i = 2008;
        object obj = i;//object 所有类的基类,将值类型转换为引用类型的过程叫做装箱
        Console.WriteLine("old:{0},{1}",i,obj);

        i = 5454;
        Console.WriteLine("new:{0},{1}",i,obj);
        Console.ReadLine();
    }
}

装箱和卸箱:拆箱时要注意,拆箱后得到的值类型数据的值与装箱对象相等,{要符合类型一致性原则,即不能将一个值为“String”的object类型转换为int类型}

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

class Program
{
    static void Main()
    {
        string strA = "用一生等待";
        string strB = "永不放弃";
        string newstr = String.Format("{0},{1}!!",strA,strB);
        Console.WriteLine(newstr);
        Console.ReadLine();
    }
}

上面是格式化字符串操作。

数组的合并和拆分:

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

class Program
{
    static void Main()
    {
        int[] arr1 = new int[] {1,2,3,4,5};
        int[] arr2 = new int[] {6,7,8,9,10};
        int len = arr1.Length + arr2.Length;
        int[] arr3 = new int[len];

        for (int i = 0; i < arr3.Length; ++i)
        {
            if (i < arr1.Length)
            {
                arr3[i] = arr1[i];
            }
            else
            {
                arr3[i] = arr2[i - arr1.Length];
            }
        }

        Console.WriteLine("合并以后的一维数组:");
        foreach (int i in arr3)
            Console.Write("{0}",i + " ");
        Console.WriteLine();

        int[,] arr4 = new int[2,5];
        for (int i = 0; i < arr4.Rank; ++i)
        {
            switch (i)
            {
                case 0:
                    {
                        for (int j = 0; j < arr1.Length; ++j)
                        {
                            arr4[i, j] = arr1[j];
                        }
                        break;
                    }
                case 1:
                    {
                        for (int j = 0; j < arr2.Length; ++j)
                        {
                            arr4[i, j] = arr2[j];
                        }
                        break;
                    }
            }
        }
        Console.WriteLine("合并以后的二维数组为:");
        for (int i = 0; i < arr4.Rank; ++i)
        {
            for (int j = 0; j < arr4.GetUpperBound(arr4.Rank - 1) + 1; ++j)
            {
                Console.Write(arr4[i,j] + " ");
            }
            Console.WriteLine();
        }
    }
}

ArrayList类

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

class Program
{
    static void Main()
    {
        System.Collections.ArrayList list1 = new System.Collections.ArrayList();
        list1.Add(3);
        list1.Add(105);
        foreach (int i in list1)
        {
            Console.Write(i + " ");
        }
        Console.WriteLine();
        //Console.Write(list1);//不能这么写,list1是个数组
        //Console.WriteLine();

        System.Collections.ArrayList list2 = new System.Collections.ArrayList();
        list2.Add("It is raining in Redmond.");
        list2.Add("It is snowing in the mountains.");
        foreach (string str in list2)
        {
            Console.WriteLine(str);
        }
        Console.WriteLine();
        //Console.Write(list2);
        //Console.WriteLine();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class GenericList<T>
{
    private class Node
    {
        public Node(T t)
        {
            next = null;
            data = t;
        }
        private Node next;

        public Node Next
        {
            get { return next; }
            set { next = value; }
        }

        private T data;

        public T Data
        {
            get { return data; }
            set { data = value; }
        }
    }
    private Node head;

    public GenericList()
    {
        head = null;
    }

    public void AddHead(T t)
    {
        Node n = new Node(t);
        n.Next = head;
        head = n;
    }

    public IEnumerator<T> GetEnumerator()
    {
        Node current = head;
        while(current != null)
        {
            yield return current.Data;
            current = current.Next;
        }
    }
}
class Program
{
    static void Main()
    {
        GenericList<int> list = new GenericList<int>();

        for (int x = 0; x < 10; ++x)
        {
            list.AddHead(x);
        }
        foreach (int i in list)
        {
            System.Console.Write(i + " ");
        }
        System.Console.WriteLine("
Done");
    }
}


  

原文地址:https://www.cnblogs.com/jiaoluo/p/3525596.html