Limitations of C#

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

Use ref and out Parameters

Use ref keyword Class RefDemo { Public void func(ref int a, ref int b); }

Call this function in main function as follows Func(ref a, ref b);

Use Out Keyword Class RefDemo { Public void func(int a, out int b); }

Call this function in main function as follows Func(a, out b);

Use a Variable Number of Arguments


When you create a method, you usually know in advance the number of arguments that you will be passing to it, but this is not always the case. Such a method cannot be created using normal parameters. Instead, you must use a special type of parameter that stands for an arbitrary number of parameters. This is done by creating a params parameter. The params modifier is used to declare an array parameter that will be able to receive zero or more arguments using System; class Min { publ i c int MinVal (params in t [ ] nums) { int m; i f ( nums.Length == 0) { Console .Wr i teL i ne ( "Er ro r : no arguments. " ) ; return 0; } m = nums[0] ; for ( i n t i=1; i < nums.Length; i++) i f ( nums[ i ] < m) m = nums[i ] ; return m; } } class ParamsDemo { stat i c void Main() { Min ob = new Min() ; int min; int a = 10, b = 20; / / Cal l with 2 values . min = ob.MinVal (a , 30); Console .Wr i teL i ne ( "Min imum i s " + min); / / Can cal l with an int array, too. int [ ] args = { 45, 67, 34, 9, 112, 8 }; min = ob.MinVal (a rgs ) ; Console .Wr i teL i ne ( "Min imum i s " + min); } }

The output from the program is shown here:


Minimum i s 10 Minimum i s 8

Object Initializers
C# 3.0 added a new feature called object initializers that provides another way to create an object and initialize its fields and properties. (See Chapter 10 for a discussion of properties.) Using object initializers, you do not call a class constructor in the normal way. Rather, you specify the names of the fields and/or properties to be initialized, giving each an initial value. Thus, the object initializer syntax provides an alternative to explicitly invoking a class constructor. The primary use of the object initializer syntax is with anonymous types created in a LINQ expression. (Anonymous types and LINQ are described in Chapter 19.) However, because the object initializers can be used (and occasionally are used) with a named class, the fundamentals of object initialization are introduced here. Lets begin with a simple example:
/ / A simple demonstrat ion that uses object in i t i a l i z e r s . using System; class MyClass { publ i c int Count; publ i c str i ng Str ; } class Obj In i tDemo { stat i c void Main() { / / Construct a MyClass object by using object in i t i a l i z e r s . MyClass obj = new MyClass { Count = 100, Str = "Test ing" }; Console .Wr i teL ine (ob j .Count + " " + obj .S t r ) ; } }

This produces the following output:


100 Test ing

Static:
1. Outside the class, to use a static member, you must specify the name of its class followed by the dot operator. 2. No object needs to be created. 3. In fact, a static member cannot be accessed through an object reference. 4. It must be accessed through its class name. 5. A static variable is initialized before its class is used. 6. If no explicit initializer is specified, it is initialized to zero for numeric types, null in the case of reference types, or false for variables of type bool. Thus, a static variable always has a value. For example, if you want to assign the value 10 to a static variable called count that is part of a class called Timer, use this line:
Timer.count = 10;

There are several restrictions that apply to static methods:


7. A static method does not have a this reference. This is because a static method does not execute relative to any object. 8. A static method can directly call only other static methods of its class. It cannot directly call an instance method of its class. The reason is that instance methods operate on specific objects, but a static method is not called on an object. Thus, on what object would the static method operate? 9. A similar restriction applies to static data. A static method can directly access only other static data defined by its class. It cannot operate on an instance variable of its class because there is no object to operate on.

Operator Overloading Syntax:


There are two forms of operator methods: one for unary operators and one for binary operators. The general form for each is shown here: // General form for overloading a unary operator public static ret-type operator op(param-type operand) { // operations } // General form for overloading a binary operator public static ret-type operator op(param-type1 operand1, param-type1 operand2) { // operations }

Ex:
/ / Overload unary - . publ i c stat i c ThreeD operator - (ThreeD op) { ThreeD resul t = new ThreeD() ; resul t . x = - op.x; resul t . y = - op.y; resul t . z = - op.z ; return resul t ; } / / Overload Binary +. publ i c stat i c ThreeD operator +(ThreeD op1, ThreeD op2) { ThreeD resul t = new ThreeD() ; /* This adds together the coordinates of the two points and returns the resul t . */ resul t . x = op1.x + op2.x; / / These are in teger addi t i ons resul t . y = op1.y + op2.y; / / and the + reta ins i t s or ig ina l resul t . z = op1.z + op2.z ; / / meaning re lat i ve to them. return resul t ;

Indexers
Creating One-Dimensional Indexers A one-dimensional indexer has this general form: element-type this[int index] { // The get accessor get { // return the value specifi ed by index } // The set accessor set { // set the value specifi ed by index } }

You might also like