gustavo-fior / study

A place to keep my notes and links on various subjects
0 stars 0 forks source link

c# #2

Open gustavo-fior opened 1 year ago

gustavo-fior commented 1 year ago

I've been coding in Java for couple of years now, so my notes are gonna be based on the differences between Java and C#

gustavo-fior commented 1 year ago
gustavo-fior commented 1 year ago

as far as I searched, there's no available version of visual studio (not vs code) for Linux :/

gustavo-fior commented 1 year ago

Packages and namespaces

using System.IO;
using DataTier = Acme.SQLCode.Client;

namespace AcmeAccounting
{
    public class GetDetails
    {
        // ...
    }
}

namespace AcmeFinance
{
    public class ShowDetails
    {
        // ...
    }
}
gustavo-fior commented 1 year ago

Data types

Short Name .NET Class Type Width Range (bits)
byte Byte Unsigned integer 8 0 to 255
sbyte SByte Signed integer 8 -128 to 127
int Int32 Signed integer 32 -2,147,483,648 to 2,147,483,647
uint UInt32 Unsigned integer 32 0 to 4294967295
short Int16 Signed integer 16 -32,768 to 32,767
ushort UInt16 Unsigned integer 16 0 to 65535
long Int64 Signed integer 64 -9223372036854775808 to 9223372036854775807
ulong UInt64 Unsigned integer 64 0 to 18446744073709551615
float Single Single-precision floating point type 32 -3.402823e38 to 3.402823e38
double Double Double-precision floating point type 64 -1.79769313486232e308 to 1.79769313486232e308
char Char A single Unicode character 16 Unicode symbols used in text
bool Boolean Logical Boolean type 8 True or false
object Object Base type of all other types    
string String A sequence of characters    
decimal Decimal Precise fractional or integral type that can represent decimal numbers with 29 significant digits 128 ±1.0 × 10e−28 to ±7.9 × 10e28
gustavo-fior commented 1 year ago

Constants

Enums

public enum Color
{
    Green,   //defaults to 0
    Orange,  //defaults to 1
    Red,     //defaults to 2
    Blue     //defaults to 3
}  

public enum Color2
{
    Green = 10,
    Orange = 20,
    Red = 30,
    Blue = 40
}
gustavo-fior commented 1 year ago

String

gustavo-fior commented 1 year ago

Value and Reference Types

in c# we have:

System.Console.WriteLine(i); // 30 System.Console.WriteLine(k); // 10


- **type reference** -> will point to the same object, which means if you made a change through ee2, ee1 will also be updated
```cs
Employee ee1 = new Employee();
Employee ee2 = ee1;
gustavo-fior commented 1 year ago

Operators

Category Symbol
Type of Operand typeof
Size of Operand sizeof
Enforce Overflow Checking checked
Suppress Overflow Checking unchecked
gustavo-fior commented 1 year ago

Checked and Unchecked operations

class TestCheckedAndUnchecked
{
    static void Main()
    {
        short a = 10000;
        short b = 10000;

        short c = (short)(a * b);                     // unchecked by default 
        short d = unchecked((short)(10000 * 10000));  // unchecked 
        short e = checked((short)(a * b));            // checked - run-time error

        System.Console.WriteLine(10000 * 10000);  // 100000000
        System.Console.WriteLine(c);              // -7936
        System.Console.WriteLine(d);              // -7936
        System.Console.WriteLine(e);              // no result
    }    
}
gustavo-fior commented 1 year ago

Switches

gustavo-fior commented 1 year ago

Foreach


static void Main()
{
    string[] arr= new string[] {"Jan", "Feb", "Mar"};

    foreach (string s in arr)
    {
        System.Console.WriteLine(s);
    }
}
gustavo-fior commented 1 year ago

Access modifiers

gustavo-fior commented 1 year ago

Main method

static int Main(string[] args)
{
    //... 
    return 0;
}
gustavo-fior commented 1 year ago

Passing by reference/value

if a parameter is a primitive, it will be passed as a value, if it's an object it will be passed as a reference. if you want to pass primitives as reference, use keyword out or ref.

gustavo-fior commented 1 year ago

Infinite params in method