Shiman’s Weblog

Collecting stones from the sea-shore.

C# .NET Method Parameter Modifiers

All the Methods, both static and instance level, tend to take parameters passed in by the caller. However, unlike some programming languages, C# provides a set of parameter modifiers that control how arguments are sent into (and possibly returned from) a given method, as shown in the Table.

Parameter Modifier

Meaning

(none)

If a parameter is not marked with a parameter modifier, it is assumed to be passed by value, meaning the called method receives a copy of the original data.
out Output parameters are assigned by the method being called (and therefore passed by reference). If the called method fails to assign output parameters, you are issued a compiler error.
params This parameter modifier allows you to send in a variable number of identically typed arguments as a single logical parameter. A method can have only a single params modifier, and it must be the final parameter of the method.
ref The value is initially assigned by the caller, and may be optionally reassigned by the called method (as the data is also passed by reference). No compiler error is generated if the called method fails to assign a ref parameter.

May 29, 2008 Posted by shiman | C#.Net, Computer Science, OOP, Programming | , , , , | No Comments Yet

Best Practices (Class Modifiers in C# .NET)

Every class modifier has specific purposes. One of the best practices is to use appropriate modifiers to meet those purposes.

abstract

The purpose of making a class abstract is to generalize classes on some common criteria but to prevent from creating instance. Class declared as abstract, it cannot be newed. An abstract class can be derived from another class, it can implement interfaces, and other class can be derived from the abstract class and make the abstract members concrete. If anyone wants to build a class which should not be instantiate but should be sub-classed, the class should be defined as abstract.

Sealed

The purpose of making a class sealed is to prevent inheritance. A sealed class can be derived from another class but cannot be inherited by other classes. If anyone wants to make a class which should not be further subclassed, the class should be declared sealed.

Static

The purpose of making a static class is to prevent both instantiation and inheritance. Usually utiliy classes are made static. static classes are sealed by default. They cannot inherite any class except System.Object and cannot implement any interfaces.

May 29, 2008 Posted by shiman | C#.Net, Computer Science, OOP, Programming | , , , , | 1 Comment