Tutorial Addendum On C# - Allotment B - Classes

sportsappsios strategygamesios utilitiesappsios
 31 December 18:00   

    



    



    

Output:

    

 

    

Print(int i): 1.

    

Print(long i): 12345678901234.

    

Print(object o): 2.

    

Print(int i, int j): 3, 4.

    

Print(params int[] i): 5, 6.

    



    



    

Two absorbing notes:

    



        

  • Print(12345678901234) is mapped to Print(long i) because the accurate is accustomed

        

    as "long".

        



  •     

  • Print(1,2) could aswell be mapped to Print(params int[] a), but I assumption Print(int i,

        

    int j) has college precedency. Or this is a anemic point in the accent design?

        



  •     



    



    



    

Properties

    



    

Properties are like fields. Ethics can be assigned to and retrieved from backdrop in

    

the aforementioned way as fields. But the acknowledgment of backdrop requires a get adjustment and a

    

set method. If a amount is assigned to a property, its set adjustment will be executed.

    

Similarly, if the amount is retrieved from a property, its get adjustment will be

    

executed. There is no anamnesis anon allocated to a property. If the amount is truely

    

needed to be stored, use addition acreage to help.

    



    

The afterward program illustrates how a acreage can be used:

    

 

    

using System;

    

class Backdrop {

    

clandestine cord a;

    

accessible cord columnist { // acreage declaration

    

get {

    

acknowledgment a;

    

}

    

set {

    

a = value; // amount is an absolute parameter

    

}

    

}

    

accessible changeless abandoned Main() {

    

Backdrop p = new Properties();

    

p.author = " Yang";

    

Console.WriteLine("p.author = {0}.", p.author);

    

}

    

}

    



    



    

Output:

    

 

    

p.author = Yang.

    



    



    



    

Operators

    



    

Operators are appropriate methods in a class, which will acquiesce altar of this class

    

to adjure those methods in a syntax agnate to the syntax of expression.

    

The afterward program shows a simple bill chic with some operators defined:

    

 

    

using System;

    

class Bill {

    

clandestine continued v;

    

accessible Currency(long value) {

    

v = value*100;

    

}

    

accessible Currency(double value) {

    

// amount is angled down afterwards two decimal digits

    

v = (long)(value*100.0);

    

}

    

accessible changeless absolute abettor double(Currency a) {

    

acknowledgment a.v/100.0;

    

}

    

accessible changeless absolute abettor Currency(double a) {

    

acknowledgment new Currency(a);

    

}

    

accessible changeless Bill operator+(Currency a, Bill b) {

    

Bill c = new Currency(0);

    

c.v = a.v+b.v;

    

acknowledgment c;

    

}

    

accessible changeless bool operator==(Currency a, Bill b) {

    

acknowledgment a.v == b.v;

    

}

    

accessible changeless bool operator!=(Currency a, Bill b) {

    

acknowledgment a.v != b.v;

    

}

    

accessible override cord ToString() {

    

if (v%100==0) acknowledgment (v/100).ToString()+".00$";

    

abroad acknowledgment (v/100).ToString()+"."+(v%100).ToString()+"$";

    

}

    

accessible changeless abandoned Main() {

    

Bill a = new Currency(100); // constructor

    

Bill b = 100.125; // about-face operator

    

Bill c = b + b; // + operator

    

Bill d = 100.1249;

    

Console.WriteLine("a = {0}.",a); // overrided ToString()

    

Console.WriteLine("b = {0}.",b);

    

Console.WriteLine("c = {0}.",c);

    

Console.WriteLine("b==d = {0}.",b==d); // == operator

    

}

    

}

    



    



    

Output:

    

 

    

a = 100.00$.

    

b = 100.12$.

    

c = 200.24$.

    

b==d = True.

    



    

 


Tags: class, property, value, method, author, console, currency, notes, public, string, properties, print

 currency, public, print, return, value, operator, static, tostring, console, writeline, author, property, properties, method, class, notes, string, double, implicit, , public static, operator currency, console writeline, new currency, print int, static bool operator, bool operator currency, public static bool, static implicit operator, static void main, public static implicit, public static void,

Share Tutorial Addendum On C# - Allotment B - Classes:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  c-sharp