منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب
استخدام Readonly و Constant في ال C# - نسخة قابلة للطباعة

+- منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب (http://vb4arb.com/vb)
+-- قسم : الأقسام التعليمية - المنتدى القديم (http://vb4arb.com/vb/forumdisplay.php?fid=90)
+--- قسم : مكتبة أكواد المنتدى (http://vb4arb.com/vb/forumdisplay.php?fid=111)
+---- قسم : مكتبة أكواد .net (http://vb4arb.com/vb/forumdisplay.php?fid=117)
+---- الموضوع : استخدام Readonly و Constant في ال C# (/showthread.php?tid=6096)



استخدام Readonly و Constant في ال C# - RaggiTech - 17-10-12

كاتب الموضوع : AhmedEssawy

كود :
// NOTE: This code snippet is designed for VS.NET 2005 or later.
public class Test
{
// Note that constants are static by default and readonly
// variables are not.
// A constant, "Hello!", must be provided at the time the constant
// is:
public const string Hello = "Hello!";
// This constant will also work:
public const int Number = 1;
// This will throw an exception because DateTime.Now is NOT a
public constant public const DateTime Now = DateTime.Now;
// This constant will throw an error because it's not set:
public const short ShortNumber;
// A readonly, "Hey!", can be set at runtime or at declaration time
// and doesn't need to be a literal value:
public readonly String Hey1 = "Hey!";
// Since a readonly variable doesn't need to be set at declaration
// time, the following will work; variable Hey2 can be set ONLY in
// the constructor of the class:
public readonly String Hey2;
// Unlike constant, readonly variables can have non-literal values:
public readonly DateTime RightNow = DateTime.Now;
}