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

sbyte, short, ushort, int, uint, long and ulong. Below is an enum with underlying type byte:

enum Days : byte

{

Sunday = 0, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

};

Also note that you can convert to/from underlying type simply with a cast:

int value = (int)NotableYear.EndOfWwI;

For these reasons you'd better always check if an enum is valid when you're exposing library functions:

void PrintNotes(NotableYear year)

{

if (!Enum.IsDefined(typeof(NotableYear), year))

throw InvalidEnumArgumentException("year", (int)year, typeof(NotableYear));

// ...

}

Section 52.62: partial

The keyword partial can be used during type definition of class, struct, or interface to allow the type definition to be split into several files. This is useful to incorporate new features in auto generated code.

File1.cs

namespace A

{

public partial class Test

{

public string Var1 {get;set;}

}

}

File2.cs

namespace A

{

public partial class Test

{

public string Var2 {get;set;}

}

}

Note: A class can be split into any number of files. However, all declaration must be under same namespace and the same assembly.

Methods can also be declared partial using the partial keyword. In this case one file will contain only the method definition and another file will contain the implementation.

GoalKicker.com – C# Notes for Professionals

275

A partial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type. Partial methods enable class designers to provide method hooks, similar to event handlers, that developers may decide to implement or not. If the developer does not supply an implementation, the compiler removes the signature at compile time. The following conditions apply to partial methods:

Signatures in both parts of the partial type must match.

The method must return void.

No access modifiers are allowed. Partial methods are implicitly private.

-- MSDN

File1.cs

namespace A

{

public partial class Test

{

public string Var1 {get;set;} public partial Method1(string str);

}

}

File2.cs

namespace A

{

public partial class Test

{

public string Var2 {get;set;} public partial Method1(string str)

{

Console.WriteLine(str);

}

}

}

Note: The type containing the partial method must also be declared partial.

Section 52.63: event

An event allows the developer to implement a notification pattern.

Simple example

public class Server

{

// defines the event

public event EventHandler DataChangeEvent;

void RaiseEvent()

{

var ev = DataChangeEvent; if(ev != null)

{

ev(this, EventArgs.Empty);

}

GoalKicker.com – C# Notes for Professionals

276

}

}

public class Client

{

public void Client(Server server)

{

// client subscribes to the server's DataChangeEvent server.DataChangeEvent += server_DataChanged;

}

private void server_DataChanged(object sender, EventArgs args)

{

// notified when the server raises the DataChangeEvent

}

}

MSDN reference

Section 52.64: sbyte

A numeric type used to store 8-bit signed integers. sbyte is an alias for System.SByte, and takes up 1 byte of memory. For the unsigned equivalent, use byte.

Valid range is -127 to 127 (the leftover is used to store the sign).

sbyte a

= 127;

//

127

sbyte b

= -127; // -127

sbyte

c

=

200;

//

Error, cannot be converted

sbyte

d

=

unchecked((sbyte)129); // -127 (overflows)

 

 

 

 

 

 

GoalKicker.com – C# Notes for Professionals

277

Chapter 53: Object Oriented Programming

In C#

This topic try to tell us how we can write programs based on OOP approach.But we don't try to teach Object Oriented Programming paradigm. We'll be covering following topics: Classes,Properties,Inheritance,Polymorphism,Interfaces and so on.

Section 53.1: Classes:

Skeleton of declaring class is:

<>:Required

[]:Optional

[private/public/protected/internal] class <Desired Class Name> [:[Inherited class][,][[Interface Name 1],[Interface Name 2],...]

{

//Your code

}

Don't worry if you can't understand whole syntax,We'll be get familiar with all part of that.for first example consider following class:

class MyClass

{

int i = 100;

public void getMyValue()

{

Console.WriteLine(this.i);//Will print number 100 in output

}

}

in this class we create variable i with int type and with default private Access Modifiers and getMyValue() method with public access modifiers.

GoalKicker.com – C# Notes for Professionals

278