Professional Documents
Culture Documents
Limitations of C#
Limitations of C#
Limitations of C#
Use ref keyword Class RefDemo { Public void func(ref int a, ref int b); }
Use Out Keyword Class RefDemo { Public void func(int a, out int b); }
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 ) ; } }
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;
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 } }