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

public struct Vector64

{

public Vector64(long x, long y)

{

X = x; Y = y;

}

public long X { get; } public long Y { get; }

public override string ToString() => $"{{{X}, {Y}}}";

}

Example

var vector1 = new Vector32(15, 39); var vector2 = new Vector32(87, 64);

Console.WriteLine(vector1 == vector2); // false

Console.WriteLine(vector1 != vector2); // true

Console.WriteLine(vector1 + vector2); // {102, 103}

Console.WriteLine(vector1 - vector2); // {-72, -25}

Section 52.28: char

A char is single letter stored inside a variable. It is built-in value type which takes two bytes of memory space. It represents System.Char data type found in mscorlib.dll which is implicitly referenced by every C# project when you create them.

There are multiple ways to do this.

1.char c = 'c';

2.char c = '\u0063'; //Unicode

3.char c = '\x0063'; //Hex

4.char c = (char)99;//Integral

A char can be implicitly converted to ushort, int, uint, long, ulong, float, double, or decimal and it will return the integer value of that char.

ushort u = c;

returns 99 etc.

However, there are no implicit conversions from other types to char. Instead you must cast them.

ushort u = 99; char c = (char)u;

Section 52.29: params

params allows a method parameter to receive a variable number of arguments, i.e. zero, one or multiple arguments are allowed for that parameter.

GoalKicker.com – C# Notes for Professionals

250

static int AddAll(params int[] numbers)

{

int total = 0;

foreach (int number in numbers)

{

total += number;

}

return total;

}

This method can now be called with a typical list of int arguments, or an array of ints.

AddAll(5, 10, 15, 20);

//

50

AddAll(new int[] { 5, 10, 15, 20 });

//

50

 

 

 

params must appear at most once and if used, it must be last in the argument list, even if the succeeding type is

di erent to that of the array.

Be careful when overloading functions when using the params keyword. C# prefers matching more specific overloads before resorting to trying to use overloads with params. For example if you have two methods:

static double Add(params double[] numbers)

{

Console.WriteLine("Add with array of doubles"); double total = 0.0;

foreach (double number in numbers)

{

total += number;

}

return total;

}

static int Add(int a, int b)

{

Console.WriteLine("Add with 2 ints"); return a + b;

}

Then the specific 2 argument overload will take precedence before trying the params overload.

Add(2, 3);

//prints "Add with 2 ints"

Add(2,

3.0);

//prints "Add with array of doubles" (doubles are not ints)

Add(2,

3, 4);

//prints "Add with array of doubles" (no 3 argument overload)

 

 

 

Section 52.30: while

The while operator iterates over a block of code until the conditional query equals false or the code is interrupted with a goto, return, break or throw statement.

Syntax for while keyword:

while( condition ) { code block; }

Example:

GoalKicker.com – C# Notes for Professionals

251

int i = 0; while (i++ < 5)

{

Console.WriteLine("While is on loop number {0}.", i);

}

Output:

"While is on loop number 1." "While is on loop number 2." "While is on loop number 3." "While is on loop number 4." "While is on loop number 5."

Live Demo on .NET Fiddle

A while loop is Entry Controlled, as the condition is checked before the execution of the enclosed code block. This means that the while loop wouldn't execute its statements if the condition is false.

bool a = false;

while (a == true)

{

Console.WriteLine("This will never be printed.");

}

Giving a while condition without provisioning it to become false at some point will result in an infinite or endless loop. As far as possible, this should be avoided, however, there may be some exceptional circumstances when you need this.

You can create such a loop as follows:

while (true)

{

//...

}

Note that the C# compiler will transform loops such as

while (true)

{

// ...

}

or

for(;;)

{

// ...

}

into

{

:label

GoalKicker.com – C# Notes for Professionals

252

// ...

goto label;

}

Note that a while loop may have any condition, no matter how complex, as long as it evaluates to (or returns) a boolean value (bool). It may also contain a function that returns a boolean value (as such a function evaluates to the same type as an expression such as `a==x'). For example,

while (AgriculturalService.MoreCornToPick(myFarm.GetAddress()))

{

myFarm.PickCorn();

}

Section 52.31: null

A variable of a reference type can hold either a valid reference to an instance or a null reference. The null reference is the default value of reference type variables, as well as nullable value types.

null is the keyword that represents a null reference.

As an expression, it can be used to assign the null reference to variables of the aforementioned types:

object a = null; string b = null; int? c = null; List<int> d = null;

Non-nullable value types cannot be assigned a null reference. All the following assignments are invalid:

int a = null; float b = null; decimal c = null;

The null reference should not be confused with valid instances of various types such as:

an empty list (new List<int>())

an empty string ("")

the number zero (0, 0f, 0m)

the null character ( '\0' )

Sometimes, it is meaningful to check if something is either null or an empty/default object. The System.String.IsNullOrEmpty(String) method may be used to check this, or you may implement your own equivalent method.

private void GreetUser(string userName)

{

if (String.IsNullOrEmpty(userName))

{

//The method that called us either sent in an empty string, or they sent us a null reference. Either way, we need to report the problem.

throw new InvalidOperationException("userName may not be null or empty.");

}

else

{

//userName is acceptable.

Console.WriteLine("Hello, " + userName + "!");

}

GoalKicker.com – C# Notes for Professionals

253