Two methods, do the same thing, in different ways [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Hi I'm new to programming and I got this code:
public void Print(out string dataToPrint)
{
//code....
dataToPrint = "some text here"
}
And:
public string dataToPrint()
{
//code
return "some text here"
}
Which one will be used today, and which example will a professional programmer will use and what is the fastest in terms of performance?

In terms of performance there is nearly no difference, you can try it by running the each method inside a loop and test the time each can take, or use benchmark.net .
I tried it with Benchmark.net using following code:
public class Class1
{
public void Print(out string dataToPrint)
{
dataToPrint = "some text here";
}
public string Print()
{
return "some text here";
}
[Benchmark]
public void one()
{
string data;
Print(out data);
}
[Benchmark]
public void two()
{
Print();
}
}
class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Class1>();
}
}
And the result was :
As you can see the difference is too small, so you shouldn't consider it in your case, but I prefer the second form for readability, however, for other cases try using same procedure and find out.

Related

How do I write to Json file in C#? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am making a game in Unity. I was trying to make an Options menu, but my apply button does not work. I wanted to make it so the options gets written to a Json file.
I have tried that, but I just get this error:
Assets/Scripts/SettingsManager.cs(48,39): error CS0117: 'JsonUtility' does not contain a definition for 'toJson'
Here is the C# file:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class SettingsManager : MonoBehaviour
{
public Toggle fullscreenToggle;
public Dropdown resolutionDropdown;
public Resolution[] resolutions;
public GameSettings gameSettings;
public Button applyButton;
void OnEnable()
{
gameSettings = new GameSettings();
fullscreenToggle.onValueChanged.AddListener(delegate { OnFullscreenToggle(); });
resolutionDropdown.onValueChanged.AddListener(delegate { OnResolutionChange(); });
applyButton.onClick.AddListener(delegate { OnApplyButtonClick(); });
resolutions = Screen.resolutions;
foreach (Resolution resolution in resolutions)
{
resolutionDropdown.options.Add(new Dropdown.OptionData(resolution.ToString()));
}
}
public void OnFullscreenToggle()
{
gameSettings.fullScreen = Screen.fullScreen = fullscreenToggle.isOn;
}
public void OnResolutionChange()
{
Screen.SetResolution(resolutions[resolutionDropdown.value].width, resolutions[resolutionDropdown.value].height, Screen.fullScreen);
}
public void OnApplyButtonClick()
{
SaveSettings();
}
public void SaveSettings()
{
string jsonData = JsonUtility.toJson(gameSettings, true);
File.WriteAllText(Application.persistentDataPath + "/gamesettings.json",jsonData);
}
public void LoadSettings()
{
}
}
You have a typo. Write ToJson with uppercase T
JsonUtility.ToJson

Get some unique name per each run of the console application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Not sure how to ask this. I am writing a CONSOLE app in C#. With each run I plan to create an output txt file. However I need some expression that will give an unique name with each run ( and also this name must remain the same through out the run ) I can use YYYYMMDDSS but this can change with each call. So you see what I mean. It should be something like Session ID ( I don't know what method/function will give me this ). Help please ? ( I am new to C# )
If you want the identifier to change on each execution, but be consistent for the life of the program, you probably want to generate it once and store the result.
One way is to store it in a readonly static property:
public static class RuntimeIdentifier
{
public static string Value { get; } = DateTime.Now.ToString("yyyyMMddhhmmss");
// Notice this is NOT the same as:
//
// public static string Value { get { return DateTime.Now.ToString("yyyyMMddhhmmss"); } }
//
// which will return a NEW value each time you access it
}
Another way would be to use Lazy<T>, which computes a value the first time you access it, and returns the same value each time after that.
public static class RuntimeIdentifier
{
public static Lazy<string> _identifier = new Lazy<string>(() => DateTime.Now.ToString("yyyyMMddhhmmss"));
public static string GetValue()
{
return _identifier.Value;
}
}
If it's important that the value be generated immediately when the app is started, you can generate and save it explicitly:
class Program
{
public static void Main()
{
RuntimeIdentifier.Value = DateTime.Now.ToString("yyyyMMddhhmmss");
// ...
}
}
public static class RuntimeIdentifier
{
public static string Value { get; set; }
}
Your scenario is not totally clear but if you are just trying to create a txt file with unique name each time you run your application than you should use Path.GetRandomFileName() for that matter. Or just look how it's implemented and adapt it to your needs.

Class with same code and send data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have this C# class to send the data from DriveInfo Class to my window form:
using System;
public class FileSystemInfo
{
public string CheckTotalFreeSpace()
{
System.IO.DriveInfo DInfo = new System.IO.DriveInfo(#"C:\");
return DInfo.TotalFreeSpace.ToString();
}
public string CheckVolumeLabel()
{
System.IO.DriveInfo DInfo = new System.IO.DriveInfo(#"C:\");
return DInfo.VolumeLabel.ToString();
}
}
I want to send huge data from one class (see my example) into my form class (maybe labels or ListBox Control), by using a a good way to solve this issue. Also I don't want to put this line of code into separate c# class method:
System.IO.DriveInfo DInfo
The basic issue for me is: deal and show many info about my computer so I need to put all this info into one structure or something else.
You may use Lazy class:
using System.IO;
public class FileSystemInfo
{
private readonly Lazy<DriveInfo> dInfo =
new Lazy<DriveInfo>(() => new DriveInfo(#"C:\"));
public string CheckTotalFreeSpace()
{
return dInfo.Value.TotalFreeSpace.ToString();
}
public string CheckVolumeLabel()
{
return dInfo.Value.VolumeLabel.ToString();
}
}

Increase the Scope of Passed Arguments [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a handful of variables passed from one form to another, but now I'm realizing these variables can't be accessed outside the form method.
public Form3(int str, int dex, int vit, int arc, int hp, int mp, int sp, string name, string charClass)
{
...
}
I'd like to be able to access the arguments from other methods. Is it possible to increase the scope of these arguments within the class itself, or do I need to go to the roots and declare them differently?
Make them class-level members. For example:
public class Form3
{
private int Str { get; set; }
public Form3(int str)
{
Str = str;
}
private void SomeOtherMethod()
{
// here you can access Str
}
// other methods, etc.
}

C#, I want to call a method with parameters like var test = new test1.test2.Method("parameter1","parameter2") [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to call a method in a fixed structure like below in C#:
var Test = new test1.test2.test3("parameter1","parameter2");
Is this possible in C#?
Here test1 and test2 can be classes and test3 is my method name and it will return string text.
I can manage if need to remove new keyword.
I am assuming it should something like this :-
Public class test1
{
Public class test2
{
Public string test3("parameter1","parameter2")
{
//Do something
}
}
}
Yes, you can if test3 is public structure type nested inside public structure type test2 which is nested inside test1
struct test1{
public struct test2{
public struct test3{
public test3(string p1,string p2) {/*do something*/}
//some params
}
//some params
//some params
}
Also test1 and test2 could be namespaces.

Categories

Resources