اشتراک‌ها
دات نت پایه، سی شارپ 8.0 و Nullable Reference Types

Here are some of the reasons why nullable reference types are less than ideal:

  • Invoking a member on a null value will issue a System.NullReferenceException exception, and every invocation that results in a System.NullReferenceException in production code is a bug. Unfortunately, however, with nullable reference types we “fall in” to doing the wrong thing rather than the right thing. The “fall in” action is to invoke a reference type without checking for null.
  • There’s an inconsistency between reference types and value types (following the introduction of Nullable<T>) in that value types are nullable when decorated with  “?” (for example, int? number); otherwise, they default to non-nullable. In contrast, reference types are nullable by default. This is “normal” to those of us who have been programming in C# for a long time, but if we could do it all over, we’d want the default for reference types to be non-nullable and the addition of a “?” to be an explicit way to allow nulls.
  • It’s not possible to run static flow analysis to check all paths regarding whether a value will be null before dereferencing it, or not. Consider, for example, if there were unmanaged code invocations, multi-threading, or null assignment/­replacement based on runtime conditions. (Not to mention whether analysis would include checking of all library APIs that are invoked.)
  • There’s no reasonable syntax to indicate that a reference type value of null is invalid for a particular declaration.
  • There’s no way to decorate parameters to not allow null. 
دات نت پایه، سی شارپ 8.0 و Nullable Reference Types
اشتراک‌ها
مجموعه نکاتی از VS Code

In this article, I'm going to talk about some of my favorite tips and tricks about my favorite IDE, VS Code. Although I'm writing this on a Mac, many of these concepts will port to Windows, so you may have to replace COMMAND key, WIN key, etc.

مجموعه نکاتی از VS Code
مطالب
نکاتی در باب T-SQL
در این مطلب قصد دارم به نکاتی ساده  در T-SQL بپردازم، امیدوارم مفید واقع شود.

1- تفاوت (*)Count و (Count(column
در (*)Count ، همه Row‌ها و مقادیرشان مورد جستجو قرار می‌گیرد، اما در  (Count(column فقط مقادیر غیر Null ستون مورد نظر،مورد جستجو قرار می‌گیرد.
مثال:
Create Table Test(ID int,Firstname varchar(20));

Insert Into Test (ID,Firstname) Values(1,'K');
Insert Into Test (ID,Firstname) Values(2,'B');
Insert Into Test (ID) Values(3);
با اجرای Query زیر خواهیم داشت:
Select COUNT(*) From Test
خروجی آن 3 می‌باشد.
اما با اجرای Query زیر خواهیم داشت:
Select COUNT(Firstname) From Test
خروجی آن 2 میباشد، چون با توجه به سه رکوردی که در جدول Test درج شده بود، رکورد سوم برای فیلد Firstname با مقدار Null پر شده است.
  • هرگاه در اجرای Count ،هدفتان بدست آوردن تعداد ستون خاصی است، از (Count(column استفاده نمایید.
2- بوسیله Script‌های زیر می‌توانیم عدد صفر تولید نماییم.
select count(cast(null as int))
select count(*) where 'a'='b'
select €
select ¥
select £
Select $ 
select count(*)-count(*)
select Ascii('A')-Ascii('A')
select LEN('')
3- با یک Select ساده می‌توان نام ستون و مقدار را کنار هم نوشت و مشاهده نمود.

در مثال بالا مقادیر ستونها، عددی در نظر گرفته شده است، چنانچه تمایل به نمایش حروف داشته باشید، کافیست کاراکترهای حرفی را بین سنگل کوتیشن قرار دهید، همانند شکل زیر:

4- دو روش پیشنهادی برای تشخیص عدد بزرگتر از بین دو عدد
  • روش اول 
      ابتدا به فرمول زیر توجه نمایید:
(a+b)+ABS(a-b)
      حاصل جواب فرمول،برابر است با دو برابر عدد بزرگتر،بنابراین اگر حاصل فوق را ضربدر عدد 0/5 نماییم،عدد بزرگتر بدست خواهد آمد. در نتیجه خواهیم داشت:
0.5(a+b)+ABS(a-b)
 با اجرای Script زیر خروجی عدد 90.34 می‌باشد:
DECLARE @Value1 DECIMAL(5,2) = 80.22
DECLARE @Value2 DECIMAL(5,2) =90.34
SELECT (0.5 * ((@Value1 + @Value2) + ABS(@Value1 - @Value2))) AS MaxColumn  
اشکال در این روش این است که، اگر مقدار یکی از اعداد Null باشد،ماکزیمم بین دو عدد Null نمایش داده خواهد شد.
  • روش دوم
DECLARE @Value1 DECIMAL(5,2) = 9.22
DECLARE @Value2 DECIMAL(5,2) = 8.34
SELECT CASE WHEN @Value1 > @Value2 THEN @Value1 ELSE @Value2 END AS MaxColumn

در این روش اگر مقدار یکی از اعداد Null باشد،ماکزیمم بین دو عدد، عدد غیر Null می‌باشد.

5- مشاهده مشخصات کلیه دیتابیس‌های موجود در SQL Server با استفاده از Sys.Databases .
Select * From sys.databases
خروجی بصورت زیر خواهد بود:



6- بوسیله دستور OUTPUT می‌توان خروجی Query‌های Delete،Update و Insert را مشاهده نمود:

مثال اول برای Query Delete :

در شکل، تک رکورد حذف شده را مشاهده می‌نمایید.

مثال دوم برای Query Update بصورت زیر میباشد:

در شکل، مقدار A، مقدار جدیدی است که بروز رسانی شده است و مقدار B مقداری است که مربوط به قبل از بروز رسانی می‌باشد.

مثال سوم برای Query Insert بصورت زیر میباشد:

موفق باشید.