caiouechi / Studies

5 stars 1 forks source link

C# #129

Open caiouechi opened 4 years ago

caiouechi commented 4 years ago

path

string path = "C:\test"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); }

var filePath = Path.Combine(path, "test.pdf");

//web projects var binFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/bin/testlala.pdf");

//console application var binFolder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

Regex

CNPJ: [RegularExpression(@"\d{2}.\d{3}.\d{3}/\d{4}-\d{2}|(\d{14})$", ErrorMessage = "Format not allowed.")] CPF: [RegularExpression(@"^((\d{3}).(\d{3}).(\d{3})-(\d{2}))*$", ErrorMessage = "Format not allowed.")]

String join concatenate

string.Join("", word1);

Regex

only alphanumeric "^[a-zA-Z0-9_]*$"

^ : start of string
[ : beginning of character group
a-z : any lowercase letter
A-Z : any uppercase letter
0-9 : any digit
_ : underscore
] : end of character group
* : zero or more of the given characters
$ : end of string

Date

Full date: DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")

Jagged Array vs Multidimensional

var jaggedArray = new int[2][]; (Array of arrays, in this case has 2 arrays. (read as 2 arrays and not 2 rows) var multiDimensional = new int[2,3]; (most used)

char

var c = (char)(97);

Bigger

var numberA = 5; var numberB = 10; Math.Max(numberA, numberB);

Linq

    var lettersGrouped = (from p in s.ToList()
                                  group p by p into g
                                  select new{
                                  Key = g.Key,
                                  Numbers = g.Count()
                                  }).ToList();

List of alphabetic:

            var alphabetic = new int[26];

            word = "caio";
            foreach (char item in word)
            {
                alphabetic[item - 'a'] = alphabetic[item - 'a'] + 1;
            }

How to debug on javascript on MVC

Using debugger;

Interviews:

var newString = "newString"; newString.Substring(0, newString.Length);

var newDictionary = new Dictionary<char,int>(); newDictionary.TryGetValue('', out int value);

//initiating with collector new Dictionary<string, string>() { { "laa", "1" } };

InterviewQuestions

Delegates:

https://www.youtube.com/watch?v=jQgwEsJISy0
1- Define a delegate
2- Define an event based on that delegate
3- Raise the event
Optional- Use EventHandler
4- delegate needs to be passed before the Trigger Method (my mistake)
5- the signature needs to be respected (the name doesn't need to be the same)

Clone

(int[])Array.Clone() -> Always cast before executing the .Clone();

Math.Abs

Absolute value

OrderBy

OrderBy -> quickSort O(nLogN) and worst case n"2 IComparable

Transaction

using (var transaction = DataContext.Database.BeginTransaction())

try{
 transaction.Commit();

} catch{
                        transaction.Rollback();
}

Reflection

System.Reflection; Type type = newPerson.GetType(); prop = type.GetProperty("Name"); var propertyGetByReflection = prop.GetValue(newPerson);

Threads

System.Threading; Thread newTread = new Thread(new ThreadStart(WorkThreadFunction));

FAQ

Difference between Thread and Task? (Thread worker, task is what needs to be done)

Custom fonts

@font-face { font-family: 'Open Sans'; src: url('fonts/OpenSans-VariableFont_wdth,wght.ttf'); }

Pointers & unsafe code

public unsafe void ChangeMemoryDebugger()
        {
            bool testVariable = false;

            var point = &testVariable;
            *point = true;

            bool newVariable = false;

            //ptr1
            bool* ptrVariable = &newVariable;

            var targetVariable = (Debugger.IsAttached);

            bool* ptrTarget = &(targetVariable);

            *ptrTarget = false;
        }