我想按值将引用类型传递给C#中的方法.有没有办法做到这一点.
在C++中,如果我想通过Value传递,我总是可以依赖复制构造函数来发挥作用.在C#中有什么办法除外:1.显式创建一个新对象2.实现IClonable然后调用Clone方法.
这是一个小例子:
让我们在C++中使用A类来实现复制构造函数.
一个方法func1(Class a),我可以通过说func1(objA)来调用它(自动创建一个副本)
C#中是否存在类似的东西.顺便说一下,我正在使用Visual Studio 2005.
如果我没有得到这个非常错误,这种行为对我来说很奇怪.而不是解释,我将在下面发布一个示例代码,请告诉我为什么我得到输出x而不是y.
private void button1_Click(object sender, EventArgs e)
{
List<int> l = new List<int>() { 1, 2, 3 };
Fuss(l);
MessageBox.Show(l.Count.ToString());
}
private void Fuss(List<int> l)
{
l.Add(4);
l.Add(5);
}
Run Code Online (Sandbox Code Playgroud)
输出应该,我假设是3.但我得到输出为5.我理解输出可以是5如果我这样做:
private void button1_Click(object sender, EventArgs e)
{
List<int> l = new List<int>() { 1, 2, 3 };
Fuss(ref l);
MessageBox.Show(l.Count.ToString());
}
private void Fuss(ref List<int> l)
{
l.Add(4);
l.Add(5);
}
Run Code Online (Sandbox Code Playgroud) 我不太确定c#中"通过值传递"和"通过引用传递"的概念.我认为价值传递意味着:
int i = 9;
Run Code Online (Sandbox Code Playgroud)
我们将int传递给一个方法,如:method(i)通过引用传递意味着准确地传递它的位置,如:
Class.method.variable
Run Code Online (Sandbox Code Playgroud)
它会给出价值.任何人都可以帮助我吗?
如果我们修改在方法内作为参数传递的数组的内容,则对参数的副本而不是原始参数进行修改,因此结果不可见.
当我们调用具有引用类型参数的方法时,会发生什么过程?
这是我想问的代码示例
using System;
namespace Value_Refrence_Type
{
class Program
{
public static void Main()
{
int[] callingarray = { 22, 200, 25485 };
abc(callingarray);
Console.WriteLine("This is callingarray");
foreach (int element in callingarray)
Console.WriteLine(element);
}
//method parameter
static void abc(int[] calledarray)
{
Console.WriteLine("Method Called--------");
foreach (int element in calledarray)
Console.WriteLine(element);
//Here on changing the value of elements of calledarray does't afftect the value of element of callingarray
//if both refrences to same memory location then the value needs to change, …Run Code Online (Sandbox Code Playgroud) 我想添加自定义策略以进行两步授权,因此在第一步之后,用户将可以访问第二步,然后再访问所有内容。当我有一项政策时,一切正常,但当我添加另一项政策时,我开始收到此错误。我如何添加策略:
AuthorizationOptions authOptions = new AuthorizationOptions();
authOptions.AddPolicy("FirstStepCompleted", policy => policy.RequireClaim("FirstStepCompleted"));
authOptions.AddPolicy("Authorized", policy => policy.RequireClaim("Authorized"));
services.AddAuthorization(o => o = authOptions);
Run Code Online (Sandbox Code Playgroud)
当我有一项政策时,我像这样添加它:
services.AddAuthorization
(
options => options.AddPolicy("FirstStepCompleted",
policy => policy.RequireClaim("FirstStepCompleted"))
);
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
UPD1。忘记了例外:
处理请求时发生未处理的异常。InvalidOperationException:未找到名为“FirstStepCompleted”的 AuthorizationPolicy。Microsoft.AspNetCore.Authorization.AuthorizationPolicy.CombineAsync(IAuthorizationPolicyProviderpolicyProvider,IEnumerableauthorizeData)
我试图刷新我对 C# 的理解。我之前使用过 Java,通过引用传递是我在 Java 中真正怀念的强大工具。但是,我需要澄清是否需要通过引用传递对象,以及是否有可能有帮助的场景?Stackoverflow 已经有多个关于引用的问题,它们确实很有启发性,但是,我想知道是否有通过引用传递对象的方案。让我附上代码以更好地说明它。
class Program
{
public static void Update(int x) { x++; }
public static void Update(ref int x) { x++; }
public static void Update(Employee x) { x.msalary += 2000; }
public static void Update(ref Employee x) { x.msalary += 2000; } //Do we even need this method? Is there a scenario where we might need this?
static void Main(string[] args)
{
int a = 0;
Update(a);
Console.WriteLine("T1 " + a.ToString()); //a is still …Run Code Online (Sandbox Code Playgroud) 我不明白为什么这个函数是这样写的:
System.Array.Resize<int>(ref int[], int)
Run Code Online (Sandbox Code Playgroud)
如果默认情况下通过引用传递数组,为什么不是这样写的:
System.Array.Resize<int>(int[], int)
Run Code Online (Sandbox Code Playgroud) 我有一组对象,并希望修改该集合中对象的属性.如果我有一个对象,我的ChangeStuff方法工作正常,从方法返回时修改对象.(Main的前4行)
然而,当迭代整个集合时,我想我错过了一些东西,因为我的更改值似乎停留在foreach循环的范围内.
我不应该通过ref(或使用out参数)传递对象,因为我没有返回新值.
对于大代码块很抱歉,但它简化了我可以做到并且仍然可以证明我的问题.
class foobar
{
public string string1;
public string string2;
}
static void Main(string[] args)
{
/***** THIS WORKS!! *****/
foobar singleFb = new foobar { string1 = "foo2", string2 = "bar2" };
ChangeStuff(singleFb);
Console.WriteLine(singleFb.string1 + ", " + singleFb.string2);
Console.ReadLine(); //pause to read output
/***** THIS DOESN'T WORK!! *****/
List<foobar> myList = new List<foobar> { new foobar {string1 = "foo1", string2 = "bar1"}, new foobar {string1 = "foo2", string2 = "bar2"}, new …Run Code Online (Sandbox Code Playgroud) 美好的一天!这个问题似乎很容易,但我想不出怎么做.我会将Type参数传递给function并检查变量是否具有该类型.
void foo(Type type = ) // how to pass here default value for example System.Object?
{
if (elem.GetType() is type)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑我有下一堂课
class Element {}
class ChildElementClass : Element {}
class SecondChildEleementClass : Element{}
Run Code Online (Sandbox Code Playgroud)
并且有Elements []数组,它存储所有三个类的元素
Element[] elements;
Run Code Online (Sandbox Code Playgroud)
所以,我会检索ChildElementClass的所有元素.我通过其他方式制作它,但它很有趣.