While deepen in C#, I noted some significant points of that. You can use this to recap or
refresh your language knowledge - or just to deepen like me.
Define Variables
Variables can only be used (allocated), after they are defined.
break and continue
While iterations, the break command predicates to leave the complete iteration. With
continue, you can leave the current loop and goon with the next one.
params[] Modifier
Methods-Parameter can be modified as params[]. There, you
can pass as lot variables as you wish, based on the type you defined as params[] - but you
must not, if you won't. (Using an array, you must know how many variables to pass - and
you must pass)
public void MyMethod(int Iin, string SIn, params[] long SomeLongParams)
There are some rules using the params[] modifier:
- only one parameter of a method can be modified as params[]
- the parameter defined as params[] stays at the end of the parameter list
- cant be set as ref or out
- is one-dimensional
- you don't have to pass any variables through (because it's optional)
Using properties
Note that you can do any operations in the properties, if you want. For example if you
have to check an input value for validation, you can write it like:
public int Num
{
get{return _Num;}
set{if(value >0) value = _Num;}
}
You can use sharing-modificators for "getter" and "setter" using properties but take in mind:
- both, the get and the set Accessor must be defined
- but only one of them can be modified
- used sharing-modificator must be restrictered than the modificator of the property
readonly and const
Both modificators are similar. The difference:
- readonly can be also defined in constructors, const not (imagine, you havn't a value till runtime)
- readonly is invoked by an object-reference, const by the class-name
Constructor
What happens, calling a constructor of a class?
- arrange required space for the object
- initialize the object
Overloaded constructors allows a variable/dynamic initialization of an object (because of the
variable set of parameters, variables and methods). Take in mind, that the
standard-constructor (the constructor that will be used if you don't define one) is public!
You can build a hirarchial (linked) constructor-calling. Instructions in constructors that are
needed many times can be packed in own constructors. The called constructor by creating a
new object can first call another constructors (and that another again...) before it initializie
itself. This method is used to reduce code-redundancy.
Example
public Circle(double Radius)
{
this.Radius = Radius;
}
public Circle(double Radius, int X, int Y) : this(Radius)
{
this.XKoordinate = X;
this.YKoordinate = Y;
}
Short-Lived Objects
Creating an object to solve some operations and delete it after well done job to save
memory-ressources is a nice idea! And here the sample:
using(ClassA Obj =
new ClassA
()){ Obj.
AnyOperation();
}}
When does it euqals?
Objects equals, when references show to the same object. Objects are identic when
references has their own objects.
Check Equality between two Objects
- Equals-Method
- ReferenceEquals-Method
- == operator
static and instance (classes and objects)
In object-methods you can modify the class-variables and call class-methods. The reverse
way (calling object-methods and modify object variables with static methods) is not possible.
Take in mind to use data encapsulation, like in objects, in classes (static variables), too!