Each type in C# is either a value type or a reference type. C# has several predefined ("builtin") types and allows for declaration of custom value types and reference types.
All IT and Non IT Interview Questions, MCQs, Online Quiz Questions, Engineering VIVA Questions
Home » All posts
Wednesday, 4 January 2012
Parameter
Parameters are variables associated with a method.
An in parameter may either have its value passed in from the callee to the method's environment, so that changes to the parameter by the method do not affect the value of the callee's variable,or passed in by reference, so that changes to the variables will affect the value of the callee's variable. Value types (int, double, string) are passed in "by value" while reference types (objects) are passed in "by reference." Since this is the default for the C# compiler, it is not necessary to use .
An out parameter does not have its value copied, thus changes to the variable's value within the method's environment directly affect the value from the callee's environment. Such a variable is considered by the compiler to be unbound upon method entry, thus it is illegal to reference an out parameter before assigning it a value. It also must be assigned by the method in each valid (non-exceptional) code path through the method in order for the method to compile.
A reference parameter is similar to an out parameter, except that it is bound before the method call and it need not be assigned by the method.
A params parameter represents a variable number of parameters. If a method signature includes one, the params argument must be the last argument in the signature.
// Each pair of lines is what the definition of a method and a call of a
// method with each of the parameters types would look like.
// In param:
void MethodOne(int param1) //definition
MethodOne(variable); //call
// Out param:
void MethodTwo(out string message) //definition
MethodTwo(out variable); //call
// Reference param;
void MethodThree(ref int someFlag) //definition
MethodThree(ref theFlag) //call
// Params
void MethodFour(params string[] names) //definition
MethodFour("Matthew", "Mark", "Luke", "John"); //call
An in parameter may either have its value passed in from the callee to the method's environment, so that changes to the parameter by the method do not affect the value of the callee's variable,or passed in by reference, so that changes to the variables will affect the value of the callee's variable. Value types (int, double, string) are passed in "by value" while reference types (objects) are passed in "by reference." Since this is the default for the C# compiler, it is not necessary to use .
An out parameter does not have its value copied, thus changes to the variable's value within the method's environment directly affect the value from the callee's environment. Such a variable is considered by the compiler to be unbound upon method entry, thus it is illegal to reference an out parameter before assigning it a value. It also must be assigned by the method in each valid (non-exceptional) code path through the method in order for the method to compile.
A reference parameter is similar to an out parameter, except that it is bound before the method call and it need not be assigned by the method.
A params parameter represents a variable number of parameters. If a method signature includes one, the params argument must be the last argument in the signature.
// Each pair of lines is what the definition of a method and a call of a
// method with each of the parameters types would look like.
// In param:
void MethodOne(int param1) //definition
MethodOne(variable); //call
// Out param:
void MethodTwo(out string message) //definition
MethodTwo(out variable); //call
// Reference param;
void MethodThree(ref int someFlag) //definition
MethodThree(ref theFlag) //call
// Params
void MethodFour(params string[] names) //definition
MethodFour("Matthew", "Mark", "Luke", "John"); //call
Local variables
Like fields, local variables can optionally be constant (const). Constant local variables are stored in the assembly data region, while non-constant local variables are stored (or referenced from) the stack. They thus have both a scope and an extent of the method or statement block that declares them.
Fields
Fields, sometimes called class-level variables, are variables associated with classes or structures. An instance variable is a field associated with an instance of the class or structure, while a static variable, declared with the static keyword, is a field associated with the type itself. Fields can also be associated with their class by making them constants (const), which requires a declaration assignment of a constant value and prevents subsequent changes to the field.
Each field has a visibility of public, protected, internal, protected internal, or private (from most visible to least visible).
Each field has a visibility of public, protected, internal, protected internal, or private (from most visible to least visible).
Fields, Local Variables, and Parameters
C# supports several program elements corresponding to the general programming concept of variable: fields, parameters, and local variables.
Variables
Variables are used to store values. More technically, a variable binds an object (in the general sense of the term, i.e. a specific value) to an identifier (the variable's name) so that the object can be accessed later. Variables can, for example, store a value for later use:
string name = "Dr. Jones";
Console.WriteLine("Good morning " + name);
In this example "name" is the identifier and "Dr. Jones" is the value that we bound to it. Also, each variable is declared with an explicit type.
Only values whose types are compatible with the variable's declared type can be bound to (stored in) the variable. In the above example we stored "Dr. Jones" into a variable of the type string. This is a legal statement. However, if we had said int name = "Dr. Jones", the compilerwould have thrown an error telling us
that you cannot implicitly convert between int and string. There are methods for doing this, but we will talk about them later.
string name = "Dr. Jones";
Console.WriteLine("Good morning " + name);
In this example "name" is the identifier and "Dr. Jones" is the value that we bound to it. Also, each variable is declared with an explicit type.
Only values whose types are compatible with the variable's declared type can be bound to (stored in) the variable. In the above example we stored "Dr. Jones" into a variable of the type string. This is a legal statement. However, if we had said int name = "Dr. Jones", the compilerwould have thrown an error telling us
that you cannot implicitly convert between int and string. There are methods for doing this, but we will talk about them later.
Constants
Pascal Case. The use of SCREAMING_CAPS is discouraged. This is a large change from earlier conventions. Most developers now realize that in using SCREAMING_CAPS they betray more implementation than is necessary. A large portion of the .NET Framework Design Guidelines is dedicated to this discussion.
Example:
Here is an example of a class that uses all of these naming conventions combined.
using System;
namespace MyExampleNamespace
{
public class Customer : IDisposable
{
private string _customerName;
public string CustomerName
{
get
{
return _customerName;
}
set
{
_customerName = value;
_lastUpdated = DateTime.Now;
}
}
private DateTime _lastUpdated;
public DateTime LastUpdated
{
get
{
return _lastUpdated;
}
private set
{
_lastUpdated = value;
}
}
public void UpdateCustomer(string newName)
{
if( !newName.Equals(customerName))
{
CustomerName = newName;
}
}
public void Dispose()
{
//Do nothing
}
}
}
Example:
Here is an example of a class that uses all of these naming conventions combined.
using System;
namespace MyExampleNamespace
{
public class Customer : IDisposable
{
private string _customerName;
public string CustomerName
{
get
{
return _customerName;
}
set
{
_customerName = value;
_lastUpdated = DateTime.Now;
}
}
private DateTime _lastUpdated;
public DateTime LastUpdated
{
get
{
return _lastUpdated;
}
private set
{
_lastUpdated = value;
}
}
public void UpdateCustomer(string newName)
{
if( !newName.Equals(customerName))
{
CustomerName = newName;
}
}
public void Dispose()
{
//Do nothing
}
}
}
Subscribe to:
Comments (Atom)