- •class имя_класса [: класс, интерфейсы]
- •доступ возвращаемый_тип методN(список_параметров)
- •доступ возвращаемый_тип метод2(список_параметров)
- •// Instantiate at MathTest object
- •public static int GetSquareOf(int x)
- •class ParameterTest
- •return 0;
- •тип_элемента this[int индекс]
- •class FailSoftArray
- •//Возвратить логическое значение true, если
class имя_класса [: класс, интерфейсы]
{
//Объявления переменных экземпляра доступ тип переменная1; доступ тип переменная2;
//. . .
доступ тип переменнаяN;
// Объявления методов доступ возвращаемый_тип метод1(список_параметров)
{
// тело метода
}
доступ возвращаемый_тип метод2(список_параметров)
{
// тело метода
}
// . . .
доступ возвращаемый_тип методN(список_параметров)
{
// тело метода
}
}
struct имя_структуры [: интерфейсы]
{
//Объявления переменных экземпляра доступ тип переменная1; доступ тип переменная2;
//. . .
доступ тип переменнаяN;
// Объявления методов доступ возвращаемый_тип метод1(список_параметров)
{
// тело метода
}
доступ возвращаемый_тип метод2(список_параметров)
{
// тело метода
}
// . . .
доступ возвращаемый_тип методN(список_параметров)
{
// тело метода
}
}
1)
class PhoneCustomer
{
public const string DayOfSendingBill = "Monday"; public int CustomerlD;
public string FirstName; public string LastName;
}
2)
struct PhoneCustomerStruct
{
public const string DayOfSendingBill = "Monday"; public int CustomerlD;
public string FirstName; public string LastName;
}
3)
PhoneCustomer myCustomer = new PhoneCustomer();
//работает с классом PhoneCustomerStruct myCustomer2 = new
PhoneCustomerStruct();
//работает со структурой
4)
PhoneCustomer Customer1 = new PhoneCustomer (); Customer1.FirstName = "Simon";
5)
class PhoneCustomer
{
public const string DayOfSendingBill = "Monday"; public int CustomerID; public string FirstName; public string LastName;
}
6)
[модификаторы] тип_возврата ИмяМетода([параметры])
{
// Тело метода
}
7)
public bool IsSquare(Rectangle rect)
{
return (rect.Height == rect.Width);
}
8)
public bool IsPositive(int value);
{
if (value < 0) return false;
return true;
}
9)
using System; namespace Wrox
{
class MainEntryPoint
{
static void Main()
{
// Try calling some static functions Console.WriteLine("Pi is " + MathTest.GetPi()); int x = MathTest.GetSquareOf(5); Console.WriteLine("Square of 5 is " + x);
// Instantiate at MathTest object
MathTest math = new MathTest(); // this is C#'s way of
//
instantiating a reference type // Call non-static methods
math.Value = 30; Console.WriteLine(
"Value field of math variable contains " + math.Value);
Console.WriteLine("Square of 30 is " + math.GetSquare());
}
}
// Define a class named MathTest on which we will call a method
class MathTest
{
public int Value; public int GetSquare()
{
public static int GetSquareOf(int x)
{
return x*x;
}
public static double GetPi()
{
return 3.14159;
}
}
}
10)
Pi равно 3.14159
5 в квадрате равно 25
Поле value переменной math содержит 30 30 в квадрате равно 900 11)
using System; namespace Wrox
{
class ParameterTest
{
static void SomeFunction(int[] ints, int i)
{
ints[0] = 100; i = 100;
}
public static int Main()
{
int i = 0;
int[] ints = { 0, 1, 2, 4, 8 };
//Display the original values Console.WriteLine("i = " + i); Console.WriteLine("ints[0] = " + ints[0]); Console.WriteLine("Calling SomeFunction...");
//After this method returns, ints will be changed,
//but i will not
SomeFunction(ints, i); Console.WriteLine("i = " + i);
Console.WriteLine("ints[0] = " + ints[0]);
return 0;
}
}
}
12)
ParameterTest.exe i = 0 ints[0) = 0
Вызов SomeFunction...
i = 0
ints[0] = 100 13)
static void SomeFunction (int [ ] ints, ref int i)
{
ints[0] = 100; i = 100;
// изменение i сохранится после завершения SomeFunction ()
}
14)
SomeFunction(ints, ref i);
15)
static void SomeFunction (out int i)
{
i = 100;
}
public static int Main()
{
int i; // переменная i объявлена, но не инициализирована
SomeFunction(out i); Console.WriteLine(i); return 0;
}
16)
string FullName(string firstName, string lastName)
{
return firstName + " " + lastName;
}
17)
FullName("John", "Doe");
FullName(lastName: "Doe", firstName: "John");
18)
void TestMethod(int optionalNumber = 10, int notOptionalNumber)
{
System.Console.Write(optionalNumber + notOptionalNumber);
}
19)
class ResultDisplayer
{
void DisplayResult(string result)
{
// реализация
}
void DisplayResult(int result)
{
// реализация
}
}
20)
class MyClass
{
int DoSomething(int x)
// нужен 2-й параметр со значением по умолчанию
10
{
DoSomething(х, 10);
}
int DoSomething(int x, int y)
{
// реализация
}
}
21)
// mainForm относится к типу System.Windows.Forms mainForm.Height = 400;