Datatypes and Operators

data types are provided in the System namespace. All of them, however, have a C# alias. These aliases are keywords in the C# language

The integral types

The default value of all integral types is 0. All of these types define two constants called MinValue and MaxValue, which provide the minimum and maximum value of the type.

Integral literals, which are numbers that appear directly in code (such as 0, -42, and so on), can be specified as decimal, hexadecimal, or binary literals. Decimal literals do not require any suffix. Hexadecimal literals are prefixed with 0x or 0X, and binary literals are prefixed with 0b or 0B. An underscore (_) can be used as a digit separator with all numeric literals.

int dec = 32;
int hex = 0x2A;
int bin = 0b_0010_1010;

An integral value without any suffix is inferred by the compiler as int. To indicate a long integer, use l or L for a signed 64-bit integer and ul or UL for an unsigned 64-bit integer.

The floating-point types

The default value for floating-point types is 0. These types also define two constants called MinValue and MaxValue that provide the minimum and maximum value of the type. However, these types also provide constants that represent not-a-number (System.Double.NaN) and infinity (System.Double.NegativeInfinity and System.Double.PositiveInfinity)

var a = 42.99;//double
float b = 19.50f;
System.Double c = -1.23;

The decimal type

The decimal type can represent up to 28 decimal places or 128 bits. is important to note that the decimal type minimizes errors during rounding but does not eliminate the need for rounding. For instance, the result of the operation 1m / 3 * 3 is not 1 but 0.9999999999999999999999999999. On the other hand, Math.Round(1m / 3 * 3) yields the value 1.

decimal a = 42.99m;
var b = 12.45m;
System.Decimal c = 100.75M;

The char type

16 bits

char a = 'A';
char b = '\x0065';
char c = '\u15FE';

The string type

A string is an array of characters. In C#, the type for representing a string is called string and is an alias for the .NET System.String. You can use any of these two types interchangeably. Internally, a string contains a read-only collection of char objects. This makes strings immutable, which means that you cannot change a string but need to create a new one every time you want to modify the content of an existing string. Strings are not null-terminated (unlike other languages such as C++) and can contain any number of null characters ('\0'). 

string s1;                       // unitialized
string s2 = null;                // initialized with null
string s3 = String.Empty;        // empty string
string s4 = "hello world";       // initialized with text
var s5 = "hello world";
System.String s6 = "hello world";
char[] letters = { 'h', 'e', 'l', 'l', 'o'};
string s7 = new string(letters); // from an array of chars
char c = s4[0];  // OK
s4[0] = 'H';     // error
var s8 = s6.Remove(5);       // hello
var s9 = s6.ToUpper();       // HELLO WORLD

formats

int i = 42;
string s1 = "This is item " + i.ToString();
string s2 = string.Format("This is item {0}", i);
string s3 = $"This is item {i}";
string s4 = @"\b\w+\b";

The object type

The object type is the base type for all other types in C#, even though you do not specify this explicitly, as we will see in the following chapters. The object keyword in C# is an alias for the .NET System.Object type. You can use these two interchangeably.

GetType() method, which is not virtual and which returns a System.Type object with information about the type of the current instance.

Equals()  behavior is different for reference and value types. for reference types, this method performs reference equality; this means it checks whether the two variables point to the same object on the heap. For value types, it performs value equality; this means that the two variables are of the same type and that the public and private fields of the two objects are equal.

The default value of a variable of the object type is null

Reference types and value types

value types All user-defined types declared as structures (with the struct keyword) are value types. Although all types are implicitly derived from the object, type value types do not support explicit inheritance
Value types are typically stored on the stack in memory

int a = 20;
DateTime dt = new DateTime(2019, 12, 25);
int a = 20;
int b = a;  // b is 20
a = 42;     // a is 42, b is 20

reference type does not contain the value directly but a reference to a memory location where the actual value is stored. The built-in data types object and string are reference types. Arrays, interfaces, delegates, and any user-defined type defined as a class are also called reference types

Nullable types

Reference types have the default value null, which indicates that a variable is not assigned to the instance of any object. Value types do not have such an option. However, there are cases when no value is a valid value for a value type too. To represent such cases, you can use a nullable type.
A nullable type is an instance of System.Nullable, a generic value type that can represent the values of an underlying T type, which can only be a value type, as well as an additional null value.

