Hello 示例

  • 终端示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationHello
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello");  // 生成的模板中,加这句
        }
    }
}



Hello World 程序解析

using System; // 类库
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationHello // 名称空间
{
    class Program  // 类
    {
        static void Main(string[] args) // 静态方法
        {
            Console.WriteLine("Hello");
        }
    }
}

类实例demo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; // 引入

namespace ConsoleApplicationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            (new Form()).ShowDialog(); // 调用实例对象的ShowDialog()方法,实现窗口
        }
    }
}

定义变量并声明demo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; // 引入

namespace ConsoleApplicationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // (new Form()).ShowDialog();
            Form myForm; // 声明变量
            myForm = new Form(); // 实例化
            myForm.Text = "我是窗体标题"; // 赋值标题文本并弹窗
            myForm.ShowDialog();
        }
    }
}

// 变量之间的引用demo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; 

namespace ConsoleApplicationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Form myForm;
            Form myFormCopy;
            myForm = new Form();
            myFormCopy = myForm;
            myForm.Text = "我是窗体标题";
            myFormCopy.Text = "我改变了原窗体标题内容"; // 窗体最后显示的内容
            myForm.ShowDialog(); 
        }
    }
}


方法运用示例

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

namespace ConsoleApplicationDemo1
{
    class Program
    {
        static void Main(string[] args)
        {
            double x = Math.Pow(2, 3); // 计算2的三次方
            Console.WriteLine(x);
        }
    }
}

查看数据类型(x.GetType().Name)

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

namespace ConsoleApplicationDemo2
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = 3;
            Console.WriteLine(x.GetType().Name);
        }
    }
}

声明一个计算类,并定义一个加法方法(带参数)

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

namespace ConsoleApplicationDemo2
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Calculator CountObj = new Calculator(); // 注意声明方式: Calculator CountObj
            int res = CountObj.Add(1, 2);
            Console.WriteLine(res);

        }
    }

    class Calculator
    {
        public int Add(int a,int b) // int 表示该函数返回int类型的数据,有返回值
        {
            int result = a + b;
            return result;
        }
    }
}


  • 声明不带参数方法(有返回自豪)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationDemo2
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Calculator CountObj = new Calculator();
            var str = CountObj.Today(); // 9


        }
    }

    class Calculator
    {
        public int Add(int a,int b)
        {
            ......
        }

        public string Today() // 定义不带参数的方法
        {
            int day = DateTime.Now.Day;
            return day.ToString();
        }
    }
}

  • 声明没有返回值的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationDemo2
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Calculator CountObj = new Calculator();
            CountObj.PrintSum(2,2); // 调用

        }
    }

    class Calculator
    {
        public int Add(int a,int b)
        {
            ......
        }

        public string Today()
        {
            ......
        }

        public void PrintSum(int c, int d) // void表示该方法没有返回值
        {
            int response = c + d;
            Console.WriteLine(response);
        }
    }
}

  • for循环体验(输出10减至1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationDemo2
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Calculator CountObj = new Calculator();
            CountObj.PrintXTo1(10); // 调用
        }
    }

    class Calculator
    {
        public int Add(int a,int b)
        {
            ......
        }

        public string Today()
        {
           ......
        }

        public void PrintSum(int c, int d)
        {
            ......
        }

        public void PrintXTo1(int x)
        {
            for (int i = x; i > 0 ; i--) // 循环
            {
                Console.WriteLine(i);
            }
        }
    }
}

  • 把for循环示例改造成递归,示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationDemo2
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Calculator CountObj = new Calculator();
            CountObj.PrintXTo1(10); // 调用
        }
    }

    class Calculator
    {
        public int Add(int a,int b)
        {
            ......
        }

        public string Today()
        {
           ......
        }

        public void PrintSum(int c, int d)
        {
            ......
        }

        public void PrintXTo1(int x) // 递归
        {
            if (x == 1)
            {
                Console.WriteLine(x);
            }
            else
            {
                Console.WriteLine(x);
                PrintXTo1(x - 1); // 调用自身
            }
        }
    }
}

以下声明方式,可以让变量重新赋值(其他情况均不允许)

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic myVar = 100; // 这种声明方式允许变量重新赋值
            Console.WriteLine(myVar);
            myVar = "OK";
            Console.WriteLine(myVar);
        }
    }
}

获取类型的另一种写法

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            // Form myForm = new Form();
            // var x = myForm.GetType().Name;
            Type myType = typeof(Form);
            Console.WriteLine(myType.FullName);
        }
    }
}

  • 注意以下结果
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            double res = 3 / 4;
            Console.WriteLine(res); // 0
        }
    }
}

  • 递归内存溢出示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            BadGuy bg = new BadGuy(); // 声明BadGuy变量并调用类方法
            bg.BadMethod();
        }
    }

    class BadGuy
    {
        public void BadMethod()
        {
            int x = 100;
            this.BadMethod(); // 递归
        }
    }
}

"C#"五大数据类型

- 总基类: Object

- 引用类型

	- 类(class)

	- 接口(高级)

	- 委托(高级)

- 值类型

	- 结构体(struct)

	- 枚举(enum)
  • 遍历list示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Person person1 = new Person();
            Person person2 = new Person();
            person1.Name = "Jim";
            person2.Name = "Green";
            // 调用 GetMarry
            List<Person> nation = (Person.GetMarry(person1, person2));
            foreach (var item in nation)
            {
                Console.WriteLine(item.Name);
            }
        }
    }

    class Person
    {
        public String Name;
        public static List<Person> GetMarry(Person p1, Person p2) // GetMarry返回list
        {
            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;
                people.Add(child);
            }
            return people;
        }
    }
}

  • 创建文件示例
using System;
using System.Collections.Generic;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.IO.File.Create("D:\\king.txt"); // 创建文件
        }
    }

}

  • 展示窗体示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; // 引入 Form

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Form myForm = new Form();
            myForm.Text = "Hello";
            myForm.ShowDialog();
        }
    }

}

  • 创建整型数组示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] myIntArray = new int[] { 1, 2, 3, 4, 5 };
            Console.WriteLine(myIntArray[0]); // 输出第一项
            Console.WriteLine(myIntArray[myIntArray.Length - 1]); // 最后一项
        }
    }

}

new实例的时候,可以给属性赋值(内部是调用构造方法实现)

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Form myForm = new Form() { Text = "Hello" ,FormBorderStyle = FormBorderStyle.FixedToolWindow};
            myForm.ShowDialog();
        }
    }
}

var搭配new实现匿名类

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var person = new { Name = "Jim Green", Age = 20 }; // 声明匿名类
            Console.WriteLine(person.Name); 
            Console.WriteLine(person.Age);
            Console.WriteLine(person.GetType().Name);  // <>f__AnonymousType0`2

        }
    }

}

继承父类demo演示

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Son s = new Son(); // 实例化子类
            s.ShowMessage(); // 调用父类方法
        }
    }

    class Parent // 声明父类
    {
        public void ShowMessage()
        {
            Console.WriteLine("I'm a message in Parent");
        }
    }

    class Son : Parent // 继承
    {

    }

}

  • 重写父类的方法,加上new关键字即可
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Son s = new Son();
            s.ShowMessage();
        }
    }

    class Parent
    {
        public void ShowMessage()
        {
            Console.WriteLine("I'm a message in Parent");
        }
    }

    class Son : Parent
    {
        new public void ShowMessage() // 重写父类的方法
        {
            Console.WriteLine("I'm a new message in Son");
        }
    }

}