Home >Backend Development >C#.Net Tutorial >C# Advanced Programming (2) - Detailed explanation of core C#
C# has two methods to ensure that variables are initialized before use Initialized:
Variables are fields, and if they are not explicitly initialized, their default value is 0 when they are created.
The local variable of the method must be explicitly initialized in the code before it can be used in the statement. If its value is used before initialization, an error will be reported.
If the local variable scope conflicts, a CS0136 error will be reported.
If a field conflicts with a local variable, hide the field. Use the this keyword to access the hidden instance field, and use object.fieldname for static fields.
Constants must be initialized when declared, and changes are not allowed after their values are assigned.
#The value of a constant must be used in calculations at compile time, and the value cannot be extracted from a variable.
# Constants are always static and are not allowed to be modified using the static keyword.
Easy to read
Easy to modify
Easy to avoid errors
Value types and reference types
The difference between concepts and storage methods, Refer to Chapter 1.
To create an object you must use the new keyword to add a reference Assigning a type to another reference type only returns its address, which changes as it changes. Such as:
Test x,y; x testX = new Test(); y testY = x; testX.value =1; //这条语句执行后testX与testY的value属性的值都会为
The basic predefined types recognized by C# are not built into C#, but are built into the .NET Framework middle.
Decimal is not a built-in type in .NET Formework and will cause performance loss.
The bool type cannot use 0 to represent false, and non-0 to represent true.
All built-in types inherit the Object type.
The immutability of string: If testX and testY in the above code are of string type, when the string pointed to by testX changes, a new one will be created in the managed heap. string and point to it, the value pointed to by testY will not be changed.
if, else if, else.
The value of the case statement must be a constant expression, variables are not allowed.
与C++的区别:激活了前一个case不会自动激活后一个case,且每一个case必须用break否则报错;可以把字符串作为测试变量。
可以使用goto "标签"经行跳转。
case为空则不需要break。
循环
for 与 while的区别:for适合可预测次数的循环,while适合不可预测次数的循环。
foreach
要实现foreach的遍历,类中就必须实现IEnumerable接口。
foreach中不能改变每一项的值。
goto
break
跳出当前循环
continue
跳过此次循环
return
退出当前方法
一旦代码编辑好,枚举就成为基本类型。
与文件或组件不同,名称空间是一种逻辑组合,而不是物理组合。
一般名称空间可接受的格式为CompanyName.ProjectName.StstemSection。
名称空间别名的语法:
using slias = NamespaceName;
命名空间修饰符为 “::” 。
1 //using System; 2 using TestSystem = System; 3 4 namespace 命名空间别名 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 TestSystem::Console.WriteLine("This is a C# Progam"); 11 TestSystem.Console.ReadKey(); 12 } 13 } 14 }
命名空间别名演示
#if #elif 支持一组逻辑运算符“!”、“==”、“!+”、“||”。
可以包含数字字符,但是它们他们必须以字母或下划线开头。
不能使用C#关键字作标识符。
规则应该是一个正确的决策,而不是一种束缚。
Pascal:名称空间、类、函数名。
Camel:类中的字段(最好加“_”为前缀能更直观的知道这是个字段)、方法的参数。
The name/naming style should be as consistent as possible.
Namespaces are the only way .NET can distinguish object names in shared assemblies.
Microsoft recommends using the following namespace: d04661e771feadb793ae73b35fbaae73.72f679de98f4cda0c0a9d332c4b5adaa
If the object looks like an object, use attributes to represent it, that is:
Fields should always be private.
2016-05-07 22:29:18
The above is the detailed content of C# Advanced Programming (2) - Detailed explanation of core C#. For more information, please follow other related articles on the PHP Chinese website!