IS Keyword
IS does not compare objects, it checks the object for its type.
For example:
"abc" string gives TRUE,
"abc" is int gives False,
"abc" is "abc" gives compile time error because it cannot be used to compare two types.
So, a comparison is not done by IS, only checking is done.
- namespace IsAndAsOperators {
- class Person {
- public int Id {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public int Age {
- get;
- set;
- }
- }
- class Alien {
- public int ID {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public int Age {
- get;
- set;
- }
- }
- class Program {
- static void Main(string[] args) {
- Person pobj = new Person();
- pobj.Id = 1;
- pobj.Name = "GOGO";
- pobj.Age = 15;
- Alien aobj = new Alien();
- aobj.ID = 20;
- aobj.Name = "POGO";
- aobj.Age = 25;
- // Is operator
- bool issame = (aobj is Person);
- System.Console.WriteLine("Alien is a Person ?: {0}", issame.ToString()); //FALSE
- issame = (pobj is Person);
- System.Console.WriteLine("Person is a Person ?: {0}", issame.ToString()); //TRUE
- System.Console.ReadLine();
- }
- }
- }
Now what happens if Alien inherit from Person (alien that evolved as earth Person over time) .
- namespace IsAndAsOperators {
- class Person {
- public int Id {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public int Age {
- get;
- set;
- }
- }
- class Alien: Person {
- public int ID {
- get;
- set;
- }
- public string NAME {
- get;
- set;
- }
- public int AGE {
- get;
- set;
- }
- }
- class Program {
- static void Main(string[] args) {
- Person pobj = new Person();
- pobj.Id = 1;
- pobj.Name = "GOGO";
- pobj.Age = 15;
- Alien aobj = new Alien();
- aobj.ID = 20;
- aobj.Name = "POGO";
- aobj.Age = 25;
- // Is operator
- bool issame = (aobj is Person);
- System.Console.WriteLine("Alien is a Person ?: {0}", issame.ToString()); //TRUE
- issame = (pobj is Person);
- System.Console.WriteLine("Person is a Person ?: {0}", issame.ToString()); //TRUE
- System.Console.ReadLine();
- }
- }
- }
AS operator
The AS operator gives a reference of various types. Say object r1 = "abc", then string r2 = r1 as string means r2 is the same as r1 but as a string, whereas Uri r3 = r1 as Uri means r3 is null because r1 wasn't a Uri so this couldn't work.
So the AS operator gives a reference if it can be done, else it gives NULL.
CODE
- namespace IsAndAsOperators {
- class Person {
- public int Id {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public int Age {
- get;
- set;
- }
- }
- class Alien {
- public int ID {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public int Age {
- get;
- set;
- }
- }
- class Program {
- static void Main(string[] args) {
- Person pobj = new Person();
- pobj.Id = 1;
- pobj.Name = "GOGO";
- pobj.Age = 15;
- Alien aobj = new Alien();
- aobj.ID = 20;
- aobj.Name = "POGO";
- aobj.Age = 25;
- // AS operator
- System.Console.WriteLine("Alien is a Person ?: {0}", useAS(aobj)); //False
- System.Console.WriteLine("Person is a Person ?: {0}", useAS(pobj)); //TRUE
- System.Console.ReadLine();
- }
- private static string useAS(dynamic aobj) {
- // If obj is Type Person it asign value to obj else it asign null
- Person obj = aobj as Person;
- if (obj != null) return "This is a Person and his name is " + obj.Name;
- return "Not a Person";
- }
- }
- }
AS with Inheritance
Now what happens if Alien inherits from Person (alien that evolved as earth Person over time).
- namespace IsAndAsOperators
- {
- class Person {
- public int Id
- {
- get;
- set;
- }
- public string Name
- {
- get;
- set;
- }
- public int Age
- {
- get;
- set;
- }
- }
- class Alien: Person {}
- class Program {
- static void Main(string[] args)
- {
- Person pobj = new Person();
- pobj.Id = 1;
- pobj.Name = "GOGO";
- pobj.Age = 15;
- Alien aobj = new Alien();
- aobj.Id = 20;
- aobj.Name = "POGO";
- aobj.Age = 25;
- // AS operator
- System.Console.WriteLine("Alien is a Person ?: {0}", useAS(aobj)); //TRUE
- System.Console.WriteLine("Person is a Person ?: {0}", useAS(pobj)); //TRUE
- System.Console.ReadLine();
- }
- private static string useAS(dynamic aobj)
- {
- // If obj is Type Person it asign value to obj else it asign null
- Person obj = aobj as Person;
- if (obj != null) return "This is a Person and his name is :" + obj.Name;
- return "Not a Person";
- }
- }
- }
CAST Operator
Let's explain this by the code first approach. We will see various ways to cast objects.
Let us say we have:
- void TestProgram(object o, EventArgs e)
- {
- //o is a string
- string s = (string) o; // 1 (Casting)
- //-OR-
- string s = o as string; // 2 (Use of AS)
- // -OR-
- string s = o.ToString(); // 3 (.tostring method)
- }
What is the difference between all these?
Using thoughts of Sander (since he explained this in the best possible way):
- Throws InvalidCastException if o is not a string, otherwise assigns o to s, even if o is null. (Use this for most conversions.)
- Assigns null to s if o is not a string or if o is null. For this reason, you cannot use it with value types (the operator could never return null in that case), otherwise assigns o to s. (Try to never use 2 since if something is not of the right type, I usually expect an exception to occur. I have only seen a need for this return-null type of functionality with badly designed libraries which use error codes, for example, return null = error instead of using exceptions).
- Causes a NullReferenceException if o is null. Assigns whatever o.ToString() returns to s, no matter what type o is. (Use it for when you need the string representation of a non-string object.)
BASIC- (int) value and "value as int" is that the former throws an exception if the conversion fails, whereas the latter returns null.
More details about CAST
We can cast only if we know what type it will be. What it means is explained by Boxing and Unboxing.
Here's the code:
- static string[] args)
- {
- short a = 10;
- object o = (object) a;
- int b = (int) o; //Illegal
- Console.WriteLine(b.ToString());
- Console.ReadLine();
- }
Here it is not possible since the compiler does not know how to unbox it. A short can be unboxed as a short only.
However:
- int jjj = (int) (short) ooo; // Perfectly legal
When a value is boxed as an Object then when unboxing the compiler does not know to which type it needs to be unboxed to. So if it's a short it needs to generate code to convert it into a short. If it's an int then it needs to generate code for an int and then there are many other complex types. Basically there will be a huge amount of code to generate and it would be very slow.
Santhakumar MunuswamyPosted Aug 16, 2015, 2:34 AM
Welcome
Santhakumar MunuswamyPosted Aug 16, 2015, 2:34 AM
Good Start
Yashwanth MuthineniPosted Aug 14, 2015, 12:54 AM
Gud one ......
Pankaj Kumar ChoudharyPosted Aug 13, 2015, 8:12 PM
Nice Explain Vishal........
Shridhar SharmaPosted Aug 13, 2015, 5:22 PM
Nice . welcome to C# Corner.
Gopi ChandPosted Aug 13, 2015, 1:00 PM
Nice to start with :) welcome
RakeshPosted Aug 13, 2015, 10:12 AM
Good start
Nilesh JadavPosted Aug 13, 2015, 9:44 AM
Nie share sir
Afzaal Ahmad ZeeshanPosted Aug 13, 2015, 8:52 AM
Good but Vishal there is no need to use dynamic when you can use object. dynamic is just used to skip compile-time checking. Object is the good way to do so, it has same function and a good reason to be used. Are you aware of boxing and unboxing?
Vishal VermaPosted Aug 13, 2015, 8:41 AM
Thanks..will continue to share more knowledge
Rajeesh MenothPosted Aug 13, 2015, 8:40 AM
Welcome to c# community....nice one
Vaikesh K PPosted Aug 13, 2015, 8:20 AM
Good
Karthikeyan KPosted Aug 13, 2015, 8:17 AM
Thanks for sharing...