Nullable<int> a;
Nullable<int> b = null;
Nullable<int> c = 42;
int? a;
int? b = null;
int? c = 42;

You can use the HasValue property to check whether a nullable type object has a value, and Value to access the underlying value:

if (c.HasValue)
    Console.WriteLine(c.Value);
string? s1 = null; // OK, nullable type
string s2 = null;  // error, non-nullable type
  • You assign values to a nullable type object the same way you would assign to the underlying type.
  • You can use the GetValueOrDefault() method to get either the assigned value or the default value of the underlying type if no value is assigned.
  • Boxing is performed on the underlying type. If the nullable type object has not assigned any value, the result of boxing is a null object.
  • You can use the null-coalescing operator, ??, to access the value of the object of a nullable type (for example, int d = c ?? -1;).

Array

int[] arr1;
int[] arr2 = null;
int[] arr3 = new int[6];
int[] arr4 = new int[] { 1, 1, 2, 3, 5, 8 };
int[] arr5 = new int[6] { 1, 1, 2, 3, 5, 8 };
int[] arr6 = { 1, 1, 2, 3, 5, 8 };
foreach(int element in arr6)
 Console.WriteLine(element);
int[,] arr1;
arr1 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] arr2 = null;
int[,] arr3 = new int[2,3];
int[,] arr4 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] arr5 = new int[2,3] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] arr6 = { { 1, 2, 3 }, { 4, 5, 6 } };
int[][] arr1;
int[][] arr2 = null;
int[][] arr3 = new int[2][];
arr3[0] = new int[3];
arr3[1] = new int[] { 1, 1, 2, 3, 5, 8 };
int[][] arr4 = new int[][]
{
   new int[] { 1, 2, 3 },
   new int[] { 1, 1, 2, 3, 5, 8 }
};
int[][] arr5 =
{
   new int[] { 1, 2, 3 },
   new int[] { 1, 1, 2, 3, 5, 8 }
};
int[][,] arr6 = new int[][,]
{
    new int[,] { { 1, 2}, { 3, 4 } },
    new int[,] { {11, 12, 13}, { 14, 15, 16} }
};

Type Conversion

string s = "example";
object o = s;          // implicit conversion
string r = (string)o;  // explicit conversion
public readonly struct fancyint
{
    private readonly int value;
    public fancyint(int value)
    {
        this.value = value;
    }
    public static implicit operator int(fancyint v) => v.value;
    public static explicit operator fancyint(int v) => new fancyint(v);
    public override string ToString() => $"{value}";
}
fancyint a = new fancyint(42);
int i = a;                 // implicit conversion
fancyint b = (fancyint)i;  // explicit conversion
DateTime dt1 = DateTime.Parse("2019.08.31");
DateTime.TryParse("2019.08.31", out DateTime dt2);
int i1 = int.Parse("42");          // successful, i1 = 42
int i2 = int.Parse("42.15");       // error, throws exception
int.TryParse("42.15", out int i3); // error, returns false, 
                                   // i3 = 0

Operators

The null-conditional operators

The null-conditional operator has two forms: ?. (also known as the Elvis operator) to apply member access and ?[] to apply element access for an array. These operators apply the operation to their operand if and only if that operand is not null. Otherwise, the result of applying the operator is also null

if(a is null)
   a = b;
//or
a??=b;

understand C compilers and build system and debugers

  • CMake — build system generator
    • You use this define how to compile your code
  • gcc/g++/clang/clang++ — compilers
    • compilers turn your code into binaries
    • gcc and g++ are the GNU C and C++ compilers, respectively
    • clang and clang++ are the LLVM compilers, respectively
    • Use CMake to define how to invoke compilers on the source code files
  • gdb/lldb — debuggers
    • gdb is a debugger provided by GNU
    • lldb is a debugger provided by LLVM
    • Use these to detect issues when running your binaries
    • It generally does not matter which compiler was used to generate the binary, i.e. you can use LLDB to debug a code compiled with a GNU compiler or vice versa

diffrence between make and cmake

Make (or rather a Makefile) is a buildsystem – it drives the compiler and other build tools to build your code.

