Questions tagged [as-keyword]
13 questions
28
votes
10 answers
Why is 'is' implemented as 'as'?
Given that this is a very natural use case (if you don't know what as actually does),
if (x is Bar) {
Bar y = x as Bar;
something();
}
is effectively equivalent (that is, the compiler-generated CIL from the above code will be equivalent)…

Vinko Vrsalovic
- 330,807
- 53
- 334
- 373
25
votes
3 answers
Is there any keyword in Java which is similar to the 'AS' keyword of C#
As we know C# provides an AS keyword which automatically performs a check whether the Object is of a type and if it is, it then casts it to the needed type else gives a null.
public class User { }
Object obj = someObj;
User user = obj As…

Anubhav Ranjan
- 1,558
- 3
- 18
- 32
13
votes
3 answers
python's `with` statement target is unexpectedly None
seems like I do not understand something with---the python with statement.
Consider this class:
class test(object):
def __enter__(self): pass
def __exit__(self, *ignored): pass
now, when using it with with, like in
with test() as michael:
…

Prestel Nué
- 831
- 2
- 9
- 12
7
votes
9 answers
Are there compelling reasons AGAINST using the C# keyword "as"?
I find that using the following:
TreeViewItem i = sender as TreeViewItem;
if(i != null){ ... }
is easier to write and understand than:
if(sender.GetType() == typeof(TreeViewItem)){
TreeViewItem i = (TreeViewItem)sender;
...
}
Are there…

Mark Carpenter
- 17,445
- 22
- 96
- 149
6
votes
2 answers
How do I use groovy's AS keyword
This may be a duplicate but "as" is an INCREDABLY hard keyword to google, even S.O. ignores "as" as part of query.
So I'm wondering how to implement a class that supports "as" reflexively. For an example class:
class X {
private val
public…

Bill K
- 62,186
- 18
- 105
- 157
4
votes
4 answers
Why initialize an object using as keyword
I've just run across some code I don't understand. It is effectively
Class c = new BaseClass() as Class;
I don't understand the advantage of doing this, so I created my own console application to see what it does.
namespace Initialize
{
class…

Dave
- 8,163
- 11
- 67
- 103
2
votes
2 answers
Does the `as` keyword bring back all the members of the class?
I've been doing some testing and came across something strange.
Say I have this interface
interface IRobot
{
int Fuel { get; }
}
As you can see, it's read only. So now i'm going to make a class that implements it
class FighterBot…

CsharpFrustration
- 137
- 2
- 10
1
vote
1 answer
"As" keyword in list comprehensions
Is there something similar to a as keyword in list comprehensions?
Example: instead of
L = [foo(bar(baz(bla(x)))) for x in X if foo(bar(baz(bla(x)))) == 1]
it would be:
L = [foo(bar(baz(bla(x)))) as y for x in X if y == 1]

Basj
- 41,386
- 99
- 383
- 673
1
vote
2 answers
moq and the "as" keyword
I'm attempting to provide a mock that will help me test:
ClaimsPrincipal princple = Thread.CurrentPrincipal as ClaimsPrincipal;
However from what I can tell, the "as" keyword returns null even though I can see in the debugger…

Alex KeySmith
- 16,657
- 11
- 74
- 152
1
vote
3 answers
A generic function to accept both reference types and nullable types to accommodate the "as" keyword possible?
This is pure curiosity/challenge, no practical importance at all. So I'm not looking for alternate solutions that get the job done.
From this question Most efficient way to check for DBNull and then assign to a variable? I found this answer which…

nawfal
- 70,104
- 56
- 326
- 368
1
vote
5 answers
Object initializations are lost through use of "as" keyword
I am using a derived class and casting the base class to it using the as keyword. When I do this, the derived class constructor is being called, and it's objects initialized, but the derived instance does not end up with the initialized objects (has…

TahoeWolverine
- 1,749
- 2
- 23
- 31
0
votes
1 answer
why use of as nickname route using in laravel and when use it
advantage of as keyword using laravel route system
why, when and where its usage is better
how many ways to access route url define by the as keyword and without define as keyword in laravel

Ghulam Abbas
- 515
- 2
- 10
- 23
-3
votes
3 answers
How to use the "is" keyword with an object?
How can I use the is keyword with an object, rather than an object's class?
Here is some code:
private bool IsObjectCompatableWithObjects(object obj, IEnumerable

Simon
- 7,991
- 21
- 83
- 163