|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionIn part II of this multipart tutorial, I describe the keywords that can loosely be categorized as dealing with object type, operators and type conversion. Why Is This Useful To You?My interest in this series of articles is to provide a concise repository for the entire collection of keywords in C#. I plan to use this myself as a reference, and hopefully you will find it useful too. In Part I[^], I provided an overview of all the keywords and described in detail, with some code examples, the keywords that can be considered modifiers to entities such as methods, parameters, etc.
In the "old" days, languages used to be taught differently: start with the keywords, then move into the syntax, and then start writing some basic (as in simple!) programs. This approach, while it doesn't satisfy the "immediate gratification" crowd is still a valid approach when trying to gain a well rounded understanding of a programming language. Why Is This Better Than Microsoft's Help On Keywords?The primary answer to that question is not so much that what I have to say is better, but that in some cases, I provide supplementary understanding that the help topics don't provide. Also, personally, I get tired of bouncing around from one link to another. Sometimes it's nice to see everything laid out in a contiguous way. Also, I find that the more ways a concept can be described, the better I'll understand it, and there's nothing better to test one's comprehension than by trying to explain it to someone else! What Have I Learned?In writing Part II, I've learned that unary operators appear (to this author) pretty much useless because operators are required to be static members. Also, binary operators are limiting (this is true for unary operators as well) because they cannot return references to objects. So much for implementing streams in C# or other features found in STL. It seems that without the ability to operate on information in the enclosing class, and without the ability to return references, C# will never support the nice capabilities of templates and STL that we've grown to love using C++ (of course, I could be wrong!). I also learned a lot about the inner "logic" of C#, especially regarding the rules for implicit and explicit casts. The KeywordsFollowing is a table of (almost all) the keywords in C#.
Keywords dealing with object type and type conversions:These are the keywords that will be discussed in this article:
The "as" KeywordThe object n=123;
object obj="abc";
string s1=n as string;
string s2=obj as string;
Console.WriteLine("s1="+s1);
Console.WriteLine("s2="+s2);
results in: s1=
s2=abc
Note that in some cases, the compiler will generate an error because it is attempting to do the cast at compile time. int i=123;
string s1=i as string;
results in a compiler error: "Cannot convert type 'int' to 'string' via a built-in conversion." To illustrate the difference between a cast and using the string s3=(string)n;
generates an exception, whereas, when using the Note that the The "is" KeywordThe object n1=123;
bool b1=n1 is int;
bool b2=n1 is string;
Console.WriteLine("n1 is int ? "+b1.ToString());
Console.WriteLine("n1 is string ? "+b2.ToString());
Results in: n1 is int ? true
n1 is string ? false
With the The "operator" KeywordThis keyword declares an operator on a class or structure. An operator does two things. The first is it performs an operation on one or two parameters, including a conversion from the parameter type to the resulting type. The second thing an operator does is to perform a conversion from one type to another. This capability is used in conjunction with the Operators are always public static ...
Unary OperatorsA unary operator takes one operand and manipulates it. The operand must be of the same type as the containing For example: public class PointEx
{
protected Point p;
public PointEx() {p=new Point(0, 0);}
public PointEx(int x, int y) {p=new Point(x, y);}
public PointEx(Point p) {this.p=p;}
public static int operator +(PointEx p)
{
return p.p.X+p.p.Y;
}
}
can be used syntactically as: PointEx p=new PointEx(1, 2);
int n=+p;
which results in n=3. This seems fairly pointless. However, overloading the "negative", "not", "exclusive-or", "increment" and "decrement" unary operators may be useful in certain instances. Binary OperatorsBinary operators perform an operation on two operands. At least one of the operands must be of the the enclosing public class PointEx
{
protected Point p;
public PointEx() {p=new Point(0, 0);}
public PointEx(int x, int y) {p=new Point(x, y);}
public PointEx(Point p) {this.p=p;}
public static PointEx operator +(PointEx p, int scalar)
{
return new PointEx(p.p.X+scalar, p.p.Y+scalar);
}
}
PointEx p=new PointEx(1, 2);
p+=5;
The Cast OperatorThe cast operator is, in my opinion, another fairly useless item because, unlike C++, it cannot return a reference. This means that you cannot cast perform a cast on an "l-value". For example: public class PointEx
{
protected Point p;
public static implicit operator Point(PointEx p)
{
return p.p;
}
public static PointEx operator +(PointEx p, int xlat)
{
((Point)p).X+=xlat;
((Point)p).Y+=xlat;
return p;
}
}
In the However, with the above code, we could of course write: Point pp=p;
and the compiler will dutifully and automatically invoke the cast operator which returns the The "implicit" KeywordThe above example uses the The "explicit" KeywordThe public static explicit operator Point(PointEx p)
{
return p.p;
}
Now, the implicit cast we used before generates a compiler error: Point pp=p; // !!! compiler error !!!
Point pp2=(Point)p; // works
What are the rules in determining whether a cast should be defined as implicit or explicit?Microsoft has this to say on the subject: By eliminating unnecessary casts, implicit conversions can improve source code readability. However, because implicit conversions can occur without the programmer's specifying them, care must be taken to prevent unpleasant surprises. In general, implicit conversion operators should never throw exceptions and never lose information so that they can be used safely without the programmer's awareness. If a conversion operator cannot meet those criteria, it should be marked explicit.
The "sizeof" KeywordThe int i=sizeof(short)
results in Note: I am not sure why this keyword can only be used in "unsafe" mode. I assume that the reason this is an "unsafe" keyword is that the underlying "generic" Instruction Language (IL) requires compiler and platform specific information to resolve the size of an object--whether a simple type like an integer or, a complex type like an object with multiple derivations or interfaces. The "typeof" KeywordThis keyword returns the type of an object (not an instance). To return the type of an instance, use the double d=0;
Type t1=d.GetType();
Type t2=typeof(double);
These two statements are equivalent, except that ConclusionI hope this series of articles helps anyone interested in understanding the capabilities of C#!
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||