菜单
  
    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.
  1. 上一篇:软件可靠性测试英文文献和中文翻译
  2. 下一篇:模具热处理并行设计英文文献和中文翻译
  1. 汽车内燃机连杆载荷和应...

  2. 机械手系统英文文献和中文翻译

  3. 固体氧化物燃料电池英文文献和中文翻译

  4. 船舶运动仿真系统英文文献和中文翻译

  5. 新能源空调系统设计英文文献和中文翻译

  6. 正交试验回归法和响应曲...

  7. 机械设计制造及其自动化英文文献和中文翻译

  8. 江苏省某高中学生体质现状的调查研究

  9. NFC协议物理层的软件实现+文献综述

  10. 巴金《激流三部曲》高觉新的悲剧命运

  11. 浅析中国古代宗法制度

  12. g-C3N4光催化剂的制备和光催化性能研究

  13. 上市公司股权结构对经营绩效的影响研究

  14. 高警觉工作人群的元情绪...

  15. 中国传统元素在游戏角色...

  16. 现代简约美式风格在室内家装中的运用

  17. C++最短路径算法研究和程序设计

  

About

优尔论文网手机版...

主页:http://www.youerw.com

关闭返回