Assign value to class : Class Definition « Class Interface « C# / C Sharp






Assign value to class

Assign value to class
 
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// This program will not compile. 
 
class X { 
  int a; 
 
  public X(int i) { a = i; } 
} 
 
class Y { 
  int a; 
 
  public Y(int i) { a = i; } 
} 
 
public class IncompatibleRef { 
  public static void Main() { 
    X x = new X(10); 
    X x2;  
    Y y = new Y(5); 
 
    x2 = x; // OK, both of same type 
 
    x2 = y; // Error, not of same type 
  } 
}


           
         
  








Related examples in the same category

1.simulate a bank account
2.Using Initializers
3.A simple inventory exampleA simple inventory example
4.Declaring and Defining Classes
5.Declaring Class Instances
6.Declare class and use itDeclare class and use it
7.Illustrates how to declare classes, object references, and create objectsIllustrates how to declare classes, object references, and create objects
8.Illustrates how to assign default values to fields using initializersIllustrates how to assign default values to fields using initializers
9.illustrates how to use a 'has a' relationshipillustrates how to use a 'has a' relationship
10.Illustrates nested classesIllustrates nested classes
11.Multiple constructors in a class definitionMultiple constructors in a class definition
12.Demonstrate the use of a nested class to contain dataDemonstrate the use of a nested class to contain data
13.Show name hiding in a derived classShow name hiding in a derived class
14.Illustrates hidingIllustrates hiding
15.Variable in and out a classVariable in and out a class
16.Create classCreate class
17.A program that uses the Building classA program that uses the Building class
18.This program creates two Building objectsThis program creates two Building objects
19.Return an objectReturn an object
20.Uses a class from Example16_3a.cs
21.A Simple C# ClassA Simple C# Class
22.Persons Info class
23.Message class
24.Point class