Wednesday, August 17, 2011

Const vs. Readonly

const vs. readonly

const and readonly perform a similar function on data members, but they have a few important differences.



const

A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const keyword and must be initialized as they are declared. For example;

public class MyClass {   public const double PI = 3.14159; }

PI cannot be changed in the application anywhere else in the code as this will cause a compiler error.

Constants must be a value type (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool), an enumeration, a string literal, or a reference to null.

Since classes or structures are initialized at run time with the new keyword, and not at compile time, you can't set a constant to a class or structure.

Constants can be marked as public, private, protected, internal, or protected internal.

Constants are accessed as if they were static fields, although they cannot use the static keyword.

To use a constant outside of the class that it is declared in, you must fully qualify it using the class name.

A read only member is like a constant in that it represents an unchanging value. The difference is that a readonly member can be initialized at runtime, in a constructor as well being able to be initialized as they are declared. For example:

readonly

public class MyClass {   public readonly double PI = 3.14159; }

or

public class MyClass {   public readonly double PI;     public MyClass()   {     PI = 3.14159;   } }

Because a readonly field can be initialized either at the declaration or in a constructor, readonly fields can have different values depending on the constructor used. A readonly field can also be used for runtime constants as in the following example:

public static readonly uint l1 = (uint)DateTime.Now.Ticks;

Notes

  • readonly members are not implicitly static, and therefore the static keyword can be applied to a readonly field explicitly if required.
  • A readonly member can hold a complex object by using the new keyword at initialization.



static

Use of the static modifier to declare a static member, means that the member is no longer tied to a specific object. This means that the member can be accessed without creating an instance of the class. Only one copy of static fields and events exists, andstatic methods and properties can only access static fields and static events. For example:

public class Car {   public static int NumberOfWheels = 4; }

The static modifier can be used with classes, fields, methods, properties, operators, events and constructors, but cannot be used with indexers, destructors, or types other than classes.

static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member. For example:

int i = Car.NumberOfWheels;


18K白金和黄金谁贵

K白金与铂金的区别。从严格意义上讲,K白金不能等同于铂金。铂金是一种本身即呈天然白色的贵金属,目前是所有首饰贵金属中价格最高的,市场销售的铂金首饰都有“PT”标志,这是铂金的英文缩写。K白金是由黄金和其它金属熔炼而成的白色合金,所谓的18K白金其实是由75%的黄金和25%的其它金属合成的。因此,K白金不可以打上“PT”标志,只能按其纯度打上黄金及纯度的印。 18K是没有白金的,18K是指黄金,之所以是白色,那是因为另外25%的含其它的白色金属,导致是白色,所以称为白色的18K金,不是18K白金,另外因为18K金是含黄金75%,所以业内计算18K金的价格都是按千足金的75%的价格来计算,不过18K金一般会额外的加不少的工费,千足金现在一般是330-380元/克,品牌不同价格区域也不同,瘦的女的,一般佩带千足金黄金的项链5-10克就可以,胖的话就多加些,戒指一般3克4克左右常见。 所以千足黄金贵,18K的便宜

Thursday, August 4, 2011

Get Caller Class

Assuming you were going with something like cfeduke's answer above, you could also add an overload to your LogManager like this:

public static ILog GetLogger() {     var stack = new StackTrace();     var frame = stack.GetFrame(1);     return new Log4NetWrapper(frame.GetMethod().DeclaringType); } 

That way in your code you can now just use:

private static readonly ILogger _logger = LogManager.GetLogger(); 

instead of either of these:

private static readonly ILogger _logger =     LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static readonly ILogger _logger =      LogManager.GetLogger(typeof(YourTypeName)); 

Which is effectively equivalent of the first alternative (i.e. the one that usesMethodBase.GetCurrentMethod().DeclaringType), only a little simpler.