Tutorial Addendum On C# - Allotment B - Classes

sportsappsios strategygamesios utilitiesappsios
 31 December 18:00   

    



    



    

"const" and "readonly"

    



    

C# has two key words to anticipate ethics of variables to be afflicted unwantedly:

    

const and readonly. Actuality is a allegory of the two:

    



    

"const"

    



        

  • Automatically become "static"


  •     

  • Can alone be amount based types


  •     

  • Can alone be assigned in the acknowledgment statement


  •     

  • Can alone be assigned with an announcement evaluable at the compiler time


  •     



    



    



    

"readonly"

    



        

  • Can be "static" or non "static"


  •     

  • Can be amount or advertence based types


  •     

  • Can alone be assigned in the acknowledgment statement


  •     



    



    

The afterward sample program shows some absorbing features:

    

 

    

class Constants {

    

accessible const continued minimum = 1; // const is aswell static

    

accessible const continued best = 100;

    

accessible changeless readonly continued boilerplate = 50;

    

accessible changeless readonly cord columnist = " Yang";

    

accessible changeless readonly int seeds = new int {3, 7, 11};

    

accessible changeless abandoned Main() {

    

// minimum = 0; // appointment not allowed

    

// best = 99;

    

// boilerplate = 49; // appointment not allowed

    

// columnist = "Someone Else"; // appointment not allowed

    

// seeds = new int {13, 17, 19}; // can not change the reference

    

seeds = 5; // but, we can change amount it is referring

    

System.Console.WriteLine("Constants.minimum = {0}.",

    

Constants.minimum);

    

System.Console.WriteLine("Constants.maximum = {0}.",

    

Constants.maximum);

    

System.Console.WriteLine("Constants.average = {0}.",

    

Constants.average);

    

System.Console.WriteLine("Constants.author = {0}.",

    

Constants.author);

    

System.Console.WriteLine("Constants.seeds = {0}.",

    

Constants.seeds);

    

}

    

}

    



    



    

Output:

    

 

    

Constants.minimum = 1.

    

Constants.maximum = 100.

    

Constants.average = 50.

    

Constants.author = Yang.

    

Constants.seeds = 5.

    



    



    



    

Method Overload

    



    

A adjustment is active if there is addition adjustment with the aforementioned adjustment signature existing

    

in the aforementioned class. A adjustment signature includs the afterward elements:

    



        

  • Method name.


  •     

  • Number of parameters.


  •     

  • Parameter type.


  •     

  • Parameter modifier, except "params".


  •     



    



    

and excludes the afterward elements:

    



        

  • Return type.


  •     

  • Method modifier.


  •     



    

The afterward program shows the how a adjustment can be active differently:

    

 

    

class Overloads {

    

accessible changeless abandoned Main() {

    

Print(1);

    

Print(12345678901234);

    

Print((object)2);

    

Print(3, 4);

    

Print(new int{5,6});

    

}

    

accessible changeless abandoned Print(object o) {

    

System.Console.WriteLine("Print(object o): {0}.", o.ToString());

    

}

    

accessible changeless abandoned Print(int i) {

    

System.Console.WriteLine("Print(int i): {0}.", i);

    

}

    

accessible changeless abandoned Print(long i) {

    

System.Console.WriteLine("Print(long i): {0}.", i);

    

}

    

accessible changeless abandoned Print(ref int i) {

    

System.Console.WriteLine("Print(ref int i): {0}.", i);

    

}

    

accessible changeless abandoned Print(int i, int j) {

    

System.Console.WriteLine("Print(int i, int j): {0}, {1}.", i, j);

    

}

    

// accessible changeless abandoned Print(int a) {

    

// // identical to Print(params int a)

    

// System.Console.WriteLine("Print(int a): {0}, {1}.", a,

    

// a);

    

// }

    

accessible changeless abandoned Print(params int a) {

    

System.Console.WriteLine("Print(params int a): {0}, {1}.",

    

a,a);

    

}

    

}

    



    



    

 


Tags: system, assignment, class, average, minimum, seeds, allowed, value, method, author, console, assigned, public, object, print

 print, constants, static, public, system, console, writeline, method, const, seeds, readonly, author, average, maximum, minimum, following, params, value, object, class, assignment, allowed, assigned, , public static, system console, console writeline, static void, writeline print, void print, print int, {0} constants, writeline constants, {0} {1}, print params, params int, print object, constants maximum, new int, static readonly, assignment not, constants minimum, constants author, constants average, constants seeds, system console writeline, public static void, static void print, console writeline print, console writeline constants, public static readonly, static void main, public const long,

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