Tutorial Addendum On C# - Allotment B - Classes
| |
"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 Interface' onMouseOver="tip('infobox1')" onMouseOut="untip()" target='_parent'> 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);
}
}
|
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, |
Also see ...
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:br
In adjustment to acknowledgment some of the questions aloft in the antecedent sections, let s yield a abutting attending at how absolute numbers are represented in a computer system.In C, the IEEE 754 single precision and doub
Rule 7: If the backer basic abundance all 1s, the atom componentis aloof for addition operation uses.For abrogating values, aggregate is identical with the absolute numbers,except the assuranc
accessible abandoned outputDebugText() { int i; Console.WriteLine("======"); Console.WriteLine("Converting a absolute amount to IEEE Stardard 754"); Console.WriteLine("Binary Amphibia