C# basic
1. output
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 = ;
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 = ;
}
static void Main()
{
int value;
Method(out value);
// value is now 44
}
}
5. ?? operator
int a = (x ?? );
equals to
int a = (x != null? x:);
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[, ];
access element in 2d array
strs[, ] = "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", );
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# basic的更多相关文章
- Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结
Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...
- Basic Tutorials of Redis(9) -First Edition RedisHelper
After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...
- Basic Tutorials of Redis(8) -Transaction
Data play an important part in our project,how can we ensure correctness of the data and prevent the ...
- Basic Tutorials of Redis(7) -Publish and Subscribe
This post is mainly about the publishment and subscription in Redis.I think you may subscribe some o ...
- Basic Tutorials of Redis(6) - List
Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with t ...
- Basic Tutorials of Redis(5) - Sorted Set
The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...
- Basic Tutorials of Redis(4) -Set
This post will introduce you to some usages of Set in Redis.The Set is a unordered set,it means that ...
- Basic Tutorials of Redis(3) -Hash
When you first saw the name of Hash,what do you think?HashSet,HashTable or other data structs of C#? ...
- Basic Tutorials of Redis(2) - String
This post is mainly about how to use the commands to handle the Strings of Redis.And I will show you ...
- Basic Tutorials of Redis(1) - Install And Configure Redis
Nowaday, Redis became more and more popular , many projects use it in the cache module and the store ...
随机推荐
- [转]Amazon AWS亚马逊云服务免费一年VPS主机成功申请和使用方法
今天部落将再次为大家介绍如何成功申请到来自亚马逊的Amazon AWS免费一年的VPS主机服务.亚马逊公司这个就不用介绍了,是美国最大的一家网络电子商务公司,亚马逊弹性计算云Amazon EC2更是鼎 ...
- 【原创】Java批量反编译利器(jd-gui)介绍
Java反编译利器(jd-gui)介绍 当要做白盒测试或安全测试时,我们需要借助工具扫描被测代码,相信大家都遇到过没有源代码,而需要反编译的情形吧,也可能大家用过jad工具,我这里介绍下方便的工具jd ...
- IE10中session失效取不到值的问题
在eworkflow工作流,ebiao报表,eform自定义表单产品升级到IE10的时候,系统登录后,总是会取不到session中的值. for j2ee版本和for dotnet版本都一样取不到值. ...
- delphi.thread.同步
注意:此文只是讲线程间的同步,其它同步不涉及. 线程同步是个好话题,因为写线程经常会遇到,所以就写写自己知道的东西. D里面,同步(特指线程同步)从线程的角度来分,有几种情况: 1:主线程与工作线程的 ...
- sqlserver表分区
参考:http://www.cnblogs.com/knowledgesea/p/3696912.html 及百度搜索sqlserver表分区 create partition function sg ...
- 64位系统装oracle(ora-12154 )
装了n次的oracle,昨下午装服务器的oracle,结果遇到了一个问题,让我百思不得其解,但最终在大家的帮助下终于解决了. 我装的服务器是windows server 2007 64位的,装完ora ...
- oozie调用shell
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agree ...
- clientTop、offsetTop和scrollTop的区分
页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offse ...
- Cocos2d-x-3.0 Touch事件处理机制
在学习Cocos2d-html5游戏例子的时候,注册事件代码一直提示:TypeError: cc.Director._getInstance(...).getTouchDispatcher is no ...
- Android test---CTS
转载 1.下载最新的CTS download 2.准备工作 3.启动CTS测试 3.1 在控制台进入目录android-cts,目录android-cts下有三个文件夹,其中一个是tools. 3.2 ...