.NET Diary

October 9, 2013

static vs. const vs. readOnly

Filed under: All, ASP.NET, C# — Tags: , , , , , , , — leoullas @ 10:06 pm

static vs. const vs. readOnly 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, and static methods and properties can only access static fields and static events.
const • Can’t be static. As it is static by default.
• Value is evaluated at compile time.  So, value can’t be changed at runtime.
• Must be initialized as they are declared.
• 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.
readonly • Can be either instance-level or static.
• Value is evaluated at run time.  So, value can be changed at runtime.
• Can be initialized in declaration or by code in the constructor.
N.B: readonly exists solely to prevent anyone from, either accidentally or intentionally, changing the value of a variable once it’s set. It is enforced at run-time.
const is similar, but enforced at compile-time. Thus, the value must be set at the time the variable is created.
Advertisements

Different types of Parameters in C#


Different types of Parameters in C#: Parameters are means of passing values to a method.
There are four different ways of passing parameters to a method in C#.
1. Value default parameter type in C#.
actual value is passed to the function, which means changes made to the parameter is local to the function and is not passed back to the calling part.
2. Out “out” parameters are output only parameters meaning they can only pass back a value from a function.
3. Ref “ref” parameters are input/output parameters meaning they can be used for passing a value to a function as well as to get back a value from a function.
4. Params “params” parameters is a very useful feature in C#. “params” parameter are used when through a variable number of arguments need to be passed. The “params” should be a single dimensional or a jagged array.
Ref vs. Out //Difference: Ref vs. Out
public void funcRefOut()
{
int a = 0, b, c = 0, d = 0;//Assigning b = 0 will not make any effect, it should be assigned a value in calling funcd = ParamTest(a, out b, ref c);
richTextBox1.Text = richTextBox1.Text + “\n” + “a = {0} ” + a;
richTextBox1.Text = richTextBox1.Text + “\n” + “b = {0} ” + b;
richTextBox1.Text = richTextBox1.Text + “\n” + “c = {0} ” + c;
richTextBox1.Text = richTextBox1.Text + “\n” + “d = {0} ” + d;
}public static int ParamTest(int a, out int b, ref int c)
{
a = 10;
b = 20;//Value assign must
c = 30;//Value assign not compulsury
//b = 20;//Error 1 The out parameter ‘b’ must be assigned to before control leaves the current method
//c = 30;// ref type may not be assigned
return 40;
}
C# Value
vs.
Ref Type
.NET uses two different areas of Ram for holding variables;. the stack & heap.
fixed in size when the application is linked Stack [LIFO – bucket]
holds value type variables plus return addresses for functions. All numeric types, ints, floats and doubles along with enums, chars, bools and structs are value types.
basically all unallocated memory. Heap
holds variables created dynamically- known as reference variables and mainly instances of classes(objects) or strings. These variables are stored in two places; there’s a hidden pointer to the place in the heap where the data is stored.
– String
– All arrays, even if their elements are value types
– Class types, such as Form
– DelegatesImportant:
string str = “new string”;//1 address space
str = “modified” + str; //different address and not same address as above
value type is derived from System.ValueType while a reference type is derived from System.Object.
If you assign a value type variable to another then a direct copy of the value is made. But copying a reference type variable just makes a copy of the reference to the variable and does not affect the variable itself.
unlike a value type, a reference type can be null.
implicit conversion vs. explicit conversion 1. Explicit conversions require a cast operator where as an implicit converstion is done automatically.
2. Explicit conversion can lead to data loss where as with implicit conversions there is no data loss.
Will the following code compile?
double d = 9999.11;
int i = d;No, the above code will not compile. Double is a larger data type than integer. An implicit conversion is not done automatically bcos there is a data loss. Hence we have to use explicit conversion as shown below.double d = 9999.11;
int i = (int)d; //Cast double to int.
N.B: InvalidCastException occurs if casting fails
Boxing storing a value type variable in a reference variable
int i = 99;
object t=(object)i;
N.B: boxing happens implicitly
Unboxing storing a reference type variable in a  value type variable
object box = 12;
int i = (int)box;
N.B: – As performance gets impacted, try to avoid boxing and unboxing. In a project where you need boxing and unboxing, use it when it’s absolutely necessary.
– unboxing is an explicit conversion

Create a free website or blog at WordPress.com.

%d bloggers like this: