C# Syntax Overview This chapter introduces you to the syntax of the C# language. It’s assumed that you have a reasonable amount of experience with C++ or Java, since C# shares a similar syntax. This is no accident.The designers of C# clearly meant to leverage the knowledge of those who have already developed with C++ and Java, which are arguably the dominant languages in object-oriented (OO) software development.I’ve noted nuances and differences that are specific to the C# language. But if you’re familiar with either C++ or Java, you’ll feel right at home with C# syntax.32644
C# Is a Strongly Typed Language
Like C++ and Java, C# is a strongly typed language, which means that every variable and object is instance in the system is of a well-defined type. This enables the compiler to check that the operations you try to perform on variables and object instances are valid. For example, suppose you have a method that computes the average of two integers and returns the result.
Expressions
Expressions in C# are practically identical to expressions in C++ and Java. The important thing to keep in mind when building expressions is operator precedence. C# expressions are built using operands, usually variables or types within your application, and operators. Many of the operators can be overloaded as well. Operator overloading is covered in Chapter 6. Table 3-1 lists the precedence of the operator groups. Entries at the top of the table have higher precedence, and operators within the same category have equal precedence.
Statements and Expressions
Statements in C# are identical in form to those of C++ and Java. A semicolon terminates one-lineexpressions. However, code blocks, such as those in braces in an if or a while statement, do not need to be terminated with a semicolon. Adding the semicolon is optional at the end of a block. Most of the statements that are available in C++ and Java are available in C#, including variable declaration statements, conditional statements, control flow statements, try/catch statements, and so on. However, C# (like Java) has some statements that are not available in C++. For example, C# provides a try/finally statement, which I discuss in detail in Chapter 7. In Chapter 12, I’ll show you the lock statement, which synchronizes access to code blocks by using the sync block of an object. C# also overloads the using keyword, so you can use it either as a directive or a statement.You can use a using statement in concert with the Disposable pattern I describe in Chapters 4 and 13. The foreach statement, which makes iterating through collections easier, also deserves mention. You’ll see more of this statement in Chapter 9, when I discuss arrays.
Reference Types
The garbage collector (GC) inside the CLR manages everything regarding the placement of objects.It can move objects at any time. When it moves them, the CLR makes sure that the variables that reference them are updated. Normally, you’re not concerned with the exact location of the object within the heap, and you don’t have to care if it gets moved around or not. There are rare cases, such as when interfacing with native DLLs, when you may need to obtain a direct memory pointer to an object on the heap. It is possible to do that using unsafe (or unmanaged) code techniques, but that is outside the scope of this book. Like the Java runtime, the CLR manages all references to objects on the heap. In C++, you must explicitly delete heap-based objects at some carefully chosen point. But in the managed environment of the CLR, the GC takes care of this for you. This frees you from having to worry about deleting objects from memory and minimizes memory leaks. The GC can determine, at any point in time, how many references exist to a particular object on the heap. If it determines there are none, it is free to start the process of destroying the object on the heap.
Default Variable Initialization
By default, the C# compiler produces what is called safe code. One of the safety concerns is making sure the program doesn’t use uninitialized memory. The compiler wants every variable to be set to a value before you use it, so it is useful to know how different types of variables are initialized.The default value for references to objects is null. At the point of declaration, you can optionally assign references from the result of a call to the new operator; otherwise, they will be set to null.When you create an object, the runtime initializes its internal fields. Fields that are references to objects are initialized to null, of course. Fields that are value types are initialized by setting all bits of the value type to zero. Basically, you can imagine that all the runtime is doing is setting the bits of the underlying storage to 0. For references to objects, that equates to a null reference, and for value types, that equates to a value of zero.For value types that you declare on the stack, the compiler does not zero-initialize the memory automatically. However, it does make sure that you initialize the memory before the value is used.
- 上一篇:软件可靠性测试英文文献和中文翻译
- 下一篇:模具热处理并行设计英文文献和中文翻译
-
-
-
-
-
-
-
河岸冲刷和泥沙淤积的监测国内外研究现状
乳业同业并购式全产业链...
电站锅炉暖风器设计任务书
当代大学生慈善意识研究+文献综述
酸性水汽提装置总汽提塔设计+CAD图纸
杂拟谷盗体内共生菌沃尔...
大众媒体对公共政策制定的影响
十二层带中心支撑钢结构...
java+mysql车辆管理系统的设计+源代码
中考体育项目与体育教学合理结合的研究