.NET 6 Yenilikleri – IsPow2 ve RoundUpToPowerOf2

.NET 6 ile hayatımıza BitOperations sınıfı içerisinde 2 yeni metod girdi. BitOperations sınıfı genellikle bit manipülasyonları için kullanılan statik bir utility sınıfı.

Bu operasyonlar;

✔️IsPow2: Verilen bir int yada long değerinin 2’nin bir tam sayı kuvveti olup olmadığını döndürür.

Console.WriteLine(BitOperations.IsPow2(64)); /// true 

https://source.dot.net/#System.Private.CoreLib/BitOperations.cs,49

✔️RoundUpToPowerOf2: Verilen bir int ya da long değerinden büyük ya da eşit, en küçük 2’nin tam sayı kuvvetini döndürür.

Console.WriteLine(BitOperations.RoundUpToPowerOf2(100)); /// 128 

https://source.dot.net/#System.Private.CoreLib/BitOperations.cs,97

Videoda bahsettiğim, Leetcode içerisindeki Amazon mülakat sorusu Power of Two: https://youtu.be/wi5jAQozZQA

Microsoft Mülakat Sorusu – Count Negative Numbers in a Sorted Matrix

LeetCode içerisinde bulunan “Count Negative Numbers in a Sorted Matrix” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir matrix içerisinde kaç adet negatif sayı olduğunu efektif bir şekilde bulmanız isteniyor.

► LeetCode 1351. Count Negative Numbers in a Sorted Matrix: https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/

► Problem açıklaması:

Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid.

Example 1:

Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]

Output: 8

Explanation: There are 8 negatives number in the matrix.

Example 2:

Input: grid = [[3,2],[1,0]]

Output: 0

Example 3:

Input: grid = [[1,-1],[-1,-1]]

Output: 3

Example 4:

Input: grid = [[-1]]

Output: 1

C# 10 Yenilikleri – Constant Interpolated Strings

C# 10 ile hayatımıza yeni giren özelliklerden biri constant interpolated strings yapısı.

C# 9 ve öncesi versiyonlarda const değerler içerisinde + operatörü ile string concatenation işlemi yapabiliyorduk. Yani aşağıdaki kod legal idi;

const string Name = "";
const string  Surname = Name + " " + "Surname";

fakat şunu yapamıyorduk;

const string Name = "";
const string  Surname = $"{Name} Surname";

Bunun nedeni de string interpolation arka tarafta string.Format çağırıyordu ve bir method çağırımı compile time’da constant olamayacağı için hata veriyordu doğal olarak. Yani string interpolation içerisinde kullandığımız değer constant olsa bile, string interpolation yapısının “kendisi” constant değildi.

Bunun legal olabilmesi için C# community’den çok fazla talep gelince C# 10 versiyonunda bu özelliği de eklemişler. Bunu artık hem yukarıdaki gibi const değerleri içerisinde, hem de attribute’ler içerisinde kullandığımız mesajlarda kullanabiliyoruz.

[Obsolete($"{Name} bunu kullanamaz!")]
void MyMethod()
{
}

C# 10 Yenilikleri – Implicit Usings

.NET 6 ile hayatımıza implicit namespace kavramı girdi. Bu sayede projelerde gelen “boilerplate” içerisindeki using karmaşasından kurtulmak hedeflendi. Namespace’leri artık “global using” şeklinde projenizde tek bir .cs dosyası içerisinde veya “\..\obj\debug\net6.0” klasöründeki GlobalsUsing.cs dosyası içerisinde saklayabiliyorsunuz.

Dikkat edilmeli ki bu GlobalsUsing.cs dosyası otomatik oluşturulan bir dosya.

Örneğin projenizdeki her bir cs dosyası içerisinde System.Data namespace’ini kullanmak zorunda olduğunuzu düşünün. Her bir .cs dosyasının tepesinde bunu yazmak yerine, .csproj içerisinde ImplicitUsings tag’ini enable ederek, tek bir cs dosyası içerisinde “global using” tanımlayarak ya da bahsettiğim GlobalsUsing.cs dosyası içerisinde tanımlayarak tüm projenizde o namespace’in aktif olmasını sağlayabiliyorsunuz.

