c# basic
.net, asp.net, c#
c# is like java language specification;
.net is like jdk/javase/javaee
asp.net: is like springboot
- default, as, is
- sln: solution ——> csproj: c sharp project ——> files .sln vs .csproj
Key concepts
ref: c# concepts
- solution: a complete application, similar to maven project. It contains several c# project like frontend, backend, library to compose a complete application.
- project: similar to maven module. It can be a web project, a library, a windows program, etc.
- assembly: similar to maven jar. A c# project is corresponding to an assembly. An assembly can be a
dll
,exe
, etc. - namespace: similar to java package. It’s a logical concept to avoid naming conflicts while assembly is a physical concept. A namespace can be in different assemblies and an assembly can contains multiple namespaces.
Accessibility levels
ref: accessibility levels
- public: access is not restricted. (all)
- private: limited to containing type (only self)
- protected: limited to the containing class and types derived from the containing class. (sub-classes)
- internal: limited to the current assembly (only the same assembly)
- protected internal: limited to the current assembly or types derived from the containing class (same assembly & sub-classes)
- protected private: limited to the containing class and types derived from the containing class in the current assembly (sub-classes in same assembly)
data types
Nullable
Nullable
1 | int? x = null // int? is the shorthand for Nullable<int> |
Delegate
delegate is like @FunctionalInterface
in java.
A delegate type represents references to methods with a particular parameter list and return type. Delegates make it possible to treat methods as entities that can be assigned to variables and passed as parameters.
1 | delegate double Function(double x); // functional interface definition |
Method
Method contains parameters & return type & body & modifier & type parameters.
parameters
Argument is where the initial value is from, and parameter is used to pass value/references to methods.
Parameter has modifier (e.g. out, final, this, params) and type
Argument are passed as parameter in 4 ways:
value parameter:
parameter change won’t affect the argument
- only for input parameter passing
- optional by specify default value
reference parameter
parameter change will affect the argument
- for input/output parameter passing
1 | public void swap(ref int x, ref int y) |
output parameter
similar to reference parameter except for that the initial value is unimportant.
1 | public void divide(int x, int y, out int res, out int remainder){} |
parameter arrays
similar to java …
extension methods
extension method is a mechanism that you can “add method” to a class without extending from it.
This method:
- must be static
- works in scope when you explictly import the namespace into you source code with a
using
directive. - must be the first param of method.
- when used, it’s same as the normal instance method.
1 | namespace ExtensionMethods |