Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

}

inheritance and implementation

Only types that inherit from a certain base class or implement a certain interface can be supplied.

public class Cup<T> where T : Beverage

{

// ...

}

public class Cup<T> where T : IBeer

{

// ...

}

The constraint can even reference another type parameter:

public class Cup<T, U> where U : T

{

// ...

}

Multiple constraints can be specified for a type argument:

public class Cup<T> where T : class, new()

{

// ...

}

The previous examples show generic constraints on a class definition, but constraints can be used anywhere a type argument is supplied: classes, structs, interfaces, methods, etc.

where can also be a LINQ clause. In this case it is analogous to WHERE in SQL:

int[] nums = { 5, 2, 1, 3, 9, 8, 6, 7, 2, 0 };

var query =

from num in nums where num < 5 select num;

foreach (var n in query)

{

Console.Write(n + " ");

}

// prints 2 1 3 2 0

Section 52.46: int

int is an alias for System.Int32, which is a data type for signed 32-bit integers. This data type can be found in mscorlib.dll which is implicitly referenced by every C# project when you create them.

Range: -2,147,483,648 to 2,147,483,647

int int1 = -10007;

GoalKicker.com – C# Notes for Professionals

267

var int2 = 2132012521;

Section 52.47: ulong

Keyword used for unsigned 64-bit integers. It represents System.UInt64 data type found in mscorlib.dll which is implicitly referenced by every C# project when you create them.

Range: 0 to 18,446,744,073,709,551,615

ulong veryLargeInt = 18446744073609451315;

var anotherVeryLargeInt = 15446744063609451315UL;

Section 52.48: true, false

The true and false keywords have two uses:

1. As literal Boolean values

var myTrueBool = true; var myFalseBool = false;

2. As operators that can be overloaded

public static bool operator true(MyClass x)

{

return x.value >= 0;

}

public static bool operator false(MyClass x)

{

return x.value < 0;

}

Overloading the false operator was useful prior to C# 2.0, before the introduction of Nullable types. A type that overloads the true operator, must also overload the false operator.

Section 52.49: struct

A struct type is a value type that is typically used to encapsulate small groups of related variables, such as the coordinates of a rectangle or the characteristics of an item in an inventory.

Classes are reference types, structs are value types.

using static System.Console;

namespace ConsoleApplication1

{

struct Point

{

public int X; public int Y;

public override string ToString()

{

return $"X = {X}, Y = {Y}";

GoalKicker.com – C# Notes for Professionals

268

}

public void Display(string name)

{

WriteLine(name + ": " + ToString());

}

}

class Program

{

static void Main()

{

var point1 = new Point {X = 10, Y = 20};

// it's not a reference but value type var point2 = point1;

point2.X = 777; point2.Y = 888;

point1.Display(nameof(point1)); // point1: X = 10, Y = 20 point2.Display(nameof(point2)); // point2: X = 777, Y = 888

ReadKey();

}

}

}

Structs can also contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types, although if several such members are required, you should consider making your type a class instead.

Some suggestions from MS on when to use struct and when to use class:

CONSIDER

defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

AVOID

defining a struct unless the type has all of the following characteristics:

It logically represents a single value, similar to primitive types (int, double, etc.)

It has an instance size under 16 bytes.

It is immutable.

It will not have to be boxed frequently.

Section 52.50: extern

The extern keyword is used to declare methods that are implemented externally. This can be used in conjunction with the DllImport attribute to call into unmanaged code using Interop services. which in this case it will come with static modifier

For Example:

using System.Runtime.InteropServices; public class MyClass

{

[DllImport("User32.dll")]

private static extern int SetForegroundWindow(IntPtr point);

GoalKicker.com – C# Notes for Professionals

269

public void ActivateProcessWindow(Process p)

{

SetForegroundWindow(p.MainWindowHandle);

}

}

This uses the SetForegroundWindow method imported from the User32.dll library

This can also be used to define an external assembly alias. which let us to reference di erent versions of same components from single assembly.

To reference two assemblies with the same fully-qualified type names, an alias must be specified at a command prompt, as follows:

/r:GridV1=grid.dll /r:GridV2=grid20.dll

This creates the external aliases GridV1 and GridV2. To use these aliases from within a program, reference them by using the extern keyword. For example:

extern alias GridV1; extern alias GridV2;

Section 52.51: bool

Keyword for storing the Boolean values true and false. bool is an alias of System.Boolean.

The default value of a bool is false.

bool b; // default value is false b = true; // true

b = ((5 + 2) == 6); // false

For a bool to allow null values it must be initialized as a bool?.

The default value of a bool? is null.

bool? a // default value is null

Section 52.52: interface

An interface contains the signatures of methods, properties and events. The derived classes defines the members as the interface contains only the declaration of the members.

An interface is declared using the interface keyword.

interface IProduct

{

decimal Price { get; }

}

class Product : IProduct

{

const decimal vat = 0.2M;

public Product(decimal price)

GoalKicker.com – C# Notes for Professionals

270