C# Advanced Concepts
Overview
There are several advanced features of C# which are very powerful.
Assembly
An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies are the building blocks of .NET applications. Assemblies has the code that the common language runtime executes.
Assemblies take the 2 forms
- executable (.exe)
<compiler command> <module name>
csc test.cs
- dynamic link library (.dll) files
<compiler command> -t:library <module name>
csc -t:library test.cs
- is similar to a class library that contains types that will be referenced by other assemblies
- has no entry point to begin execution
Note. Each assembly have only one entry point: DllMain, WinMain, or Main.
- Static assemblies
- stored on disk in portable executable (PE) files
- include interfaces, classes, and resources like bitmaps, JPEG files, and other resource files
- Dynamic assemblies
- run directly from memory and aren’t saved to disk before execution
- can save dynamic assemblies to disk after they have executed.
To use an assembly in an application, you must add a reference to it. To add a reference to a assembly. Use the following methods
- static
Load
method of theSystem.Reflection.Assembly
GetType
method of theType
class can load assembliesLoad
method of theSystem.AppDomain class
- …
Once an assembly is referenced, all the accessible types, properties, methods, and other members of its namespaces are available to your application as if their code were part of your source file.
Attributes
Attributes can add metadata (information about the types defined in a program) to the program. An attribute is actually an object that is associated with any of these elements: Assembly, Class, Method, Delegate, Enum, Event, Field, Interface, Property and Struct.
The attributes work by placing the name of the attribute enclosed in square brackets ([]) above the declaration of the entity to which it applies. It can contain parameters and the users can customize the attributes.
See here for more info.
In Unity, a public element with the attribute HideInInspector
won’t appear in the inspector of Unity Editor. See here for Unity attribute.
A sample of serialization attribute is in the Serialization and Deserialization below.
Exceptions
See here for more info.
Generics
Generics makes it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code.
A Generics class can also be inherited by other class. The derived class can specify the type of the class or it can inherit without specify the class. Generics class can also add Constrains to give some restrictions of the type.
1 | using System; |
See here for more about generic.
Interface
Just define the return type and type of parameters, the specific implementation needs to be finish in derived class, which looks like the header file in C++, that only decare but not implement.
1 | interface IMyInterface |
See here for more info.
Lambda expression
Lambda expression can be used for anonymous method. The operator =>
is used.
1 | Func<string> greet = () => "Hello, World!"; |
Properties
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field (Member variables or methods in a class or structures). Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.
1 | class Myclass |
In some cases, property get
and set
only return a value or assign a value, so the properties can be used auto-implemented to write. Note the code below only has the properties but no actual private field set. The required field of class of this kind of code will be set automatically during the compile.
1 | public class SaleItem |
The properties can also use body expression to simplify. Here we need to use the operator of =>
. It could link a member with an expression. Here we don’t need to write the keywords of return but still require an private field.
1 | public class SaleItem |
The abstract class may have an abstract property, which can be implemented in the derived class
1 | public abstract class Person { |
Serialization and Deserialization
It is the process of converting an object into a stream of bytes to store the object or transmit it to memory. The stream of bytes can be deserialized to the object.
To serialize an object, the object should be have a SerializableAttribute
attribute. Fields within a class that don’t need to be serialized should include a NonSerializedAttribute
attribute.
Users can use binary or XML serialization to serialize an object. Below is a sample to use basic serialization to serialize a class.
1 | using System; |
Thread
One thread if one execution flow for the code. For some parallel work in the same program, we can use multi-thread to save the total execution time.
See here for more information.
LINQ (Language Integrated Query)
Query expression is like the SQL language, it can search elements with some relationship. It starts with from
and ends with select
or group
.
LINQ is the data object language that integrated into C#. It can be used for search, order, compare and summarize action.
1 | List<User> users = new List<User> |
LINQ can be used in following scenarios.
- Objects: object’s in code. e.g. class, collectiosn
- XML: XML file
- SQL: SQL database
- Entities
- Datasets