Var,Dynamic,Check,Unchecked,As,IS keywords

CSharp · Console

/Program.cs

using System;
using System.Threading;
					
public class Program
{
	static readonly string readonlystring;   //you can levae it blank and can assign value at runtime,assign object to it 
	const string constring ="abc"; // you have to assign some value to const while declaring,you can assign ref type to const
	
	static Program()
	{
		readonlystring ="aaaa"; 
    }
	public static void Main()
	{
		
		//1. Var vs Dynamic
		var a = "aaaaa";
		
		//a = 1;   // Cannot implicitly convert type 'int' to 'string'
		
		dynamic ab = "aaaaa";
		ab =1;
		Console.WriteLine(a.GetType());
		Console.WriteLine(ab.GetType());
		
		
		
		//2. Const and readyonly
		Console.WriteLine(constring);
		Console.WriteLine(readonlystring);
		
		//3. checked keyword
		 int  checkedk = int.MaxValue;
		
		Console.WriteLine(checkedk);
		
				
		Console.WriteLine((checkedk+ 20) + " is a wrong output to handle such situation use checked keyword" );
		
		//uncomment below code to throw error
		
		//checked block
		//Console.WriteLine(checked(checkedk+ 20));
		
		//checked expression
			//checked
			//{
			//	int i = 10;
			//	 int i3 = 2147483647 + i;
              //  Console.WriteLine(i3);
			//}
		
		
		
		//4 . as and is keyword
		
		person p = new person();
	    Men men = new Men();
		
		Men m2 =  p as Men;
		Console.WriteLine(p is Men);
		Console.WriteLine(men is person);
		if(m2 !=null)
		{
			Console.WriteLine("m is person type");
		}
		else
		{
		   Console.WriteLine("m is not person type");
		}
		
		
		
		
	}
	
	
	    public class person
		{
			public string Fname{get;set;}
			public string Lname{get;set;}
		}
		public class Men : person
		{
			public string Fisrtname{get;set;}
			public string Lastname{get;set;}
		}
	
}
	   
;