├── README.md ├── csharp01.md ├── csharp02.md └── csharp03.md /README.md: -------------------------------------------------------------------------------- 1 | 针对Java程序员的C#快速入门教程。 2 | 3 | [开始阅读](csharp01.md) 4 | 5 | 目录 6 | 7 | 1. [c#与java对比](csharp01.md) 8 | 2. [Windows GUI 编程](csharp02.md) 9 | 3. [Web编程(MVC4)](csharp03.md) 10 | 11 | -------------------------------------------------------------------------------- /csharp01.md: -------------------------------------------------------------------------------- 1 | #c#课时01 2 | ##c#与java对比 3 | ###创建: 4 | 文件-新建-项目-VisualC#-控制台应用程序。 5 | ###结构: 6 | C#: 7 | 8 | using System; 9 | namespace HelloWorld 10 | { 11 | class Hello 12 | { 13 | static void Main() 14 | { 15 | Console.WriteLine("Hello World!"); 16 | Console.WriteLine("Press any key to exit."); 17 | Console.ReadKey(); 18 | } 19 | } 20 | } 21 | 22 | JAVA: 23 | 24 | import java.io.*; 25 | package cn.easycomm.test; 26 | public class HelloWorld{ 27 | public static void main(String[] args) { 28 | System.out.println("Hello World!"); 29 | } 30 | } 31 | 32 | 1. `namespace`与`package` 33 | 2. `using`与`import` 34 | 35 | > 注:using有另一种用法 36 | 37 | 3. `Main`与`main` 38 | 39 | ###数据类型: 40 | 41 | 1. C# 提供 Java 中可用的所有数据类型,并增加了对无符号数字和新的 128 位高精度浮点类型的支持。 42 | 2. Java 的 `boolean` 在 C# 中称为 `bool` 43 | 3. 常量,Java 使用 `final` 字段修饰符声明此类变量,而 C# 则使用 `const` 关键字 44 | 45 | const int NUM = 1; //c# 46 | public static final int NUM = 1; //java 47 | 4. 字符串,Java用`equals`,C#可以直接用`==`或`!=` 48 | 5. 转义字符,都使用 `\` ,C#中字符串开始前使用 `@` 声明字符串则不需转义字符 49 | 50 | ###运算符 51 | 52 | 1. C# 提供 Java 支持的所有适用的运算符 53 | 2. C# 中可用但 Java 中没有的一些新运算符(`checked`,`unchecked`...) 54 | 55 | ###流控制 56 | 57 | 1. 在 Java 和 C# 这两种语言中,`if` `else` 完全相同 58 | 2. switch,C# 要求在每个 `case` 的末尾都使用 `break`, `case` 中可以使用字符串变量 59 | 3. 在 C# 和 Java 中,`for` 循环的语法和操作相同 60 | 4. C#中引入了`foreach` ,Java中使用的是`for` 61 | 62 | C# 63 | 64 | static void Main() 65 | { 66 | string[] arr= new string[] {"Jan", "Feb", "Mar"}; 67 | 68 | foreach (string s in arr) 69 | { 70 | System.Console.WriteLine(s); 71 | } 72 | } 73 | Java 74 | 75 | for (String x : list) { 76 | System.out.println(x); 77 | } 78 | 5. `while` 和 `do...while` 语句的语法和操作是相同的 79 | 80 | ###参数传递 81 | 1. 在 Java 和 C# 中,引用对象的方法参数始终都是通过引用传递的,而基元数据类型参数(C# 中的值类型)是通过值传递的。 82 | 2. 在 C# 中,若要通过引用传递值类型,需要指定关键字 `ref` 或 `out` 83 | 84 | ref 85 | 86 | class TestRef 87 | { 88 | private static void Add(int i, ref int result) 89 | { 90 | result += i; 91 | return; 92 | } 93 | 94 | static void Main() 95 | { 96 | int total = 20; 97 | System.Console.WriteLine("Original value of 'total': {0}", total); 98 | 99 | Add(10, ref total); 100 | System.Console.WriteLine("Value after calling Add(): {0}", total); 101 | } 102 | } 103 | 104 | Original value of 'total': 20 105 | Value after calling Add(): 30 106 | out 107 | 108 | class TestOut 109 | { 110 | private static void Add(int i, int j, out int result) 111 | { 112 | // The following line would cause a compile error: 113 | // System.Console.WriteLine("Initial value inside method: {0}", result); 114 | 115 | result = i + j; 116 | return; 117 | } 118 | 119 | static void Main() 120 | { 121 | int total = 20; 122 | System.Console.WriteLine("Original value of 'total': {0}", total); 123 | 124 | Add(33, 77, out total); 125 | System.Console.WriteLine("Value after calling Add(): {0}", total); 126 | } 127 | } 128 | 129 | Original value of 'total': 20 130 | Value after calling Add(): 110 131 | 132 | ###属性 133 | 1. `get` ,`set`方法 134 | 135 | public class Animal 136 | { 137 | public string Age { get; set; } 138 | private string name; 139 | 140 | public string Species 141 | { 142 | get 143 | { 144 | return name; 145 | } 146 | set 147 | { 148 | name = value; 149 | } 150 | } 151 | } 152 | 2. 访问属性 153 | 154 | animal.Species = "Lion"; // set accessor 155 | System.Console.WriteLine(animal.Species); // get accessor 156 | 157 | ###数组 158 | 159 | 1. 定义初始化 160 | 161 | int[] arr2Lines; 162 | //int arr2[]; //compile error 163 | arr2Lines = new int[5] {1, 2, 3, 4, 5}; 164 | int[] arr1Line = {1, 2, 3, 4, 5}; 165 | 166 | ###继承与接口 167 | 168 | 1. 在 C# 中,继承及接口实现均由 : 运算符定义,此运算符与 Java 中的 extends 和 implements 等效 169 | 2. base与super,访问基类 170 | 171 | ###异常 172 | 173 | 1. C# 中的异常处理与 Java 中的异常处理非常相似 174 | 2. Exception 为所有异常类的基类 175 | 176 | try 177 | { 178 | // code to open and read a file 179 | } 180 | catch (System.IO.FileNotFoundException e) 181 | { 182 | // handle the file not found exception first 183 | } 184 | catch (System.IO.IOException e) 185 | { 186 | // handle any other IO exceptions second 187 | } 188 | catch 189 | { 190 | // a catch block without a parameter 191 | // handle all other exceptions last 192 | } 193 | finally 194 | { 195 | // this is executed whether or not an exception occurs 196 | // use to release any external resources 197 | } 198 | 199 | ###C#高级技术 200 | 201 | 1. 属性(类似Java中的批注) 202 | 203 | [System.Serializable()] //可以被序列化 204 | public class Employee 205 | { 206 | public int ID; 207 | public string Name; 208 | [System.NonSerialized()] public int Salary; 209 | } 210 | 211 | 2. 事件与委托(把方法当成类型,传递方法) 212 | 3. LINQ查询表达式 213 | 4. Lambda表达式 214 | 215 | 216 | ##2、c#常用操作 217 | 218 | ###常用集合 219 | 1. ArrayList 220 | 221 | [http://msdn.microsoft.com/zh-cn/library/vstudio/system.collections.arraylist_methods(v=vs.90).aspx](http://msdn.microsoft.com/zh-cn/library/vstudio/system.collections.arraylist_methods(v=vs.90).aspx "http://msdn.microsoft.com/zh-cn/library/vstudio/system.collections.arraylist_methods(v=vs.90).aspx") 222 | 2. Hashtable 223 | 224 | [http://msdn.microsoft.com/zh-cn/library/vstudio/system.collections.hashtable(v=vs.100).aspx](http://msdn.microsoft.com/zh-cn/library/vstudio/system.collections.hashtable(v=vs.100).aspx "http://msdn.microsoft.com/zh-cn/library/vstudio/system.collections.hashtable(v=vs.100).aspx") 225 | 3. Dictionary 226 | 227 | [http://msdn.microsoft.com/zh-cn/library/vstudio/ms132468(v=vs.90).aspx](http://msdn.microsoft.com/zh-cn/library/vstudio/ms132468(v=vs.90).aspx "http://msdn.microsoft.com/zh-cn/library/vstudio/ms132468(v=vs.90).aspx") 228 | ###文件操作 229 | 1. 文件读写 230 | 231 | class TestFileIO 232 | { 233 | static void Main() 234 | { 235 | string fileName = "test.txt"; // a sample file name 236 | 237 | // Delete the file if it exists. 238 | if (System.IO.File.Exists(fileName)) 239 | { 240 | System.IO.File.Delete(fileName); 241 | } 242 | 243 | // Create the file. 244 | using (System.IO.FileStream fs = System.IO.File.Create(fileName, 1024)) 245 | { 246 | // Add some information to the file. 247 | byte[] info = new System.Text.UTF8Encoding(true).GetBytes("This is some text in the file."); 248 | fs.Write(info, 0, info.Length); 249 | } 250 | 251 | // Open the file and read it back. 252 | using (System.IO.StreamReader sr = System.IO.File.OpenText(fileName)) 253 | { 254 | string s = ""; 255 | while ((s = sr.ReadLine()) != null) 256 | { 257 | System.Console.WriteLine(s); 258 | } 259 | } 260 | } 261 | } 262 | ###数据库 263 | 264 | 1. 连接数据库 265 | 266 | 267 | using System; 268 | using System.Data; 269 | using System.Data.SqlClient; 270 | 271 | class Program 272 | { 273 | static void Main() 274 | { 275 | string connectionString = "Data Source=(local);Initial Catalog=Northwind;" 276 | + "Integrated Security=SSPI"; 277 | string queryString = 278 | "SELECT CategoryID, CategoryName FROM dbo.Categories;"; 279 | using (SqlConnection connection = 280 | new SqlConnection(connectionString)) 281 | { 282 | SqlCommand command = connection.CreateCommand(); 283 | command.CommandText = queryString; 284 | 285 | try 286 | { 287 | connection.Open(); 288 | 289 | SqlDataReader reader = command.ExecuteReader(); 290 | 291 | while (reader.Read()) 292 | { 293 | Console.WriteLine("\t{0}\t{1}", 294 | reader[0], reader[1]); 295 | } 296 | reader.Close(); 297 | } 298 | catch (Exception ex) 299 | { 300 | Console.WriteLine(ex.Message); 301 | } 302 | } 303 | } 304 | } 305 | ###Links 306 | 1. C#(针对Java开放人员) 307 | 308 | [http://msdn.microsoft.com/zh-cn/library/ms228358(v=vs.90).aspx](http://msdn.microsoft.com/zh-cn/library/ms228358(v=vs.90).aspx) 309 | 2. C#编程指南 310 | 311 | [http://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx](http://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx) 312 | 313 | -------------------------------------------------------------------------------- /csharp02.md: -------------------------------------------------------------------------------- 1 | #c#课时02 2 | ##Windows GUI 编程 3 | ###WinForm 4 | 1. 控件 5 | 2. 事件 6 | 3. 数据源 7 | ###Wpf 8 | 1. 控件 9 | 2. 事件 10 | 3. 数据源 -------------------------------------------------------------------------------- /csharp03.md: -------------------------------------------------------------------------------- 1 | #c#课时03 2 | ##MVC4(MVC模式) 3 | ###创建MVC4工程 4 | 1. 文件-模板-Visual C#-Web-ASP.NET MVC4 Web 应用程序-Internet应用程序(模板引擎"Razor") 5 | 6 | ###创建Controller 7 | 8 | using System; 9 | using System.Collections.Generic; 10 | using System.Linq; 11 | using System.Web; 12 | using System.Web.Mvc; 13 | 14 | namespace MvcApplication1.Controllers 15 | { 16 | public class HomeController : Controller 17 | { 18 | public ActionResult Index() 19 | { 20 | ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序。"; 21 | 22 | return View(); 23 | } 24 | 25 | public ActionResult About() 26 | { 27 | ViewBag.Message = "你的应用程序说明页。"; 28 | 29 | return View(); 30 | } 31 | 32 | public ActionResult Contact() 33 | { 34 | ViewBag.Message = "你的联系方式页。"; 35 | 36 | return View(); 37 | } 38 | } 39 | } 40 | 41 | 访问: 42 | 43 | - http://localhost:30626/ 44 | - http://localhost:30626/Home 45 | - http://localhost:30626/Home/About 46 | 47 | ###创建View 48 | 1. html+css+js 49 | 2. Razor语法 50 | 51 | @{string productName = "台灯";} //c#代码 52 | @productName 53 | @model.name 54 | @DateTime.Now.ToString("yyyy-MM-hh") 55 | 56 | 3. 使用Model中的数据 57 | 58 | @model Models.HomeModel //cshtml文件开始 59 | ...... 60 | @model.name //使用 61 | 62 | ###创建Model 63 | 1. 创建属性 64 | 65 | public属性可以在View中访问 66 | 2. 其他方法 67 | 68 | 访问数据库等逻 69 | 70 | ###Links 71 | 72 | 1. mvc4 73 | 74 | [http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4](http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4) 75 | 76 | ##Unity.Mvc4(三层架构) 77 | ###目录结构 78 | BLL //逻辑 79 | Common //工具类 80 | DAL //数据库逻辑 81 | Lib //第三方库 82 | Model //数据库对应Model,与数据库字段映射 83 | Web 84 | -Controllers //Controller 85 | -Models //View上使用的Model 86 | -Views //页面 87 | ###重要文件说明 88 | WEB 89 | -Web.config //配置数据库 90 | -Bootstrapper.cs //配置DAL,BLL,注入 91 | Common 92 | -OperateMessage.cs //公共跳转页面 93 | DAL 94 | -DBHelper 95 | -DBHeloerSqlServer.cs //数据库操作方法 96 | ###创建页面流程 97 | 1. 在对应文件夹创建Controller 98 | 2. 创建页面View 99 | 3. 创建View上使用的Model 100 | 4. 在BLL下创建IXXXBLL接口,XXXBLL实现 101 | 5. 在DAL下创建IXXXDAL接口,XXXDLL实现,XXXDLL中操作数据库 102 | 6. 在Bootstrapper.cs中配置对应关系 103 | 104 | --------------------------------------------------------------------------------