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 ...
随机推荐
- Frameset的使用
一.frameset 1. 属性 ①border 设置框架的边框粗细. ②bordercolor 设置框架的边框颜色. ③frameborder 设置是否显示框架边框.设定值只有0.1:0 表示不要边 ...
- json转换
Newtonsoft.Json.JsonConvert.SerializeObject 使用上述语句,将创建的对象, 转换成json格式
- Android基础开发文档汇总
一.Android 基本组件 1. Android中PackageManager使用示例 : http://blog.csdn.net/qinjuning/article/details/68678 ...
- 二模12day1解题报告
T1.笨笨与电影票(ticket) 有n个1和m个0,求每个数前1的个数都大于等于0的个数的排列数. 非常坑的一道题,推导过程很烦.首先求出所有排列数是 C(n+m,m),然后算不合法的个数. 假设存 ...
- js对象常用2中构造方法
//js 对象的构造方法通常有2中情况: //第一种是通过json对象构造 var persion={ name:"孙悟空", age:40, eat:function () { ...
- 安卓基于WifiScanner的签到APP
没图说个JB?首先上图: 友情提醒:后台数据库使用的是Bmob后端云 主要设计思路:首先选一个附近的wifi,输入签到码,进行签到. 签到之后会启动一个后台线程每隔一段时间扫描附近wifi, ...
- 移动端穿插着PC端自动化-Python基础(干货)
1.前面已经把所有前期工作完成了 下面进行一些简单的小脚本来更好的了解Python.对Python有一些基础的童鞋理解起来会比较容易,我刚接触的时候也会有点懵的,现在简单的也是没问题了. 大牛请不要喷 ...
- MySQL 5.7 Command Line Client输入密码后闪退和windows下mysql忘记root密码的解决办法
MySQL 5.7 Command Line Client输入密码后闪退的问题: 问题分析: 1.查看mysql command line client默认执行的一些参数.方法:开始->所有程序 ...
- LCC
LCC: super vector:
- rabbitMQ学习(四)
按照routing key接收信息 发送端: public class EmitLogDirect { private static final String EXCHANGE_NAME = &quo ...