标签:VS2017 学习 ++ linq 防止 tox 1.3 后缀 app1
??C#的编程有点像java哈!C#的代码文件后缀是 .cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
}
}
}
??学习任何语言都要从 Hello World 开始!C#的输出语句为 Console.WriteLine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello! World!");
Console.ReadKey();//防止console一闪而过
}
}
}
??任何语言都少不了输入函数,C#的输入函数是 Console.Readline() ,这个函数从控制台读取一行输入,它是 String 类型的,所以要适当地进行类型转换。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int a;
string b;
b = "是我刚刚输入的值!";
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("a={0},{1}",a,b);
Console.ReadKey();
}
}
}
??C#的格式转换函数的一般形式为 Convert.ToXXX ,如果使用的是VS2017的话,IDE会自动提示。
??if 是所有语言都有的语法!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int a;
a = Convert.ToInt32(Console.ReadLine());
if(a>5)
{
Console.WriteLine("{0}>5!\n", a);
}
else
{
Console.WriteLine("{0}<=5!\n", a);
}
Console.ReadKey();
}
}
}
??循环是编程语言的命脉!
for 循环
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int i;
for(i=0;i<5;i++)
{
Console.WriteLine("{0}\n", i);
}
Console.ReadKey();
}
}
}
while 循环
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int i=0;
while(i<=5)
{
Console.WriteLine("{0}\n", i);
i = i + 1;
}
Console.ReadKey();
}
}
}
??在C#中,有三种常用数组结构,他们各有特点:
数组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[5] {2,3,4,5,6};
int i;
for(i=0;i<a.Length;i++)
{
Console.WriteLine("{0}", a[i]);
}
Console.ReadKey();
}
}
}
List
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<int> l = new List<int>();
int i,a = 5;
while(a>=0)
{
l.Add(a);//添加数据
a = a - 1;
}
for(i=0;i<l.Count;i++)
{
Console.Write("{0} ", l[i]);
}
Console.Write("\n");
l[0] = 8;//修改数据
for (i = 0; i < l.Count; i++)
{
Console.Write("{0} ", l[i]);
}
Console.Write("\n");
l.RemoveAt(0);
for (i = 0; i < l.Count; i++)
{
Console.Write("{0} ", l[i]);
}
Console.ReadKey();
}
}
}
ArrayList
??记得在最前面添加 using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
ArrayList ArL = new ArrayList();
int i;
//添加数据
ArL.Add(1);
ArL.Add(2);
ArL.Add(3);
ArL.Add("a");
ArL.Add("b");
ArL.Add("c");
for (i=0;i< ArL.Count; i++)
{
Console.Write("{0} ", ArL[i]);
}
Console.Write("\n");
ArL[0] = 8;//修改数据
for (i = 0; i < ArL.Count; i++)
{
Console.Write("{0} ", ArL[i]);
}
Console.Write("\n");
ArL.RemoveAt(0);
for (i = 0; i < ArL.Count; i++)
{
Console.Write("{0} ", ArL[i]);
}
Console.ReadKey();
}
}
}
??使用函数,降低代码耦合度!
输入半径,计算面积,pi=3.1415926
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApp1
{
class Program
{
static Double Calculate(Double R)
{
return R*R*3.1415926;
}
static void Main(string[] args)
{
double r;
r = 5;
Console.WriteLine("r*r*pi={0}", Calculate(r));
Console.ReadKey();
}
}
}
标签:VS2017 学习 ++ linq 防止 tox 1.3 后缀 app1
原文地址:https://www.cnblogs.com/yznnnn/p/10544804.html