Skip to content

Commit c8c6253

Browse files
SimonCroppPureWeen
authored andcommitted
fix some incorrect casting using as (#30459)
if the type is known then a direct cast should be used instead of an as. since, in the case where the assumption is wrong, then it is better to get a cast exception instead of a null ref exception. basically if code uses `as` and does not check for null when that variable is used, then it is likely problematic code.
1 parent 086a7dd commit c8c6253

15 files changed

+26
-26
lines changed

src/Controls/samples/Controls.Sample/Controls/Rate/RateItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected override void OnApplyTemplate()
104104
{
105105
base.OnApplyTemplate();
106106

107-
_icon = (GetTemplateChild(ElementIcon) as View)!;
107+
_icon = (View)GetTemplateChild(ElementIcon);
108108
_icon.WidthRequest = Width;
109109
}
110110
}

src/Controls/samples/Controls.Sample/Pages/Controls/CarouselViewGalleries/IndicatorCodeGallery.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public IndicatorCodeGallery()
6262
generator.GenerateItems();
6363

6464
_carouselView.PropertyChanged += CarouselViewPropertyChanged;
65-
(_carouselView.ItemsSource as ObservableCollection<CollectionViewGalleryTestItem>)!.CollectionChanged += IndicatorCodeGalleryCollectionChanged;
65+
((ObservableCollection<CollectionViewGalleryTestItem>)_carouselView.ItemsSource).CollectionChanged += IndicatorCodeGalleryCollectionChanged;
6666

6767
var indicatorView = new IndicatorView
6868
{
@@ -205,7 +205,7 @@ public IndicatorCodeGallery()
205205
Padding = new Thickness(5),
206206
Command = new Command(() =>
207207
{
208-
var items = (_carouselView.ItemsSource as ObservableCollection<CollectionViewGalleryTestItem>)!;
208+
var items = (ObservableCollection<CollectionViewGalleryTestItem>)_carouselView.ItemsSource;
209209
items.Remove(items[0]);
210210
})
211211
};
@@ -238,7 +238,7 @@ public IndicatorCodeGallery()
238238
_carouselView.Position++;
239239
}, () =>
240240
{
241-
var items = (_carouselView.ItemsSource as ObservableCollection<CollectionViewGalleryTestItem>)!;
241+
var items = (ObservableCollection<CollectionViewGalleryTestItem>)_carouselView.ItemsSource;
242242
return _carouselView.Position < items.Count - 1;
243243
})
244244
};
@@ -252,7 +252,7 @@ public IndicatorCodeGallery()
252252
Padding = new Thickness(5),
253253
Command = new Command(() =>
254254
{
255-
var items = (_carouselView.ItemsSource as ObservableCollection<CollectionViewGalleryTestItem>)!;
255+
var items = (ObservableCollection<CollectionViewGalleryTestItem>)_carouselView.ItemsSource;
256256
var indexToRemove = items.Count - 1;
257257
items.Remove(items[indexToRemove]);
258258
})

src/Controls/samples/Controls.Sample/Pages/Controls/CollectionViewGalleries/ItemsSourceGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public ItemsSourceGenerator(ItemsView cv, int initialItems = 1000,
5757
button.Clicked += GenerateItems;
5858
WeakReferenceMessenger.Default.Register<ExampleTemplateCarousel, string>(this, "remove", (_, obj) =>
5959
{
60-
(cv.ItemsSource as ObservableCollection<CollectionViewGalleryTestItem>)!.Remove((obj.BindingContext as CollectionViewGalleryTestItem)!);
60+
((ObservableCollection<CollectionViewGalleryTestItem>)cv.ItemsSource).Remove((obj.BindingContext as CollectionViewGalleryTestItem)!);
6161
});
6262

6363
Content = layout;

src/Controls/samples/Controls.Sample/Pages/Controls/CollectionViewGalleries/ObservableCodeCollectionViewGallery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ObservableCodeCollectionViewGallery(ItemsLayoutOrientation orientation =
2424

2525
IItemsLayout itemsLayout = grid
2626
? new GridItemsLayout(3, orientation)
27-
: new LinearItemsLayout(orientation) as IItemsLayout;
27+
: new LinearItemsLayout(orientation);
2828

2929
var itemTemplate = ExampleTemplates.PhotoTemplate();
3030

src/Controls/samples/Controls.Sample/Pages/Controls/CollectionViewGalleries/ObservableCollectionResetGallery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public ObservableCollectionResetGallery()
1717
}
1818
};
1919

20-
var itemsLayout = new GridItemsLayout(3, ItemsLayoutOrientation.Vertical) as IItemsLayout;
20+
IItemsLayout itemsLayout = new GridItemsLayout(3, ItemsLayoutOrientation.Vertical);
2121

2222
var itemTemplate = ExampleTemplates.PhotoTemplate();
2323

src/Controls/samples/Controls.Sample/Pages/Controls/CollectionViewGalleries/ObservableMultiItemCollectionViewGallery.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public ObservableMultiItemCollectionViewGallery(ItemsLayoutOrientation orientati
2121
}
2222
};
2323

24-
var itemsLayout = grid
24+
IItemsLayout itemsLayout = grid
2525
? new GridItemsLayout(3, orientation)
26-
: new LinearItemsLayout(orientation) as IItemsLayout;
26+
: new LinearItemsLayout(orientation);
2727

2828
var itemTemplate = ExampleTemplates.PhotoTemplate();
2929

