• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[C#]C#Basics(1)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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,
but are limited to public or internal. (Nested type can be private).

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
time, and therefore a constant member cannot be assigned to an object reference (whose value is
computed at runtime)

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
if you wish to create an unchangeable field whose initial value is not known until runtime? Then read-only fields in the choice.

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:
• A given class (or structure) may define only a single static constructor.
• A static constructor executes exactly one time, regardless of how many objects of the type are created.
• A static constructor does not take an access modifier and cannot take any parameters.
• The runtime invokes the static constructor when it creates an instance of the class or before accessing the first static member invoked by the caller.
• The static constructor executes before any instance-level 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
a hash-based collection such as System.Collections.Hashtable. Under the hood, the Hashtable type
calls the Equals() and GetHashCode() members of the contained types to determine the correct object
to return to the caller.

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
character data.

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:  

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#树类型及其遍历发布时间:2022-07-10
下一篇:
C#控制台窗口居中显示(转)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap