在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1. System.Environment and System.Console class Program
2: {
void Main()
4: {
5: Console.ForegroundColor = ConsoleColor.DarkGreen;
6:
string[] aArgs = Environment.GetCommandLineArgs();
in aArgs)
9: {
, aArg);
11: }
12:
, Environment.OSVersion);
14:
, Environment.CurrentDirectory);
16:
string[] aDrives = Environment.GetLogicalDrives();
in aDrives)
19: {
, aDrive);
21: }
22:
, Environment.Version);
24: }
25: }
2. String Formatting Flags 3. Type Visibility Types (classes, interfaces, structures, enumerations, and delegates) can also take accessibility modifiers, 4. Const Fields VS Read-Only Fields VS Static Read-Only Fields Const Fields: It is important to understand that the value assigned to a constant variable must be known at compile class ConstData
2: {
// The value assigned to a const must be known at compile time.
;
double SIMPLE_PI = 3.14;
true;
bool FALSITY = !TRUTH;
8: }
IL Sample:
'Timberwolves')
You can see the value is hard-coded directly into the assembly! And constant fields are implicitly static! Read-Only Fields As mentioned earlier, the value assigned to a constant must be known at compile time. However, what Unlike const however, read-only fields are not implicitly static; therefore if you wish to expose such data at class level, the static keyword must be included. class Tire
2: {
new Tire(90);
new Tire(100);
int manufactureID;
public Tire() {}
int ID)
8: { manufactureID = ID; }
9: }
Read-only fields have another distinction from constant data: their value may be assigned within the scope of a constructor. This can be very useful if the value to assign to a read-only field must be read from an external source (such as a text file or database). Static Read-Only Fields Unlike constant data, read-only fields are not implicitly static. If you wish to allow object users to obtain the value of a read-only field from the class level, simply make use of the static keyword: 5. Static Constructor
Static constructor is mainly used to initialize the static field. 虽然static field的值可以在声明的时候进行赋值,但是有时候可能该值不是简单的字面值或者一个expression就可以搞定,像new XXX()之类,如果需要进行好几个statement操作才可以得到该static field的初始值(比如说从数据库中取值),这个时候需要把这些操作放到一个方法里面才行。如果是放到instance constructor里面,这样每实例化一个对象的时候,这个static field就会被重新赋值一下,就失去了static field的真正意义,因此这个时候需要把这些操作放到static constructor中。 Here are a few points of interest regarding static constructors: 6. Static Classes
C# 2005 (c# 2.0) has widened the scope of the static keyword by introducing static classes.When a class has been defined as static, it is not creatable using the new keyword, and it can contain only static members or fields (if this is not the case, you receive compiler errors). At first glance, this might seem like a very useless feature, given that a class that cannot be created does not appear all that helpful. However, if you create a class that contains nothing but static members and/or constant data, the class has no need to be allocated in the first place. Prior to C# 2005, the only way to prevent an object user from creating such a type was to either redefine the default constructor as private or mark the class as an abstract type using the C# abstract keyword. 7. The Switch Statement One nice feature of the C# switch statement is that you can evaluate string data in addition to numeric data. 8. System.Object // The topmost class in the .NET universe: System.Object
namespace System
3: {
class Object
5: {
public Object();
virtual Boolean Equals(Object obj);
virtual Int32 GetHashCode();
public Type GetType();
virtual String ToString();
void Finalize();
protected Object MemberwiseClone();
object objB);
object objB);
15: }
16: }
static : Equals / ReferenceEquals virtual: Equals (reference comparison by default)/ GetHashCode protected: MemberwiseClone / Finalize Override GetHashCode() By and large, overriding this method is only useful if you intend to store a custom type within There are many algorithms that can be used to create a hash code—some fancy, others not so fancy. As mentioned, an object’s hash value will be based on its state data. As luck would have it, the System.String class has a very solid implementation of GetHashCode() that is based on the string’s 9. System.String You should be aware that although string is a reference type, the equality operators (== and !=) are defined to compare the value with the string objects, not the memory to which they refer. 10. Nullable Type )]
struct
3: {
bool hasValue;
value;
value);
bool HasValue { get; }
public T Value { get; }
public T GetValueOrDefault();
public T GetValueOrDefault(T defaultValue);
object other);
int GetHashCode();
string ToString();
value);
value);
16: }
)]
struct
3: {
bool hasValue;
value;
value)
7: {
value;
true;
10: }
11:
bool HasValue
13: {
14: get
15: {
this.hasValue;
17: }
18: }
public T Value
20: {
21: get
22: {
this.HasValue)
24: {
25: ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NoValue);
26: }
value;
28: }
29: }
public T GetValueOrDefault()
31: {
value;
33: }
34:
public T GetValueOrDefault(T defaultValue)
36: {
this.HasValue)
38: {
return defaultValue;
40: }
value;
42: }
43:
object other)
45: {
this.HasValue)
47: {
null);
49: }
null)
51: {
false;
53: }
value.Equals(other);
55: }
56:
int GetHashCode()
58: {
this.HasValue)
60: {
return 0;
62: }
value.GetHashCode();
64: }
65:
string ToString()
67: {
this.HasValue)
69: {
;
71: }
value.ToString();
73: }
74:
value)
76: {
value);
78: }
79:
value)
81: {
value.Value;
83: }
84: }
85:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论