我写了一段代码。我想确保我以正确的方式处理对象。
我有一个像这样的一次性类,用于从非托管资源读取一些数据。
class MyFileReader : IDisposable
{
private readonly FileStream _stream;
public MyFileReader(FileStream stream)
{
_stream = stream;
}
public void Dispose()
{
_stream.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
目前在我的程序中,我像这样处理对象。
using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (MyFileReader reader = new MyFileReader(stream))
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎没问题。后来我注意到类是通过引用传递的,所以也许如果我处理其中一个,就不需要处理另一个。
我的问题是我可以做这样的事情吗?
using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
MyFileReader reader = new MyFileReader(stream);
// Remove IDisposable from MyFileReader and stream will close after using.
}
Run Code Online (Sandbox Code Playgroud)
还是这个?
FileStream stream = …
Run Code Online (Sandbox Code Playgroud) 目前在c#7(版本15.3.4)中,以下代码对编译有效,但两个变量都是合法不可用的.
switch(fruit)
{
case Apple apple:
case Orange orange:
// impossible to use apple or orange
break;
case Banana banana:
break;
}
Run Code Online (Sandbox Code Playgroud)
如果您尝试使用它们,则会得到熟悉的错误,在访问之前可能无法初始化变量.
有时在模式匹配中,您不关心确切的类型,只要该类型属于您想要的类别.这里只以苹果和橘子为例.
List<Fruit> applesAndOranges = new List<Fruit>();
switch(fruit)
{
case Fruit X when X is Apple || X is Orange:
applesAndOranges.Add(X);
break;
case Banana banana:
break;
}
Run Code Online (Sandbox Code Playgroud)
有更好的方法吗?
我有一个限制在父级底部的视图。我也有顶部的文本字段。当键盘出现时,它会向上推底部视图,这将覆盖我的文本字段。
我在滚动视图中拥有所有内容,键盘应该覆盖底部视图,并且我应该能够滚动到底部以到达底部视图。
这是一个简单的例子。我手动增加了高度以更容易重现问题。我其实有更多的观点,这只是为了演示。
请注意,fillViewPort 也已启用。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edit_text_1"
android:layout_width="0dp"
android:layout_height="180dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/edit_text_2"
android:layout_width="0dp"
android:layout_height="180dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/edit_text_1" />
<View
android:id="@+id/bottom_view"
android:layout_width="0dp"
android:layout_height="160dp"
android:background="@color/grey_500"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud) android scrollview android-softkeyboard android-constraintlayout
以下代码因此错误而失败
E0413不存在从“ lambda [] float(int i)-> float”到“ float(*)(int i)”的合适转换函数
int test;
float (*f)(int i) = [&](int i) -> float {return test; };
Run Code Online (Sandbox Code Playgroud)
我该如何解决?我需要Capture子句。
我想知道在我们得到堆栈溢出异常之前我们可以在c#中执行多少调用
所以我决定写下面的代码
static void Method2(int Calls)
{
if(!Calls.Equals(0))
Method1(--Calls);//if more calls remain call method1 and reduce counter
}
static void Method1(int Calls)
{
if (!Calls.Equals(0))//if more calls remain call method2 and reduce counter
Method2(--Calls);
}
static void Main(string[] args)
{
var Calls= 42994;//number of calls(stack overflow appears for large number)
Method1(Calls);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是编译器如何决定抛出堆栈溢出异常是关于内存限制的?一旦我把42995我得到stackoverflow但这个数字不是恒定所以这是如何工作的?
如何看待3D
Model 的前端WPF
?
我很困惑为LookDirection设置XYZ.
我不知道当我设置xyz时会发生什么,我不知道相机在哪里看.我不知道同样的事情UpDirection
.
以下是我的相机属性:
camera.Position = new Point3D(100, 100, 150);
camera.LookDirection = new Vector3D(-100, -100, -100);
camera.UpDirection = new Vector3D(0, 0, 1);
Run Code Online (Sandbox Code Playgroud)
问题是相机从后面看.模型显示为下面的立方体.
如何让它看起来正面?
另请说明如何设置xyz.我知道什么属性做但我无法想象他们.
我认为X是从右到左.Y是深度.Z是最多的.
我正在使用MVVM模式,并希望使用可观察的集合更新ListView.我经历了几个SO问题,但看不出我做错了什么.任何帮助深表感谢.谢谢.
View.xaml
命名空间: xmlns:local="clr-namespace:MusicPlayer.ViewModel"
DataContext的
<UserControl.DataContext>
<local:AllTracksViewModel/>
</UserControl.DataContext>
Run Code Online (Sandbox Code Playgroud)
列表显示
<ListView x:Name="TrackListView"
ItemsSource="{Binding Path=TrackListObservable}">
...
<ListView.View>
<GridView>
<GridViewColumn Header="Title" Width="250" DisplayMemberBinding="{Binding Title}" />
<GridViewColumn Header="Album" Width="200" DisplayMemberBinding="{Binding Album}" />
<GridViewColumn Header="Artist" Width="150" DisplayMemberBinding="{Binding Artist}" />
<GridViewColumn Header="Duration" Width="100" DisplayMemberBinding="{Binding FormattedDuration}" />
</GridView>
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)
ViewModel.cs
public class AllTracksViewModel
{
public ObservableCollection<Track> TrackListObservable { get; private set; }
public AllTracksViewModel()
{
TrackListObservable = new ObservableCollection<Track>();
}
}
Run Code Online (Sandbox Code Playgroud)
我确认项目肯定会被添加到observable中.再一次感谢你的帮助.
我想我前段时间在GitHub中听过"ref like struct"这个词.
现在我掌握了最新的C#版本(7.3),我终于可以自己测试了.所以这似乎是一个有效的代码:
public ref struct MyStruct
{
int x;
}
Run Code Online (Sandbox Code Playgroud)
我知道什么是ref本地和ref返回,因为有关于它的文档.但是我找不到关于ref struct的文档.
引用结构不能用于自动属性或字段.它们也不能被投射到对象上.这些都是实证研究结果.
有了新的c#最近给我的"Span"背景,我猜测ref struct是一个只有堆栈的结构.这是一个永远不会堆的结构.但我不是100%肯定.
我很确定应该有关于此的文档,但我没有找到它.
我正在看一个关于p的例子.斯蒂芬克莱里的40本书就是这本书
// Note: this is not the most efficient implementation.
// This is just an example of using a lock to protect shared state.
static int ParallelSum(IEnumerable<int> values)
{
object mutex = new object();
int result = 0;
Parallel.ForEach(source: values,
localInit: () => 0,
body: (item, state, localValue) => localValue + item,
localFinally: localValue =>
{
lock (mutex)
result += localValue;
});
return result;
}
Run Code Online (Sandbox Code Playgroud)
我有点困惑为什么lock
需要它.因为如果我们所做的只是总结int
s 的集合{1, 5, 6}
,那么我们就不需要关心result
以任何顺序递增的共享和.
(1 + 5) …
Run Code Online (Sandbox Code Playgroud) 我有这个iterator- return类型,IEnumerable<Task<Event>>
以便以后可以等待每个项目:
private IEnumerable<Task<Event>> GetEventsAsync(long length)
{
var pos = _reader.BaseStream.Position;
while (_reader.BaseStream.Position - pos < length)
{
yield return ReadEvent(); // this method is async
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我想将其传递给构造函数。
private async Task<EventManager> ReadEventManagerAsync()
{
// some other stuff
var length = Reverse(await _reader.ReadInt32()); // bytes to read
var events = GetEventsAsync(length); // cant await iterator. so use linq
return new EventManager(events.Select(async e => await e).Select(x => x.Result));
}
Run Code Online (Sandbox Code Playgroud)
构造函数采用此参数。
internal EventManager([NotNull, NoEnumeration]IEnumerable<Event> events) {...}
Run Code Online (Sandbox Code Playgroud)
这段代码会异步运行吗?
因为即使方法被标记为异步,我也无法在lambda中等待。我试图做一些修改。
我了解为什么Select(async …
c# ×8
.net ×2
c#-7.0 ×2
wpf ×2
3d ×1
android ×1
async-await ×1
asynchronous ×1
c++ ×1
c++11 ×1
callstack ×1
camera ×1
fall-through ×1
function ×1
idisposable ×1
lambda ×1
limits ×1
linq ×1
listview ×1
mvvm ×1
ref ×1
scrollview ×1
struct ×1
using ×1