src/Controls/samples/Controls.Sample/Pages/Controls/CollectionViewGalleries/ScrollModeGalleries/ItemsUpdatingScrollModeGallery.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected override async void OnAppearing()
5050

5151
void OnItemsUpdatingScrollModeChanged(object sender, EventArgs e)
5252
{
53-
CollectionView.ItemsUpdatingScrollMode = (ItemsUpdatingScrollMode)(sender! as EnumPicker)!.SelectedItem;
53+
CollectionView.ItemsUpdatingScrollMode = (ItemsUpdatingScrollMode)((EnumPicker)sender!).SelectedItem;
5454
}
5555
}
5656
}

src/Controls/samples/Controls.Sample/Pages/Core/DragAndDropBetweenLayouts.xaml.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected override void OnAppearing()
3636

3737
private void OnDragStarting(object sender, DragStartingEventArgs e)
3838
{
39-
var boxView = (View)(sender as Element)!.Parent;
39+
var boxView = (View)((Element)sender)!.Parent;
4040
DragStartingTitle.IsVisible = true;
4141
DragStartingPositionLabel.Text = $"- Self X:{(int)e.GetPosition(boxView)!.Value.X}, Y:{(int)e.GetPosition(boxView)!.Value.Y}";
4242
DragStartingScreenPositionLabel.Text = $"- Screen X:{(int)e.GetPosition(null)!.Value.X}, Y:{(int)e.GetPosition(null)!.Value.Y}";
@@ -54,7 +54,7 @@ private void OnDragStarting(object sender, DragStartingEventArgs e)
5454

5555
private void OnDropCompleted(object sender, DropCompletedEventArgs e)
5656
{
57-
var sl = (sender as Element)!.Parent as StackLayout;
57+
var sl = ((Element)sender).Parent as StackLayout;
5858

5959
if (sl == SLAllColors)
6060
SLRainbow.Background = SolidColorBrush.White;
@@ -65,7 +65,7 @@ private void OnDropCompleted(object sender, DropCompletedEventArgs e)
6565

6666
private void OnDragOver(object sender, DragEventArgs e)
6767
{
68-
var sl = (StackLayout)(sender as Element)!.Parent;
68+
var sl = (StackLayout)((Element)sender).Parent;
6969

7070
if (!e.Data.Properties.ContainsKey("Source"))
7171
return;
@@ -86,7 +86,7 @@ private void OnDragOver(object sender, DragEventArgs e)
8686

8787
private void OnDragLeave(object sender, DragEventArgs e)
8888
{
89-
var sl = (StackLayout)(sender as Element)!.Parent;
89+
var sl = (StackLayout)((Element)sender).Parent;
9090

9191
if (!e.Data.Properties.ContainsKey("Source"))
9292
return;
@@ -107,7 +107,7 @@ private void OnDragLeave(object sender, DragEventArgs e)
107107

108108
private void OnDrop(object sender, DropEventArgs e)
109109
{
110-
var sl = (sender as Element)!.Parent as StackLayout;
110+
var sl = ((Element)sender).Parent as StackLayout;
111111

112112
if (!e.Data.Properties.ContainsKey("Source"))
113113
return;
@@ -122,7 +122,7 @@ private void OnDrop(object sender, DropEventArgs e)
122122
DropScreenPositionLabel.Text = $"- Screen: X:{(int)e.GetPosition(null)!.Value.X}, Y:{(int)e.GetPosition(null)!.Value.Y}";
123123
DropRelativePositionLabel.Text = $"- This label: X:{(int)e.GetPosition(DropRelativePositionLabel)!.Value.X}, Y:{(int)e.GetPosition(DropRelativePositionLabel)!.Value.Y}";
124124

125-
var color = (e.Data.Properties["Color"] as SolidColorBrush)!;
125+
var color = (SolidColorBrush)e.Data.Properties["Color"];
126126

127127
if (AllColors.Contains(color))
128128
{

src/Controls/samples/Controls.Sample/Pages/Core/NavigationGallery.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void SwapRoot(object sender, EventArgs e)
9595
}
9696
else
9797
{
98-
(Parent as IStackNavigationView)!.RequestNavigation(
98+
((IStackNavigationView)Parent).RequestNavigation(
9999
new NavigationRequest(_currentNavStack, true));
100100

101101
_currentNavStack = null;

src/Controls/samples/Controls.Sample/Pages/Others/LargeTitlesPageiOS.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public LargeTitlesPageiOS()
4545
{
4646
Text = "Tooggle UseLargeTitles on Navigation",
4747
Command = new Command( () =>{
48-
var navPage = (Parent as NavigationPage)!;
48+
var navPage = (NavigationPage)Parent;
4949
navPage.On<iOS>().SetPrefersLargeTitles(!navPage.On<iOS>().PrefersLargeTitles());
5050
} )
5151
},
@@ -54,7 +54,7 @@ public LargeTitlesPageiOS()
5454
{
5555
Text = "UseLargeTitles on Navigation with safe Area",
5656
Command = new Command( () =>{
57-
var navPage = (Parent as NavigationPage)!;
57+
var navPage = (NavigationPage)Parent;
5858
navPage.On<iOS>().SetPrefersLargeTitles(true);
5959
var page = new ContentPage { Title = "New Title", BackgroundColor = Colors.Red };
6060
page.On<iOS>().SetUseSafeArea(true);

0 commit comments

Comments
 (0)