我知道在C#中使用Null合并运算符的标准方法是设置默认值.
string nobody = null;
string somebody = "Bob Saget";
string anybody = "";
anybody = nobody ?? "Mr. T"; // returns Mr. T
anybody = somebody ?? "Mr. T"; // returns "Bob Saget"
Run Code Online (Sandbox Code Playgroud)
但还有什么可以??用于?它不像三元运算符那样有用,除了比以下更简洁和更容易阅读:
nobody = null;
anybody = nobody == null ? "Bob Saget" : nobody; // returns Bob Saget
Run Code Online (Sandbox Code Playgroud)
所以考虑到甚至更少知道空合并运算符......
你有没有用过??别的东西?
是??必要的,还是应该只使用三元运算符(大多数人都熟悉)
c# null coding-style conditional-operator null-coalescing-operator
下面的代码是在C#中,我使用的是Visual Studio 2010.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace FrontEnd
{
class Flow
{
long i;
private int x,y;
public int X
{
get;set;
}
public int Y
{
get;set;
}
private void Flow()
{
X = x;
Y = y;
}
public void NaturalNumbers(int x, int y)
{
for (i = 0; i < 9999; i++)
{
Console.WriteLine(i);
}
MessageBox.Show("done");
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译上面的代码时,我收到此错误:
错误:'Flow':成员名称不能与其封闭类型相同
为什么?我该如何解决这个问题?
我需要将Bitmap转换PixelFormat.Format32bppRgb为PixelFormat.Format32bppArgb.
我希望使用Bitmap.Clone,但似乎没有用.
Bitmap orig = new Bitmap("orig.bmp");
Bitmap clone = orig.Clone(new Rectangle(0,0,orig.Width,orig.Height), PixelFormat.Format24bppArgb);
Run Code Online (Sandbox Code Playgroud)
如果我运行上面的代码然后检查clone.PixelFormat它被设置为PixelFormat.Format32bppRgb.怎么回事/如何转换格式?
比方说,我们有一个变量,我们想要命名 Fubar
让我们说这Fubar是一个String!
这意味着,我们将Fubar定义为:
public string Fubar;
Run Code Online (Sandbox Code Playgroud)
现在,假设我们想要Fubar一个getter和setter(换句话说,成为一个C#属性)!
private string Fubar;
public string Fubar_gs
{
get
{
//Some fancy logic
return Fubar;
}
set
{
//Some more fancy logic
Fubar = value;
}
}
Run Code Online (Sandbox Code Playgroud)
好极了!这一切都很好,花花公子,除了我想要将PROPERTY命名为Fubar,而不是原始变量怎么办?
很明显,我只想重命名这两个变量.但问题是,原始变量的最佳名称是什么?
这种情况是否有命名惯例?
在我的反射代码中,我遇到了我的通用代码部分的问题.特别是当我使用一个字符串.
var oVal = (object)"Test";
var oType = oVal.GetType();
var sz = Activator.CreateInstance(oType, oVal);
Run Code Online (Sandbox Code Playgroud)
例外
An unhandled exception of type 'System.MissingMethodException' occurred in mscorlib.dll
Additional information: Constructor on type 'System.String' not found.
Run Code Online (Sandbox Code Playgroud)
我试过这个用于测试目的,它也发生在这个单一的衬里
var sz = Activator.CreateInstance("".GetType(), "Test");
Run Code Online (Sandbox Code Playgroud)
我最初写的
var sz = Activator.CreateInstance("".GetType());
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误
Additional information: No parameterless constructor defined for this object.
Run Code Online (Sandbox Code Playgroud)
如何使用反射创建字符串?
我有一个有图像的表格.我正在使用滑块来更改图像的不透明度.所以在滑块的"ValueChanged"事件中,我调用以下方法来更改不透明度.
//Setting the opacity of the image
public static Image SetImgOpacity(Image imgPic, float imgOpac)
{
Bitmap bmpPic = new Bitmap(imgPic.Width, imgPic.Height);
Graphics gfxPic = Graphics.FromImage(bmpPic);
ColorMatrix cmxPic = new ColorMatrix();
cmxPic.Matrix33 = imgOpac;
ImageAttributes iaPic = new ImageAttributes();
iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
gfxPic.DrawImage(imgPic, new Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic);
gfxPic.Dispose();
return bmpPic;
}
Run Code Online (Sandbox Code Playgroud)
返回的图像设置为原始图像.
我的问题是图像的不透明度没有变化......如果有任何错误,请善意指出.. Thnx ......
我做了快速测试应用程序,将LINQ排序与我的自定义对象上的Array.Sort进行比较.Array.Sort似乎非常慢!
我创建了这样的自定义类:
class Person : IComparable<Person>
{
public int Age { get; set; }
public string Name { get; set; }
public int CompareTo(Person obj)
{
return this.Age.CompareTo(obj.Age);
}
public Person()
{ }
}
Run Code Online (Sandbox Code Playgroud)
然后我在main()中做了我的测试人员:
string name = "Mr. Tomek";
Random r = new Random();
int size = 10000000;
DateTime start, end;
Person[] people1 = new Person[size];
Person[] people2 = new Person[size];
for (int i = 0; i < size; i++)
{
people1[i] = new Person();
people1[i].Age = r.Next(0, 10000); …Run Code Online (Sandbox Code Playgroud) 自Symfony 1.x的管理生成器以来,我发现这种工具对原型应用程序非常有用,可以非常快速地向客户展示等等.
现在对于Symfony2,管理生成器似乎不是优先级(请参阅此处和此处)
Django的管理生成器似乎非常有趣......
您会推荐哪种Web应用程序管理生成器(任何语言/技术)(优点/缺点)?
内部保护:
该工会的保护和内部辅助功能(这比限制较少的保护或内部单独)
该CLR拥有的概念交集的保护和内部辅助功能,但C#不支持这一点.
所以我的问题是:
省略这个访问修饰符的含义是什么,有一个具体的原因吗?那么为什么C#不应该支持呢?
还在学习async-await.我碰到了类似以下的例子:
public async Task MethodAsync()
{
await Method01Async();
await Method02Async();
}
Run Code Online (Sandbox Code Playgroud)
最后等待的目的是什么?Method02Async是MethodAsync方法的最后一行.所以没有任何方法余数 - 下面没有任何行 - 在编译器生成的回调中没有任何东西要调用...我错过了什么吗?
c# ×9
.net ×4
image ×2
activator ×1
arrays ×1
async-await ×1
c#-5.0 ×1
clr ×1
coding-style ×1
frameworks ×1
linq ×1
null ×1
php ×1
properties ×1
python ×1
reflection ×1
ruby ×1
sorting ×1