C# basic
2016-01-19 05:45
821 查看
1. output
2. naming convention
variable: start with lower-case, use camel-case
for the rest (class name, method name, const): start with upper-case, use camel-case
3. value type
similar to primitive in jave
for example: int, float, bool
4. out
The out keyword causes arguments to be passed by reference. This is like the ref keyword, except that ref requires that the variable be initialized before it is passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword.
5. ?? operator
equals to
6. is (check type compatible)
7. compare string
not like Java, in which == and equals are different.
for string in c#, == and Equals() are the same.
equals
8. define 2d array
access element in 2d array
9. List
10. foreach
11. dictionary
12. encapsulation
the second line is a method call
below is a same definition of name
13. override method
14. extend class
15. as, is
"as" is safer than below, it return null if failed to cast
Console.WriteLine("hello world");
2. naming convention
variable: start with lower-case, use camel-case
double thePrice = 14.95;
for the rest (class name, method name, const): start with upper-case, use camel-case
const int HomeRunRecord = 61;
3. value type
similar to primitive in jave
for example: int, float, bool
4. out
The out keyword causes arguments to be passed by reference. This is like the ref keyword, except that ref requires that the variable be initialized before it is passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword.
class OutExample { static void Method(out int i) { i = 44; } static void Main() { int value; Method(out value); // value is now 44 } }
5. ?? operator
int a = (x ?? 0);
equals to
int a = (x != null? x:0);
6. is (check type compatible)
static void Test(object o) { Class1 a; if (o is Class1) { Console.WriteLine("o is Class1"); a = (Class1)o; // Do something with "a." } }
7. compare string
not like Java, in which == and equals are different.
for string in c#, == and Equals() are the same.
if (s1.Equals(s2)){}
equals
if (s1 == s2) {}
8. define 2d array
string[,] strs = new string[3, 4];
access element in 2d array
strs[1, 2] = "hello";
9. List
var fruits = new List<string>(); fruits.Add("apple");
10. foreach
foreach (var item in fruits) { Console.WriteLine(item); }
11. dictionary
var inventory = new Dictionary<string, double>(); inventory.Add("apples", 56); if (inventory.TryGetValue("apples", out value)) { Console.WriteLine("apple value:" + value); }
12. encapsulation
class Fruit { private string name; public string Name { get { return name; } set { name = value; } } }
the second line is a method call
var f1 = new Fruit(); f1.Name = "apple";
below is a same definition of name
public string Name { get; set; }
13. override method
public override string ToString() { return base.ToString(); }
14. extend class
class Produce { private string name; public Produce(string name) { Name = name; } } class Fruit : Produce { public Fruit(string name): base(name) { } }
15. as, is
class A { } class B : A { } class Program { static void Main(string[] args) { B obj = new B(); A obj2 = obj as A; if (obj is A) { Console.WriteLine("obj is A"); } Console.ReadKey(); } }
"as" is safer than below, it return null if failed to cast
A obj2 = (A)obj;
相关文章推荐
- C# 在一个窗体中调用另一个窗体的控件可能出现的问题
- C#中 时间戳与普通时间格式的转换
- C# MVC 页面静态化导致的问题
- C#通过指针读取文件
- C#中直接操作内存的方法
- C#中堆和栈的区别?
- c# 调用c 的dll
- C#如何使用Soap协议调用WebService?
- C# 获取某一文件夹下给定后缀名的文件名列表
- 设计模式前奏–封装继承
- C#文件操作
- C#技术分享【PDF转换成图片——13种方案】(2013-07-25重新整理)
- C#基础入门典型例题(1)
- C#实现Stream与byte[]之间的转换实例教程
- C#编程总结(十)字符转码
- C# using、namespace使用注意事项
- C# winIO32位,64位的使用
- C# 读取EXCEL数据
- 调用外部webwervice测试
- [C#新手入门 九] C#读取Excel表中的数据时,有的数据读取不到