CMake is a generator of buildsystems. It can produce Makefiles, it can produce Ninja build files, it can produce KDEvelop or Xcode projects, it can produce Visual Studio solutions. From the same starting point, the same CMakeLists.txt file. So if you have a platform-independent project, CMake is a way to make it buildsystem-independent as well.

If you have Windows developers used to Visual Studio and Unix developers who swear by GNU Make, CMake is (one of) the way(s) to go.

cmake -G"MSYS Makefiles"  when compiling for MinGW
cmake -G "Visual Studio 16 2019" -A Win32  when compiling for vs
cmake -G "Visual Studio 16 2019" -A x64 when compiling for vs

for more about cmake

GCC and G++

The GNU C and C++ compiler are called gcc and g++, respectively.

// Compile and link source file hello.c into executable a.exe (Windows) or a (Unixes)
> gcc hello.c
  
// (Unixes / Mac OS X) In Bash shell
$ gcc -o hello hello.c
$ chmod a+x hello
$ ./hello

// (Windows) In CMD shell
> g++ -o hello.exe hello.cpp
   // Compile and link source hello.cpp into executable hello.exe
> hello
// Execute under CMD shell

// (Unixes / Mac OS X) In Bash shell
$ g++ -o hello hello.cpp
$ chmod a+x hello
$ ./hello
$ g++ -Wall -g -o Hello.exe Hello.cpp
-o: specifies the output executable filename.
-Wall: prints "all" Warning messages.
-g: generates additional symbolic debuggging information for use with gdb debugger.
// Compile-only with -c option
> g++ -c -Wall -g Hello.cpp
// Link object file(s) into an executable
> g++ -g -o Hello.exe Hello.o
-c: Compile into object file "Hello.o". By default, the object file has the same name as the source file with extension of ".o" (there is no need to specify -o option). No linking with other object files or libraries.
Linking is performed when the input file are object files ".o" (instead of source file ".cpp" or ".c"). GCC uses a separate linker program (called ld.exe) to perform the linking.

Suppose that your program has two source files: file1.cppfile2.cpp. You could compile all of them in a single command:

g++ -o myprog.exe file1.cpp file2.cpp 

However, we usually compile each of the source files separately into object file, and link them together in the later stage. In this case, changes in one file does not require re-compilation of the other files.

> g++ -c file1.cpp
> g++ -c file2.cpp
> g++ -o myprog.exe file1.o file2.o
gcc program.o -llib1 -Wl,-Bstatic -llib2 -Wl,-Bdynamic -llib3 // link dynamic and static filels and object to executable file
> cpp hello.c > hello.i //preprocessing 
> gcc -S hello.i //compilation generate assembly code
> as -o hello.o hello.s //assembler generate machine code 
> ld -o hello.exe hello.o ...libraries... linker and generate excutable machine code
g++ --std=c++2a test2.cpp -o test2 //select c++ version

GNU Make

First Makefile By Example

// hello.c
#include <stdio.h>
 
int main() {
    printf("Hello, world!\n");
    return 0;
}
all: hello.exe

hello.exe: hello.o
	 gcc -o hello.exe hello.o

hello.o: hello.c
	 gcc -c hello.c
     
clean:
	 rm hello.o hello.exe

Run the “make” utility as follows:

> make
gcc -c hello.c
gcc -o hello.exe hello.o

Boost Libraries

Windows Install
Ubuntu Install C++
Ubuntu Install

boost tutorial

coroutine

With Boost.Coroutine it is possible to use coroutines in C++. Coroutines are a feature of other programming languages, which often use the keyword yield for coroutines. In these programming languages, yield can be used like return. However, when yield is used, the function remembers the location, and if the function is called again, execution continues from that location.

#include <boost/coroutine/all.hpp>
#include <iostream>

using namespace boost::coroutines;

void cooperative(coroutine<void>::push_type &sink)
{
  std::cout << "Hello";
  sink();
  std::cout << "world";
}

