, - . . new, , .
( ):
//
class Square: Rectangle {
public Square() {
}
public Square(int size): base(size, size) {
}
public new int Width {
get { return base.Width;}
set { base.Width = value; base.Height = value; }
}
public new int Height {
get { return base.Height;}
set { base.Width = value; base.Height = value; }
}
}
public static void Main(string[] args) {
Square square = new Square(5);
Console.WriteLine(square.Height);//5
square.Width = 6;
Console.WriteLine(square.Height);//6
Console.ReadKey();
}
, .
() . , ( ).
:
- ;
- ;
- , ;
- ;
- ;
- .
- try-catch. :
try {
// ,
} catch ([ ] []) {
//
}
try, , , , catch, catch. , try . catch, try-catch .
. , . , , (Message), (StackTrace), . .
, catch , .
:
public static void Main(string[] args) {
string result = "";
Console.WriteLine(" :");
try {
int a = Convert.ToInt32(Console.ReadLine()); // ,
result = " " + a;
|
|
} catch (FormatException) {
result = ". ";
}
Console.WriteLine(result);
Console.ReadLine();
}
C# . , . C# System.Exception. : SystemException (, CLR) ApplicationException ( ).
ApplicationException. - : , , , , Message InnerException, .
:
class RegistrationFailedException: ApplicationException {
public RegistrationFailedException(): base() {}
public RegistrationFailedException(string message): base(message) {}
public RegistrationFailedException(string message, Exception innerException): base(message, innerException) {}
public RegistrationFailedException(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context): base(info, context) {}
private string[] errors;
public RegistrationFailedException(string[] errors): base("Registation failed!"){
this.errors = errors;
}
public void printErrors() {
for (int i = 0; i < errors.Length; i++) {
Console.WriteLine(errors[i]);
}
}
}
public static void Main(string[] args) {
try {
Console.WriteLine(" :");
string login = Console.ReadLine();
Console.WriteLine(" :");
string password = Console.ReadLine();
Console.WriteLine(" :");
string passwordSubmit = Console.ReadLine();
Console.WriteLine(" email:");
string email = Console.ReadLine();
string errors = "";
if (login.Length < 6 || login.Length > 32) {
errors += " 6..32 .";
}
if (password.Length < 6 || password.Length > 32) {
errors += "& 6..32 .";
}
if (password!= passwordSubmit) {
errors += "& .";
}
if (email == "[email protected]") {
errors += "& .";
}
if (errors.Length > 0) {
throw new RegistrationFailedException(errors.Split('&'));
} else {
//save user info
}
} catch (RegistrationFailedException e) {
e.printErrors();
}
Console.ReadLine();
}
:
:
user1
:
pass
:
password
email:
6..32 .
6..32 .
.
.
9
, - . . . . public, virtual static ( -). . . , . - .
|
|
, , , , , - . , , , , ; , -.