Ayrıca .csproj dosyası içerisinde ItemGroup olarak ta bu namespace’leri Using Include veya Using Remove kullanarak ekleme veya çıkartabiliyorsunuz.

RC1 ile birlikte, .NET 6 projesi oluşturduğunuzda .csproj dosyası içerisinde default olarak ImplicitUsings enable olarak geliyor. Dilerseniz bunu .csproj dosyanız içerisinde DisableImplicitNamespaceImports tag’ini true olarak disable edebiliyorsunuz.

Scott Hanselman‘ın söylediğine göre C# 10 projeleri bu sayede daha hızlı bir şekilde ayağa kalkıyorlarmış.

Her bir proje SDK tipinin default namespace değerleri şu şekilde;

Console/Library

System

System.Collections.Generic

System.IO

System.Linq

System.Net.Http

System.Threading

System.Threading.Tasks

Web

System.Net.Http.Json

Microsoft.AspNetCore.Builder

Microsoft.AspNetCore.Hosting

Microsoft.AspNetCore.Http

Microsoft.AspNetCore.Routing

Microsoft.Extensions.Configuration

Microsoft.Extensions.DependencyInjection

Microsoft.Extensions.Hosting

Microsoft.Extensions.Logging

Worker

Microsoft.Extensions.Configuration

Microsoft.Extensions.DependencyInjection

Microsoft.Extensions.Hosting Microsoft.Extensions.Logging

Kaynaklar:

Microsoft Docs: https://docs.microsoft.com/en-us/dotnet/core/compatibility/sdk/6.0/implicit-namespaces

Scott Hanselman’ın bloğu: https://www.hanselman.com/blog/implicit-usings-in-net-6

Facebook Mülakat Sorusu – Binary Tree Right Side View

LeetCode içerisinde bulunan “Binary Tree Right Side View” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir binary tree‘nin en sağ tarafındaki node‘ları yukarıdan aşağıda doğru olacak şekilde geri döndürmeniz isteniyor.

► LeetCode 199. Binary Tree Right Side View: https://leetcode.com/problems/binary-tree-right-side-view/

► Problem açıklaması:

Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example 1:

Input: root = [1,2,3,null,5,null,4]

Output: [1,3,4]

Example 2:

Input: root = [1,null,3]

Output: [1,3]

Example 3: Input: root = []

Output: []

Constraints:

The number of nodes in the tree is in the range [0, 100].

-100 <= Node.val <= 100

Amazon Mülakat Sorusu – Set Matrix Zeroes

LeetCode içerisinde bulunan “Set Matrix Zeroes” sorusunun açıklaması ve çözümü. Bu soruda size verilen m * n matrisi içerisinde, eğer bir eleman 0 ise, o elemanın ait olduğu tüm satır ve sütunları da 0 yapmanız isteniyor.

► LeetCode 73. Set Matrix Zeroes: https://leetcode.com/problems/set-matrix-zeroes/

► Problem açıklaması:

Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0’s, and return the matrix.

You must do it in place.

Example 1:

Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]

Output: [[1,0,1],[0,0,0],[1,0,1]]

Example 2:

Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]

Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]

Constraints:

m == matrix.length

n == matrix[0].length

1 <= m, n <= 200

-2^31 <= matrix[i][j] <= 2^31 – 1

Linkedin Mülakat Sorusu – Maximum Depth of Binary Tree

LeetCode içerisinde bulunan “Maximum Depth of Binary Tree” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir binary tree içerisinde, root node‘undan en uzaktaki leaf node‘una olan uzaklığı bulmanız isteniyor.

► LeetCode 104. Maximum Depth of Binary Tree: https://leetcode.com/problems/maximum-depth-of-binary-tree/

► Problem açıklaması:

Given the root of a binary tree, return its maximum depth.

A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1:

Input: root = [3,9,20,null,null,15,7]

Output: 3

Example 2:

Input: root = [1,null,2]

Output: 2

Example 3:

Input: root = []

Output: 0

Example 4:

Input: root = [0]

Output: 1

Constraints:

The number of nodes in the tree is in the range [0, 10^4].

-100 <= Node.val <= 100