int main()
{
  coroutine<void>::pull_type source{cooperative};
  std::cout << ", ";
  source();
  std::cout << "!\n";
}
using namespace boost::coroutines;
void cooperative(coroutine<int>::push_type &sink)
{
    int i=0;
  sink(++i);
  sink(++i);
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    coroutine<int>::pull_type source{cooperative};
    qDebug()<<source.get();
    if(source){
        qDebug()<<source().get();
    }
    return a.exec();
}
//1 2
#include <boost/coroutine/all.hpp>
#include <stdexcept>
#include <iostream>

using boost::coroutines::coroutine;

void cooperative(coroutine<void>::push_type &sink)
{
  sink();
  throw std::runtime_error("error");
}

int main()
{
  coroutine<void>::pull_type source{cooperative};
  try
  {
    source();
  }
  catch (const std::runtime_error &e)
  {
    std::cerr << e.what() << '\n';
  }
}

Rotation Transform

basic 2D Rotation

Rotation About an Arbitrary Axis

Homogeneous Coordinates

Four-Dimensional Transforms

Transforming Normal Vectors

Quaternion Mathematics

rotation with quaternion

Spherical Linear Interpolation

bash command lines

Builtin Bash

echo string string string : multiple string
echo ~ : tilde expansion return user absolute path
echo "string string  string" : single string
echo $((arithmetic operation)) : arithmetic operations support + - * / % **
echo "x"{a,b,c} : xa xb xc brace expansion 
file f1 f2 f3  = file "f"{1,2,3} = file "f"{1..3}
echo $(ls x) : command sustitution syntax
ls dir | sort : pipeline commands 
ls -l *.html : wild card expansion
ls > file :to write output to the file 
ls >> file :to append output to the file 
command .................. &  : to run command in background

what is different between double quote and single quote
double-quote is prevent space expansion and tilde expansion and brace expansion but arithmetic expansion and parameter expansion and command substitution still performed in the other hand single quote will prevent all of them

& after command to let the command execute in the background

&& lets you do something based on whether the previous command completed successfully
for example in c++ if main function return -1 the command on right && never perform

The nohup command executes another program specified as its argument and ignores all SIGHUP (hangup) signals. SIGHUP is a signal that is sent to a process when its controlling terminal is closed.

apt install wget && wget url

for more resources visit
https://www.geeksforgeeks.org/basic-operators-in-shell-scripting/?ref=lbp

File System

cp originalFile newFile  :  copy original to new 
cp -r originalDir newDir : copy directory recursively

rm file or directory :  remove file or directory
rm -rf  directory :  force remove non-empty directory 
rm -r directory : remove directory recursively 

mkdir directory :  create directory 

mv fileFrom fileTo : move file from to 

ls director : display all directory content
ls -l dir: display files create date and owner and mod
ls -a dir: display all files including files start with dot(.)

cd path: set the current path

pwd : get current path

file filename : get file type like if it text or binary or symbolic link 

dir directory : diplay all directories inside directory

touch file : create file

cat filename file2name : display file content
cat -n filename : display file content with line number
cat > filename : to write to file
cat >> filename : to append to file
less filename : display file content
less -n filename : display file content with line number
head -n number filename : display the first number lines of the content 
tile -n number filename : display the last number lines of the content

ln -s targetfile linkname : create symbolic link for target file
readlink linkname : get the absolute path of the target this file linked to it  

which command : get absolute path for command

chmod {options} filename : change permision of file
chown  USERNAME:GROUPNAME path  :  change owner
chown -R USERNAME:GROUPNAME path : change owner recursive
chgrp group file1 file2 dir1  :  change group of file

zip|unzip file  
cat ./file.txt | grep "^hello .*ld.*$"
user@host:/home/user$ chmod ugo+rwx file4 #add read and write and execute permision for owner and group and others

System

lspci : pci hardware info
inxi : hardware info more readable need to install
free : memory size and how much used and free
dmesg : debug all devices
lsusb : all serial devices connected
top : monitor all processors
kill -9 pid : force kill for proccesor
ps -aux :to get all arunnig processors

Users

sudo : play command as root
sudo -i : always as root
adduser userName : add user
su : switch user
usermod -aG group user : add user to group
deluser user group : remove user from group
groups : display groups of current user
exit : logout
passwd : change passward of current user
adduser –system userName : add system user
addgroup group : add new group
userdel username : to delete user -r for remove every thing belong to user

note: if you want to add new command move your program to /usr/bin directory